for loop, while loop, and do while loop
Explain when you should use a "for" loop, a "while" loop, and a "do-while" loop.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted May 19, 2012, 9:10 AM
Satyapriya NayakPosted May 18, 2012, 2:17 PM
Loops are used to carry out certain instruction(s) in continuation for a fixed no of times.
Syntax of while loop:
//initialization stmt(s)
while (condition)
{
//stmt1;
//stmt2;
}
Do- while loop:
This is similar to while loop; the only difference is unlike while loop, in do-while loop condition is checked after the loop statements are executed. This means the statements in the do while loop are executed at least once; even if the condition fails for the first time itself.
//initialization stmt(s)
do
{
Stmt1;
Stmt2;
} while (condition)
This prints numbers 0 through 9 on the screen.
The for loop:
It does exactly the same thing as while loop; only difference is the initialization, the condition stmt is written on the same line.
for loop:
This is used when we want to execute certain statements for a fixed no of times. This is achieved by initializing a loop counter to a value and increasing or decreasing the value for certain no of times until a condition is satisfied. The loop statements are executed once for every iteration of the for loop.
for (initialize loop counter; condition checking; increasing/decreasing loop counter)
{
//stmt1;
//stmt2;
}
Also Refer the below link
http://www.dotnetperls.com/loop
Thanks