for loop &while loop difference
Answers
Answered by
1
Major difference between for and whileloop is at pragmatic level because under the hood, both loops are all the same conditional goto; therefore the choice between while and for is arbitrary, based on which seems clearer. Both for and whileloops are entry controlled loops that means test condition is checked for truth while entering into the loop's body.
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 forand while loop.
Difference Between for and while loopsforwhile
Syntax of for loop:
for(initialization; condition; iteration)
{
// body of the 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.
The
for(initialization; condition; iteration)
{
// body of the loop
}
is equivalent to
initialization;
while(condition)
{
// body of the loop
iteration;
}
Syntax of whileloop:
while(condition)
{
// body of the loop
}
The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true. When the condition becomes false, program control passes to the line of code immediately following the loop.
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 forand while loop.
Difference Between for and while loopsforwhile
Syntax of for loop:
for(initialization; condition; iteration)
{
// body of the 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.
The
for(initialization; condition; iteration)
{
// body of the loop
}
is equivalent to
initialization;
while(condition)
{
// body of the loop
iteration;
}
Syntax of whileloop:
while(condition)
{
// body of the loop
}
The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true. When the condition becomes false, program control passes to the line of code immediately following the loop.
vinaynarsimha:
ho.. great i just asked differenc
Similar questions