🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-for-loop
C for Loop - GeeksforGeeks
The outer loop iterates through the rows (from 1 to 5), and the inner loop (controlled by j) iterates through the columns (also from 1 to 5). For each combination of i and j, the product i * j is printed, creating the table entries.
Published   October 8, 2025
🌐
W3Schools
w3schools.com › c › c_for_loop.php
C For Loop
Statement 2 defines the condition for the loop to run: i < 5.
People also ask

What is for loop in C?
A for loop in C is a control statement that repeats a block of code a specific number of times by handling initialization, condition checking, and updating together in a single statement.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › for-loop
For Loop in C (Syntax, Examples, Flowchart)
When do we use for loop in C?
We use for loop in C when the number of iterations is fixed, such as generating patterns, calculating sums, or iterating arrays.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › for-loop
For Loop in C (Syntax, Examples, Flowchart)
Why do we use for loop in C?
We use for loop in C to execute a block of code multiple times when the number of iterations is known beforehand, like printing numbers or iterating arrays.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › for-loop
For Loop in C (Syntax, Examples, Flowchart)
🌐
Programiz
programiz.com › c-programming › c-for-loop
C for Loop (With Examples)
Before we wrap up, let’s put ... non-negative integer n is the product of all positive integers less than or equal to n. For example, the factorial of 3 is 3 * 2 * 1 = 6....
🌐
WsCube Tech
wscubetech.com › resources › c-programming › for-loop
For Loop in C (Syntax, Examples, Flowchart)
3 days ago - Learn in this tutorial about the for loop in C language, including its syntax, examples, and flowchart. Understand how it works to repeat tasks in C programs.
🌐
Reddit
reddit.com › r/c_programming › learning c: for loops
r/C_Programming on Reddit: Learning C: For Loops
August 10, 2024 -

Hello, I am learning 'for' loops and I wanted to know a bit more about their function as far as the order in which it operates. I recently started Harvard's CS50 program and I was having trouble with the Mario Bricks solution which involves a 'for' loop within a 'for' loop. Attempting to wrap my head around it without a visual representation is difficult. I can paste the C file itself if that helps.

Top answer
1 of 3
4
I see some technical explanations below. I'll try a different approach. For loops in C can be a little confusing because of the syntax and because of how how they use a conditional. Let's look at a Python for loop (which is also a little confusing): for x in range(2, 6): print(x) This will print 2 3 4 5. It won't print 6, but let's not worry about that for now. This loops a certain number of times with an staring number and an ending number. The equivalent in C is: for (i = 2; i < 6; i++) { printf("%d ", i); } Hopefully that makes sense. They Python example will actually put each number on a new line, and the C example will have spaces between them. Don't worry about that for now. Does this make sense so far? For loops are generally used when you know how many times you want to loop. That number could also be a variable. So you could have a user enter a number and then you could loop that number of times, for example. Here's one reason for loops in C can be confusing: the conditional part of the loop is really a "while" clause, not just a counter. The conditional in a for loop in C can be any conditional that will evaluate to true or false. This while loop is the same functionally as the for loop above: i = 2; while (i < 6) { printf("%d ", i); i++; } For loops in C are not limited to looping a certain number of times. They are a more convenient way of creating an incrementing/decrementing while loop in some cases. I hope that helped.
2 of 3
3
basic idea is for(; ; ){ //do something } the {} is optional if you only need to loop one statement. Initialization is where variables are optionally declared to be used within the scope of the loop(variables declared here can't be used outside of the loop). Condition is an optional expression that determines when the loop ends. update is an expression that executes each pass of the loop, that is, when the stuff inside the {} finishes. for(int i = 0; i < 5; i++){ printf("%d ", i); } results in: 0 1 2 3 4 A nested loop is simply a loop in a loop. Each pass of the outer loop, you execute the entire inner loop. Generally you want to avoid nesting loops whenever possible, it makes for very slow code. Also for(;;) //do something is a valid loop. it runs infinitely.
🌐
w3resource
w3resource.com › c-programming-exercises › for-loop › index.php
C programming exercises: For Loop - w3resource
Write a C program to convert a binary number into a decimal number without using array, function and while loop. Test Data : Input a binary number :1010101 Expected Output : The Binary Number : 1010101 The equivalent Decimal Number : 85 Click ...
🌐
Steve's Data Tips and Tricks
spsanderson.com › steveondata › posts › 2024-12-04
Mastering For Loops in C: A Comprehensive Beginner’s Guide with Examples – Steve's Data Tips and Tricks
Increment/Decrement: After the ... with a very simple example that prints the numbers 1 to 5: #include <stdio.h> int main() { for (int i = 1; i <= 5; i++) { printf("%d ", i); } return 0; }...
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_for_loop.htm
For Loop in C
Most programming languages including C support the for keyword for constructing a loop. In C, the other loop-related keywords are while and do-while. Unlike the other two types, the for loop is called an automatic loop, and is usually the first
Find elsewhere
🌐
Study.com
study.com › courses › computer science courses › computer science 111: programming in c
For Loop in C Programming | Definition, Syntax & Examples - Lesson | Study.com
May 17, 2019 - In this example, finding an incorrect password would result in a true evaluation, and the block in D would run by asking for another password input and increasing the count of attempts by one.
🌐
BeginnersBook
beginnersbook.com › 2014 › 01 › c-for-loop
C – for loop in C programming with example
The counter variable is initialized before the loop and incremented inside the loop. int num=10; for (;num<20;) { //Statements num++; } 5) As mentioned above, the counter variable can be decremented as well. In the below example the variable gets decremented each time the loop runs until the condition num>10 returns false.
🌐
Learn C
learnc.net › home › learn c programming › c for loop
An Essential Guide to C for loop Statement By Examples
April 13, 2025 - To check if a number is even, you compare the remainder of the division of a number and 2. If the result is zero, the number is even. The following example uses a for loop statement to calculate the sum of consecutive numbers from 1 to n, where n is an input number:
🌐
freeCodeCamp
freecodecamp.org › news › for-loops-in-c
For Loops in C – Explained with Code Examples
November 3, 2021 - And the value of count is increased ... evaluates to true. In this example, the looping condition count < = 10 evaluates to false when the count value is 11 – and your loop terminates....
🌐
W3Schools
w3schools.com › c › c_for_loop_reallife.php
C Real-Life For Loop Examples
C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Compiler C Syllabus C Study Plan C Interview Q&A C Certificate ... To demonstrate a practical example of the for loop, let's create a program that counts to 100 by tens:
🌐
ScholarHat
scholarhat.com › home
Loop in C with Examples: For, While, Do..While Loops
We know it will take 10 iterations to print 10 numbers so, we have used the for loop. ... The condition i<10 will be checked. It is true, therefore i+1 i.e.
Published   July 31, 2025
🌐
Unstop
unstop.com › home › blog › for loop in c explained with detailed code examples
For Loop In C Explained With Detailed Code Examples
January 12, 2024 - The printf() statement inside the loop consists of a formatted string, which prints the iteration number (i) and the current sum. After the loop runs five times, we use another printf() statement to display the final sum of the first five natural numbers to the console. The program terminates with a return 0. Time Complexity: O(n) - The loop runs a constant number of times (5 in this case), leading to linear time complexity. Also read- Compilation In C | A Step-By-Step Explanation & More (+Examples)
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-loops
Loops in C - GeeksforGeeks
December 6, 2025 - In the following code, we have used a loop to print text 3 times. We could have done it for 100 or even 1000 times in the same number of code lines.
Top answer
1 of 7
133

The comma is not exclusive of for loops; it is the comma operator.

x = (a, b);

will do first a, then b, then set x to the value of b.

The for syntax is:

for (init; condition; increment)
    ...

Which is somewhat (ignoring continue and break for now) equivalent to:

init;
while (condition) {
    ...
    increment;
}

So your for loop example is (again ignoring continue and break) equivalent to

p=0;
while (p+=(a&1)*b,a!=1) {
    ...
    a>>=1,b<<=1;
}

Which acts as if it were (again ignoring continue and break):

p=0; 
while (true) {
    p+=(a&1)*b;
    if (a == 1) break;
    ...
    a>>=1;
    b<<=1;
}

Two extra details of the for loop which were not in the simplified conversion to a while loop above:

  • If the condition is omitted, it is always true (resulting in an infinite loop unless a break, goto, or something else breaks the loop).
  • A continue acts as if it were a goto to a label just before the increment, unlike a continue in the while loop which would skip the increment.

Also, an important detail about the comma operator: it is a sequence point, like && and || (which is why I can split it in separate statements and keep its meaning intact).


Changes in C99

The C99 standard introduces a couple of nuances not mentioned earlier in this explanation (which is very good for C89/C90).

First, all loops are blocks in their own right. Effectively,

for (...) { ... }

is itself wrapped in a pair of braces

{
for (...) { ... }
}

The standard sayeth:

ISO/IEC 9899:1999 §6.8.5 Iteration statements

¶5 An iteration statement is a block whose scope is a strict subset of the scope of its enclosing block. The loop body is also a block whose scope is a strict subset of the scope of the iteration statement.

This is also described in the Rationale in terms of the extra set of braces.

Secondly, the init portion in C99 can be a (single) declaration, as in

for (int i = 0; i < sizeof(something); i++) { ... }

Now the 'block wrapped around the loop' comes into its own; it explains why the variable i cannot be accessed outside the loop. You can declare more than one variable, but they must all be of the same type:

for (int i = 0, j = sizeof(something); i < j; i++, j--) { ... }

The standard sayeth:

ISO/IEC 9899:1999 §6.8.5.3 The for statement

The statement

for ( clause-1 ; expression-2 ; expression-3 ) statement

behaves as follows: The expression expression-2 is the controlling expression that is evaluated before each execution of the loop body. The expression expression-3 is evaluated as a void expression after each execution of the loop body. If clause-1 is a declaration, the scope of any variables it declares is the remainder of the declaration and the entire loop, including the other two expressions; it is reached in the order of execution before the first evaluation of the controlling expression. If clause-1 is an expression, it is evaluated as a void expression before the first evaluation of the controlling expression.133)

Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a nonzero constant.

133) Thus, clause-1 specifies initialization for the loop, possibly declaring one or more variables for use in the loop; the controlling expression, expression-2, specifies an evaluation made before each iteration, such that execution of the loop continues until the expression compares equal to 0; and expression-3 specifies an operation (such as incrementing) that is performed after each iteration.

2 of 7
8

The comma simply separates two expressions and is valid anywhere in C where a normal expression is allowed. These are executed in order from left to right. The value of the rightmost expression is the value of the overall expression.

for loops consist of three parts, any of which may also be empty; one (the first) is executed before the first iteration, and one (the third) at the end of each iteration. These parts usually initialize and increment a counter, respectively; but they may do anything.

The second part is a test that is executed at the beginning of each execution. If the test yields false, the loop is aborted. That's all there is to it.

🌐
R-bloggers
r-bloggers.com › r bloggers › mastering for loops in c: a comprehensive beginner’s guide with examples
Mastering For Loops in C: A Comprehensive Beginner’s Guide with Examples | R-bloggers
December 4, 2024 - This process continues until the condition is false. Let’s start with a very simple example that prints the numbers 1 to 5: #include <stdio.h> int main() { for (int i = 1; i <= 5; i++) { printf("%d ", i); } return 0; }
🌐
Udemy
blog.udemy.com › home › introduction to for loop c: how to repeat code blocks with the for() loop
Introduction to For Loop in C with Examples - Udemy Blog
February 16, 2022 - An infinitely recursive loop can be a huge problem if it’s done accidentally but is sometimes useful when done intentionally. Here is an example of an infinitely recursive loop: for (i = 1; i <= 10; i = 1) { printf(“%d\n”, i); }