Entrance Exams
Govt. Exams
The 'break' statement is used to exit or terminate a loop prematurely. 'exit' terminates the entire program, while 'return' exits functions.
int i = 0;
while(i < 3) {
printf("%d ", i++);
if(i == 2) continue;
}
The loop prints 0, then i becomes 1. When i becomes 2, continue skips the rest but the loop continues. i becomes 3 and loop exits. Output: 0 1 2
A switch statement is ideal for menu-driven programs where you match user input against specific cases. It's more efficient and readable than multiple if-else statements for this purpose.
Variables declared in a for loop's initialization (e.g., for(int i=0; ...)) have block scope limited to that loop. They are not accessible outside the loop.
Each break statement exits only its immediately enclosing loop. To exit multiple nested loops, you need one break for each level you want to exit.
Switch case values must be constant integral expressions (int, char, enum). Floating-point values and strings (in C89/C90) are not allowed.
The if-else-if chain allows testing multiple different conditions sequentially. switch is used for a single expression with multiple values.
When continue is executed in a for loop, the post-increment (i++) still executes before the condition is checked again. Only the body statements after continue are skipped.
The exit() function terminates the entire program and returns control to the operating system. break exits loops, and return exits functions.
A do-while loop executes the body at least once before checking the condition. Even if the condition is false, the body executes one time.