Govt. Exams
Entrance Exams
Without break, execution falls through to the next case. Default can be anywhere, and modern C allows switch with other types.
int x = 10;
while(x-- > 5) {
printf("%d ", x);
}
x-- returns the value before decrementing. Loop runs while value > 5: prints 9,8,7,6 (when x becomes 5, condition fails).
int x = 5;
if(x > 3)
if(x < 10)
printf("Pass");
else
printf("Fail");
The inner if condition (x < 10) is true, so 'Pass' is printed. The else is associated with the inner if, not the outer one.
continue skips all remaining statements in the current loop iteration and jumps to the increment/condition check for the next iteration.
switch(x) {
case 1:
case 2:
case 3: printf("A"); break;
default: printf("B");
}
What advantage does stacking cases provide?
Stacking cases (without break between them) allows multiple values to execute the same code block, reducing redundancy.
int i = 0;
do {
printf("%d ", i);
} while(++i < 3);
do-while executes body first (prints 0), then checks ++i < 3. Loop continues for i=1,2. When i=3, condition fails. Output: 0 1 2
switch typically uses jump tables for efficient multi-way branching with integral values, while if-else evaluates boolean expressions sequentially.
int x = 0;
while(1) {
if(++x > 3) break;
printf("%d ", x);
}
What is the output?
Pre-increment ++x happens first. When x becomes 4, condition is true and loop breaks. Values printed: x=1,2,3 (before x reaches 4).
break statement exits only the innermost loop it's in. To break outer loop, you need additional logic or goto statement.
for(int i = 1; i
Outer loop: i=1,2,3. Inner loop repeats i times. When i=1: print '1' once. i=2: print '2' twice. i=3: print '3' thrice. Each iteration separated by space.