🌐
W3Schools
w3schools.com › c › c_for_loop.php
C For Loop
Statement 2 defines the condition for the loop to run: i < 5.
🌐
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
Discussions

Learning C: For Loops
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. More on reddit.com
🌐 r/C_Programming
16
1
August 10, 2024
What is the full "for" loop syntax in C? - Stack Overflow
I have seen some very weird for loops when reading other people's code. I have been trying to search for a full syntax explanation for the for loop in C but it is very hard because the word "for" a... More on stackoverflow.com
🌐 stackoverflow.com
C++26: Std:Is_within_lifetime
It'd be funny if it ends up being just C++35 · C Source code => Tradicional UNIX C compiler => ASM => object file More on news.ycombinator.com
🌐 news.ycombinator.com
73
50
3 weeks ago
Multiple conditions in a C 'for' loop - Stack Overflow
Completing Mr. Crocker's answer, be careful about ++ or -- operators or I don't know maybe other operators. They can affect the loop. for example I saw a code similar to this one in a course: More on stackoverflow.com
🌐 stackoverflow.com
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....
🌐
Programiz
programiz.com › cpp-programming › for-loop
C++ for Loop (With Examples)
... Write a function to calculate the sum of natural numbers. Return the sum of the first n natural numbers. Natural numbers are positive integers starting from 1. The sum of the first n natural numbers can be calculated using the formula: sum = n * (n + 1) / 2. For example, if n = 10, the ...
🌐
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
🌐
HackerRank
hackerrank.com › domains › c
Solve C Code Challenges
Please read our cookie policy for more information about how we use cookies. Ok · Prepare · C · Solve Challenge · Solve Challenge · Solve Challenge · Solve Challenge · Solve Challenge · Solve Challenge · Solve Challenge · Solve Challenge · Solve Challenge · Solve Challenge · Status · Solved · Unsolved · Skills · C (Basic) C (Intermediate) Difficulty · Easy · Medium · Hard · Subdomains · Introduction · Conditionals and Loops ·
🌐
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....
Find elsewhere
🌐
WsCube Tech
wscubetech.com › resources › c-programming › for-loop
For Loop in C (Syntax, Examples, Flowchart)
5 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.
🌐
Visual Studio Code
code.visualstudio.com › docs › cpp › config-mingw
Using GCC with MinGW
November 3, 2021 - You can do this by setting a watch on the variable. Place the insertion point inside the loop. In the Watch window, select the plus sign and in the text box, type word, which is the name of the loop variable. Now view the Watch window as you step through the loop. Add another watch by adding this statement before the loop: int i = 0;. Then, inside the loop, add this statement: ++i;. Now add a watch for i as you did in the previous step.
🌐
Makefile Tutorial
makefiletutorial.com
Makefile Tutorial by Example
Simply expanded (using :=) allows you to append to a variable. Recursive definitions will give an infinite loop error. one = hello # one gets defined as a simply expanded variable (:=) and thus can handle appending one := ${one} there all: echo $(one)
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.

🌐
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:
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_while_loop.htm
C - While Loop
The following flowchart represents how the while loop works − · The C compiler evaluates the expression. If the expression is true, the code block that follows, will be executed. If the expression is false, the compiler ignores the block next to the while keyword, and proceeds to the immediately next statement after the block.
🌐
Hacker News
news.ycombinator.com › item
C++26: Std:Is_within_lifetime | Hacker News
3 weeks ago - It'd be funny if it ends up being just C++35 · C Source code => Tradicional UNIX C compiler => ASM => object file
🌐
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 ...
🌐
Kotlin
kotlinlang.org › docs › basic-syntax.html
Basic syntax overview | Kotlin Documentation
See while loop. //sampleStart fun describe(obj: Any): String = when (obj) { 1 -> "One" "Hello" -> "Greeting" is Long -> "Long" !is String -> "Not a string" else -> "Unknown" } //sampleEnd fun main() { println(describe(1)) println(describe("Hello")) println(describe(1000L)) println(describe(2)) println(describe("other")) } See when expressions and statements. Check if a number is within a range using in operator:
🌐
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
December 4, 2024 - 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; }...
🌐
Log2Base2
log2base2.com › C › loop › for-loop-in-c.html
for loop in c
Here we exactly know the loop iteration count which is 10. ... Here also we know the iteration count exactly which is 100. for (initialize; condition; increment or decrement) { //statements }