The for is usually appropriate for loops in which the initialization and increment are single statements and logically related, since it is more compact than while and it keeps the loop control statements together in one place. The for loop seems most appropriate when number of iteration are known in advance, for example, counting array elements.
But, there could be many complex problems where number of iterations depend upon a certain condition and can't be predicated beforehand, in those situation programmers usually prefer to use while loop. The following table lists differences between for and while loop. The initialization is an assignment statement that is used to set the loop control variable.
The condition is a relational expression that determines when the loop exits. The increment defines how the loop control variable changes each time the loop is repeated. The body of loop can either be empty or a single statement or a block of statements. Yes, sometimes you could make it work with a complicated enough expressions, use of the ternary? In practice, most of my loops are either stepping through an array or structure of some kind, in which case I use a FOR loop; or are reading a file or a result set from a database, in which case I use a WHILE loop "while!
They are pretty much same except for do-while loop. The for loop is good when you have a counter kind of variable. It makes it obvious. It will depend on the programmer as to whether the while loop or for loop is used. Some are comfortable using while loop and some are with for loop. Use any loop you like. However, the do Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow.
Learn more. For vs. Ask Question. Asked 11 years, 5 months ago. Active 1 year, 4 months ago. Viewed k times. Wil Moore III 6, 3 3 gold badges 32 32 silver badges 46 46 bronze badges. Isn't recursion also considered as loop? Add a comment. Active Oldest Votes. A while loop will always evaluate the condition first. Thanks clyfe The main difference between the for 's and the while 's is a matter of pragmatics: we usually use for when there is a known number of iterations, and use while constructs when the number of iterations in not known in advance.
For loops are especially nice because they are concise. Robert Greiner Robert Greiner Should you discuss the behaviour of 'continue;' in a 'for' loop vs a 'while' loop? Otherwise, good answer. Worth noting: for loops don't require a counter variable. The main difference between the for's and the while's is a matter of pragmatics: we usually use for when there is a known number of iterations , and use while constructs when the number of iterations in not known in advance.
I love the answer, esplly the part where you talk about uniformity in incrementing the counter in case of a 'for' versus potentially anywhere in the iteration in case of a 'while'. To be honest, I would have never put it so eloquently albeit there is not much new here — Shravan. Show 9 more comments. BTW, in this specific example, the worst case is given by the "for" loop. Peter Mortensen 29k 21 21 gold badges 97 97 silver badges bronze badges. For the sake of readability.
For that matter, you can also use goto as a loop. For another matter, there's recursion. And you can replace any style of loop with any other style. In fact WhirlWind Except that in languages without recursion you'll probably end up with stack overflows. And you might end up here asking about stack overflows, which would make me smile a little bit. Sean yeah, I was thinking about that, and if there's a way to get around the stack overflow. Of course, if there's tail recursion Show 1 more comment.
Michael Mrozek Michael Mrozek k 25 25 gold badges silver badges bronze badges. Not really. Both for and while can execute the body zero times - a do-while cannot. Ugly and repetitive, but that's kind of my point about why variations exist : — Michael Mrozek. Neil: You can use an additional flag to skip the first execution. Interchangeable says nothing about how easy or useful it is.
The body of the loop is still entered - this is not the case for while and for. Neil Well, for certain definitions of "body of the loop". Show 2 more comments. Justin Ethier Justin Ethier k 49 49 gold badges silver badges bronze badges. I don't know about much easier. I would hazard a guess that if C didn't have the first form, we would all be quite used to seeing the second by now.
Code should read as prose, the former is much closer to natural language. Perhaps not "much easier" but quite likely much faster to recognise and comprehend. A loop is a command that tends to repeat itself to obtain the desired result. In other words, a programming command that repeats itself either the known number of times or the unknown number of times to fulfill certain conditions is defined as a loop.
There are various kinds of loops such as for loop, while loop, if loop, if-else loop, if-else-if loop, etc. But the most commonly used loops are for and while loops.
The difference between for loop and while loop is that in for loop the number of iterations to be done is already known and is used to obtain a certain result whereas in while loop the command runs until a certain condition is reached and the statement is proved to be false. A loop in which the control statement is executed for a pre-known number of times to obtain the result is known as the for loop.
It can be said that for loop is a repetitive command. In for loop, the command is controlled by a variable. Each time the loop iterates, the predefined variable gets a new value. During the initialization of for loop, a starting value for the variable is to be declared.
This loop requires initialization only once. After initialization, the compiler checks whether the condition is true or not, and if it is true, the loop continues to iterate till the predefined number of iterations are obtained.
A while loop is when the command iterates for an uncertain number of times till the condition is true. Once the condition is proved false, the iteration of command stops.
0コメント