Govt Exams
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.
Switch case values must be constant integral expressions (int, char, enum). Floating-point values and strings (in C89/C90) are not allowed.
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.
The correct syntax is (condition) ? (value_if_true) : (value_if_false). The condition comes first, followed by ? and then the true and false values separated by :.
The break statement exits only the innermost loop it is in. To exit multiple nested loops, you need multiple break statements or use goto.
A for loop with an empty body is valid syntax in C. The loop executes for the specified iterations but performs no operations in each iteration.
Without a break statement, execution continues to the next case (fall-through behavior). This is a common source of bugs if not intentional.