printf("infinitive for loop example by javatpoint");
Basis of Difference For Loop While Loop Do While Loop
The for loop is
appropriate
The other two loops i.e. while and do
when we know in
while loops are more suitable in the
advance
situations where it is not known before
how many times the
hand when the loop will terminate.
loop
will be executed.
In case if the
Where to
test condition In case if the test
Use for Loop, while Loop
fails at the condition fails at the
and do while Loop
beginning, and beginning, and you
you may not may want to execute
want to execute the body of the loop
the body of the atleast once even in
loop even once the failed condition,
if it fails, then then the do while
the while loop loop should be
should be preferred.
preferred.
A for loop initially A while loop A do while loop will
initiates a counter will always always executed the
variable (initialization- evaluate the code in the do {} i.e.
expression), then it test-expression body of the loop
checks the initially. It the block first and then
How all the three loops
test-expression, and test-expression evaluates the
works?
executes the body of becomes true, condition. In this
the loop if the test then the body of case also, the counter
expression is true. the loop will be variable is initialized
After executing the executed. The outside the body of
body of the loop, update the loop.