do ... while Loop is exit controlled loop. In this loop, the codes inside the do will be executed first and then the condition in while is checked. Here's the syntax of do ... while Loop. declaration and initialization of the variable do { // Codes inside do increment / decrement } while ( conditon ); Same as while Loop, we need to declare and initialize the variable before the Loop. Here, the do part will be executed first and then the condition which is in while part is checked. If the condition is satisified then the compiler will again go to the do and execute it. NOTE : In do ... while Loop, once the do is executed before checking the condition. Example for do ... while Loop can be as of while Loop i.e Printing hello 5 times.
#include <stdio.h> int main() { int i=1; do { printf("Hello\n"); i++; } while(i<=4); return 0; }
Explaination of the program : Here, we have first declared and initialized the variable i to 1. We are printing Hello and incrementing the value of i inside the do part of the Loop and the condition in while is i <= 4 ( i less than or equal to 4, because once the compiler will print the Hello as the do part will execute first. \n means breaking of line. Once the condition gets break, the compiler will skip the do ... while Loop and execute the next line of code.
0 Comment