Even though Do loop and while loop look similar in most of the programming languages from C to C# there is main one major difference between them that is in case of Do loop it is assured to the programmer that the instruction present within the loop block while be surely executed one time before iterating for the second time because the exist statement to terminate the loop is given at the bottom of the loop.
In case of while loop even one executions is not assured because the existing statement of the loop on the top of the code block hence if the condition fails initially the program will never enter into the loop
1. Do loop syntax
do{
//code to be executed /loop body
//Update statement
}while(condition)
2. While loop syntax
while(condition){
//code block to be executed
}
Note: Do not forget to increase the variable used in the condition otherwise the loop will never end
Comments
Post a Comment