DO WHILE Loop in C Language:
(1) Unlike the for and while loops, the do-while is an exit-controlled loop i.e. it evaluates its test-expression at the bottom of the loop after executing its loop-body statements. This means that a do-while loop always executes at least once. In other two loops for and while, the test-expression is evaluated at the beginning of the loop i.e. before executing the loop-body. If the test-expression evaluates to false for the first time itself, the loop is never executed. But in some situations, it is wanted that the loop-body is executed at least once, no matter what the initial state of the test-expression is. In such cases, the do-while loop is the obvious choice.
(2) Sample C language code and its output, as shown:
#include <stdio.h> #include <conio.h> //Table of 2 using for loop int main() { int x; int i; i = 0; x = 1; do { i = i + 2; printf(“2 * %d = %d \n”,x,i); x++; } while (x <= 10); getch(); return 0; } |
(3) After that go to the “Build” menu and select the “Build” option for publishing the code as shown.
(4) After building the code, press the “Run” button, as shown.
(5) After pressing the “run” button, the output screen is opened, as shown.
DO WHILE Loop Syntax:
do { //write logic } While (condition) |