Loop questions in c

broken image
broken image

Same example using do-while loop #include Įxplanation: As I mentioned in the beginning of this guide that do-while runs at least once even if the condition is false because the condition is evaluated, after the execution of the body of loop. Printf('Value of variable j is: %d\n', j) }while(condition test) Flow diagram of do while loop Consider a part of a loop as shown below. Similar to while you can put loop counter variable-initialization statement before loop and variable-update before end of do.while loop. If loop condition is true then loop repeats otherwise terminates. The loop condition is a boolean expression evaluating to an integer value. So you can say that if a condition is false at the first place then the do while would run once, however the while loop would not run at all. How many times will the following loop be executed in below C program. Body of loop contains single or set of statements to repeat.

broken image

On the other hand in the while loop, first the condition is checked and then the statements in while loop are executed. A do while loop is similar to while loop with one exception that it executes the statements inside the body of do-while before checking the condition. In the previous tutorial we learned while loop in C.