r/cprogramming • u/long_onion1 • Oct 25 '26
what is the difference between a (specifc) while loop and a (specific) for loop (read body)
what is the difference between
for (i = 0; i < 10; i++) {}
and
i = 0;
while (i < 10) {
i++;
}
edit: thank you all for commenting, now i know, and u/deckarep, no not homework, just a question, to see if there were any difference
12
Upvotes
14
u/ACRM64 Oct 25 '26
From a programming perspective they are the same with the for() loop being rather more self-explanatory about what you are trying to do.
The while() loop gives you a bit more flexibility, like being able to increment the counter before, or in the middle of the code block instead of at the end. The code block could also decrement, reset, or not alter the counter.
7
u/Recycled5000 Oct 25 '26
A continue statement will have different effects: in the for-loop, a continue will execute the increment part, then the loop condition test. Whereas in the whole loop, since there is no explicit increment part, a continue statement will skip directly to the condition test.
5
u/onlyonequickquestion Oct 25 '26
Slap em into https://godbolt.org/ with various optimization levels and see what's generated
5
u/Hot_Dog2376 Oct 25 '26
inside the loop? functionally nothing.
Outside the loop, regarding the while loop, you still have i = 10, after. The for loop drops it because it is initialised in the function.
2
u/somewhereAtC Oct 25 '26
There is basically no difference, given that the i++ is always executed. In practice, the i++ could be manipulated, like i+=2 or even skipped entirely (e.g., a continue statement might short-circuit the increment).
Stylistically, I use the 2nd form when the initializer or incrementer are syntactically complex.
2
u/deckarep Oct 25 '26
They are just different forms of expression. They may result in slightly or different assembly code but the way they are written here is semantically the same.
The for loop does have the pre, condition, and post sections and the body. The while loop only has the condition and body to be more precise.
Note: you do have to be conscious of where additional code is added for the while loop , because the for loop post always runs at the end. The while loop i++ runs wherever it is placed.
Is this a homework question btw?
2
u/Classic-Rate-5104 Oct 25 '26
Both loops are empty, but if not, the i++; must be just before the closing } of the while loop to make them identical
2
u/EpochVanquisher Oct 25 '26
Both loops are equivalent to
i = 10;
2
u/Rich-Engineer2670 Oct 25 '26 edited Oct 25 '26
It comes down to the assembly code generated by the compiler mostly.....
The for loop provides all of the information for the compiler -- it can optimize the loop. The white loop does the same task, but the compiler doesn't know when the loop "ends" so it has to generate more code.
THis is a super-high-level answer, but in general, the for loop is easier for the compiler to deal with. If you're on a Linux or Unix system using gcc, try each with the -s option (it could be -S) That stops the compiler after the assembly generation stage so you can actually see teh assembly code it generates.
Remember on a 4GHz machine, this probably doesn't matter, but on an embedded system where every instruction costs you a bit of battery, every instruction saved matters.
If your compiler has a REALLY good optimizer, it might recognize the while loop can be rewritten as a for loop and do it behind your back.
1
u/ErikBKL Oct 25 '26
Usually you want to use a for loop when you know in advance the len of the dataset and a while loop when you iterate towards some sentinel
1
u/SmokeMuch7356 Oct 25 '26
As written, none. Both have the same tests and side effects in the same order,
iwill have the same value at the end.If you had any other statements in the loop body,
i++would have to be the last statement in thewhileloop for them to stay equivalent.1
u/fluffycritter Oct 25 '26
Assuming that is the total amount of code, there shouldn't be any difference between them, as the
forloop becomes syntactic sugar for that samewhileloop. A clever-enough optimizer might be able to convert that whole thing toi = 10though.1
u/Key_River7180 28d ago
For is more easily optimized and a lil bit more convenient. There is also a do while loop which is more interesting as it guarantees the body is ran at least once
0
u/Weshmek Oct 25 '26
In your example, the way you've formatted it, the increment step is at the very top of the
whileloop, so it would mean that yourwhileloop only iterates fromi = 1...9, compared to theforloop'si = 0...9But, in general, so long as you are deliberate about where you place the increments,
forandwhileloops should function identically.