The while loop checks the condition first before executing the loop body, whereas the do-while loop executes the body first before checking the condition. The do-while loop will always execute the body at least once even if the condition is false, whereas the while loop may not execute the body at all if the condition is false. Both loops take different approaches to control the loop flow - the while loop uses entry control and the do-while uses exit control.
The while loop checks the condition first before executing the loop body, whereas the do-while loop executes the body first before checking the condition. The do-while loop will always execute the body at least once even if the condition is false, whereas the while loop may not execute the body at all if the condition is false. Both loops take different approaches to control the loop flow - the while loop uses entry control and the do-while uses exit control.
Comparison between the while loop and do-while loop in the C language
No while loop do-while loop
1. While the loop is an entry control loop The do-while loop is an exit control loop because firstly, the condition is checked, then because in this, first of all, the body of the loop the loop's body is executed. is executed then the condition is checked true or false. 2. The statement of while loop may not be The statement of the do-while loop must be executed at all. executed at least once. 3. The while loop terminates when the As long as the condition is true, the compiler condition becomes false. keeps executing the loop in the do-while loop. 4. In a while loop, the test condition variable In a do-while loop, the variable of test must be initialized first to check the test condition Initialized in the loop also. condition in the loop. 5. In a while loop, at the end of the condition, In this, at the end of the condition, there is a there is no semicolon. semicolon. Syntax: Syntax: while (condition) while (condition); 6. While loop is not used for creating menu- It is mostly used for creating menu-driven driven programs. programs because at least one time; the loop is executed whether the condition is true or false. 7. In a while loop, the number of executions In a do-while loop, irrespective of the condition depends on the condition defined in the mentioned, a minimum of 1 execution occurs. while block. 8. Syntax of while loop: Syntax of do-while loop: while (condition) do { { Block of statements; statements; } } Statement-x; while (condition); Statement-x; 9. Program of while loop: Program of do-while loop: #include<stdio.h> #include<stdio.h> #include<conio.h> #include<conio.h> Void main() Void main() { { int i; int i; clrscr(); clrscr(); i = 1; i = 1; while(i<=10) do { { printf("hello"); printf("hello"); i = i + 1; i = i + 1; } } getch(); while(i<=10); } getch(); } 10. Flowchart of while loop: Flowchart of do-while loop: