













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!














Because the condition is checked later, the statements written inside the do-while block run at least one time , even if the condition is false initially. This unique nature makes it different from for and while loops. It is widely used in menu-driven programs, repeated user input, and retry-based operations , where execution must occur before validation. Why it executes minimum one time? In a do-while loop, the control enters the loop first, executes the block, and then checks the condition. If the condition is true, the loop repeats, otherwise it exits. This ensures one guaranteed execution regardless of the condition value — even if false from the beginning. Syntax: - do { statements; } while(condition); Example Program: - #include int main() { int i = 1 ; do { printf("%d ", i); i++; } while(i <= 5 ); return 0 ; } Output: - 1 2 3 4 5 Explanation Here the loop prints numbers starting from 1. Even if the condition is wrong at the beginning, the block executes once before checking. After printing, the increment updates i, then condition (i <= 5) is checked. The loop continues until i becomes 6, after which condition becomes false and loop stops. Therefore, a do-while loop is best when a program must execute once before checking conditions — like menus, password attempts, games, and user-driven tasks.
Output Example: Using for loop: 1 2 3 4 5 6 7 8 9 10 Using while loop: 1 2 3 4 5 6 7 8 9 10 Using do-while loop: 1 2 3 4 5 6 7 8 9 10 Explanation: All three loops successfully print numbers from 1 to 10 but differ in control mechanism. for loop handles initialization, condition, and increment in a single statement. while loop checks condition before execution — may run zero times. do-while loop prints first, then checks condition — guarantees one execution. Thus, although the task and output remain same, the working style of each loop is different, making this example perfect for understanding loop behavior in C.
2. while Loop: - A while loop executes only if the condition is true at the beginning. It is used when the number of repetitions is not fixed and depends on logic or input. Initialization and increment must be written separately. Syntax: - while(condition) { statements; } Proper Example Program: - #include #include int main() { int i = 1 ; printf("Printing numbers using while loop:\n"); while(i <= 5 ) { printf("%d ", i); i++; } getch(); return 0 ; } Output: - Printing numbers using while loop: 1 2 3 4 5
2) continue Statement: - The continue statement skips the current iteration of the loop. It does not exit the loop but jumps to the next cycle directly. Syntax: continue; Proper Example Program: #include #include int main() { int i; printf("Demonstration of continue statement:\n"); for(i = 1 ; i <= 6 ; i++) { if(i == 3 || i == 5 ) continue; printf("%d ", i); } getch(); return 0 ; } Output: 1 2 4 6 Explanation: Only 3 and 5 are skipped. Loop continues normally for other values. 3) goto Statement: - The goto statement is used to jump control to a labeled statement within same function. It can perform loop-like behaviour and jump anywhere instantly. However, excessive use may make programs unstructured. Syntax: goto label; label: statements; Proper Example Program: #include #include int main() { int i = 1 ; start: printf("%d ", i); i++; if(i <= 10 ) goto start; getch(); return 0 ; }
**C Program: