A for loop could be called syntactic sugar. for (init; test; increment) {body} It translates to: init; while (test) {body; increment} Init. If test do body and do increment. Repeat until test fails. Yes, you could intermingle body and increment. I've done it, and it works. Don't though - the point of the for() loop is readability. In addition to readability it controls the scope of the token you're defining during init. That variable is (or should be) scoped to the loop only, and be inaccessible outside of it. Helps reduce "wth just happened" debugging moments when an init goes wrong. Answer from kagato87 on reddit.com
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_loop_for.asp
W3Schools.com
For Loops can execute a block of code a number of times.
Discussions

for loop syntax - C++ Forum
So r will have the below values: ... and the loop exits. EDIT: ninja'd by giblit ... thank you mr. giblit...this is not math 101..this is programming...i dont think is that obvious for a beginner...then why dont we just write...for(int r = 0; r!=5; r++) i understand and accept what you say...but i dont think is that obvious..it could as well be a special syntax for something ... More on cplusplus.com
๐ŸŒ cplusplus.com
Syntactic sugar in C - (ab)using "for" loops

There is a caveat though. As the author said in the summary section, while the prologue/epilogue example should work in normal conditions, it would break (pun not intended) when you break or goto inside the loop. And break inside the loop happens a lot. Be careful when using this to manage resources. In C++, using a stack allocated object with a destructor in the initialization part can handle exceptional control flows.

More on reddit.com
๐ŸŒ r/programming
30
211
May 14, 2014
What makes for-loops difficult for beginners?
The syntax of for loops in C-like languages is not exactly intuitive. Even the name "for" seems like a rather non-descriptive, meaningless word. I don't know how for loops are commonly taught, but perhaps it is common to see them introduced with examples like this: for (int i = 0; i < 10; ++i) print i; Now, it is true that the most common use of for loops is to repeat something a certain number of times like this, but if this is how for loops are introduced, then I can understand how they seem mysterious. What in the world is all that garbage syntax doing there for a simple counting loop? Why are there all of those semicolons, and why do you have to say ++i? Etc. It would be better, I think, to teach that the loop for (initialization; condition; update) statement; is (almost) equivalent to initialization; while (condition) { statement; update; } and then show several examples of the same code written in both of these ways to demonstrate and reinforce this equivalence. These examples should include not only the common case of repeating a statement a certain number of times, but also other uses of the for loop, such as finding the length of a C string (where the condition is like *p != '\0'). More on reddit.com
๐ŸŒ r/learnprogramming
7
2
April 3, 2013
While loop vs. For loops

http://stackoverflow.com/questions/3875114/why-use-a-for-loop-instead-of-a-while-loop

Notably:

There is a semantic difference between the two. While loops, in general, are meant to have an indefinite number of iterations. (ie. until the file has been read..no matter how many lines are in it), and for loops should have a more definite number of iterations. (loop through all of the elements in a collection, which we can count based on the size of the collection.)

More on reddit.com
๐ŸŒ r/learnprogramming
11
3
November 24, 2014
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ for-loop-in-programming
For loop in Programming - GeeksforGeeks
July 23, 2025 - The general syntax of for loop varies slightly depending on the programming language, but it typically consists of three main components: initialization, condition, and increment (or decrement).
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_for_loop.php
C For Loop
Statement 3 increases a value each time the code block in the loop has been executed: i++ ... int sum = 0; int i; for (i = 1; i <= 5; i++) { sum = sum + i; } printf("Sum is %d", sum); Try it Yourself ยป
๐ŸŒ
Cppreference
en.cppreference.com โ€บ w โ€บ cpp โ€บ language โ€บ for.html
for loop - cppreference.com
As is the case with while loop, if statement is not a compound statement, the scope of variables declared in it is limited to the loop body as if it was a compound statement. ... As part of the C++ forward progress guarantee, the behavior is undefined if a loop that is not a trivial infinite ...
๐ŸŒ
Stanford
web.stanford.edu โ€บ class โ€บ archive โ€บ cs โ€บ cs106a โ€บ cs106a.1202 โ€บ handouts โ€บ py-loop.html
For Loop
Here is a for-loop example that prints a few numbers: ... Loop Syntax: the loop begins with the keyword for followed by a variable name to use in the loop, e.g. num in this example. Then the keyword in and a collection of elements for the loop, e.g.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ computer science fundamentals โ€บ for-loop-syntax
For loop Syntax - GeeksforGeeks
February 14, 2024 - Typically, you increment or decrement the variable to ensure progress toward the loop termination condition. for (initialization; condition; update) { // Code block to be executed repeatedly as long as the condition is true }
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Statements โ€บ for
for - JavaScript | MDN
The for statement creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a statement (usually a block statement) to be executed in the loop.
๐ŸŒ
Stanford CS
cs.stanford.edu โ€บ people โ€บ nick โ€บ py โ€บ python-for.html
For Loop - Stanford Computer Science
Here is a for-loop example that prints a few numbers: ... Loop Syntax: the loop begins with the keyword for followed by a variable name to use in the loop, e.g. num in this example. Then the keyword in and a collection of elements for the loop, e.g.
๐ŸŒ
Berkeley
pythonnumericalmethods.studentorg.berkeley.edu โ€บ notebooks โ€บ chapter05.01-For-Loops.html
For-Loops โ€” Python Numerical Methods
A for-loop assigns the looping variable to the first element of the sequence. It executes everything in the code block. Then it assigns the looping variable to the next element of the sequence and executes the code block again.
๐ŸŒ
Mozilla
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Guide โ€บ Loops_and_iteration
Loops and iteration - JavaScript | MDN
A for loop repeats until a specified condition evaluates to false. The JavaScript for loop is similar to the Java and C for loop. ... The initializing expression initialization, if any, is executed. This expression usually initializes one or more loop counters, but the syntax allows an expression ...
๐ŸŒ
University of Utah
users.cs.utah.edu โ€บ ~germain โ€บ PPS โ€บ Topics โ€บ for_loops.html
Programming - For Loop
The variable "i" below is always used as the loop counter. The variables, start_value,by_count,and finish_value all represent numbers. For each language and example of the code to sum the numbers from 1 to 10 is given. % design pattern for i = start_value:by_count:finish_value do something end % example: sum numbers from 1 to 10 total = 0; for i = 1 : 10 total = total + i; end
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_for_loop.htm
For Loop in C
All the elements (initialization, test condition, and increment) are placed together to form a for loop inside the parenthesis with the for keyword. The syntax of the for loop in C programming language ...
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ controlflow.html
4. More Control Flow Tools โ€” Python 3.14.3 documentation
If the loop finishes without executing the break, the else clause executes. In a for loop, the else clause is executed after the loop finishes its final iteration, that is, if no break occurred.
๐ŸŒ
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.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ visual-basic โ€บ language-reference โ€บ statements โ€บ for-next-statement
For...Next Statement - Visual Basic | Microsoft Learn
July 15, 2025 - You use a For...Next structure when you want to repeat a set of statements a set number of times. In the following example, the index variable starts with a value of 1 and is incremented with each iteration of the loop, ending after the value of index reaches 5.
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 105846
for loop syntax - C++ Forum
This would mean the first one starts ... 0 and continues while it is less than or equal to 5 incrementing by one each time. Therefor loop 1 = 0 , 1 , 2 , 3 , 4 and loop 2 = 0 , 1 , 2 , 3 , 4 , 5....
๐ŸŒ
Simplilearn
simplilearn.com โ€บ home โ€บ resources โ€บ software development โ€บ understanding for loop in java with examples and syntax
Understanding For Loop in Java With Examples and Syntax
July 31, 2025 - Java provides three types of loops, i.e., โœ“ for loop โœ“ while loop โœ“ do-while loop. In this tutorial, you will learn all about for loop in Java. Start now!
Address ย  5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States