Govt. Exams
Entrance Exams
Switch works with integral types only. Multiple case labels before statements (fall-through) is valid. Strings and floats are not allowed.
int i = 0;
while(i < 5) {
if(i == 2) {
i++;
continue;
}
printf("%d ", i);
i++;
}
When i==2, continue skips printf but still increments i. So 2 is not printed. Output: 0 1 3 4
int x = 5;
if(x > 3)
if(x > 10)
printf("A");
else
printf("B");
Which statement best describes the output?
The else binds to the nearest if statement (inner if). Since x=5 is not > 10, the inner condition fails and else executes, printing 'B'.
int i = 0;
while(1) {
i++;
if(i > 5) break;
}
while(1) creates an infinite loop. i increments until i > 5 (i becomes 6), then break exits. Loop runs 6 iterations.
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
if(i + j == 1) continue;
printf("Done");
continue only affects the inner loop, not the outer. The loops complete normally and 'Done' is printed once after both loops finish.
if(1) if(0) printf("A"); else printf("B");
The else binds to the nearest if (inner if). Outer if(1) is true, inner if(0) is false, so else executes printing 'B'.
Case labels must be constant integral expressions (int, char, etc.). Floating-point constants are not allowed as case labels.
int x = 0;
for(int i = 1; i
continue skips even values. x accumulates: 1(i=1) + 3(i=3) + 5(i=5) = 9. Values i=2,4 are skipped.
for(int i = 0; i < 3; i++) {
if(i == 1) goto skip;
printf("%d ", i);
}
skip: printf("End");
When i=1, goto skip jumps to the skip label, skipping the printf. i continues to 2 in the loop. Output: 0 2 End
int x = 5;
switch(x) {
case 5: printf("Five");
case 6: printf("Six");
default: printf("Default");
}
Without a break statement after case 5, control falls through to case 6 and default. This is called fall-through behavior in switch statements.