Govt. Exams
Entrance Exams
char ch = 'B';
switch(ch) {
case 'A':
case 'B':
case 'C': printf("Vowel");
break;
default: printf("Consonant");
}
ch='B' matches case 'B', which has no statements. Execution falls through to case 'C' which has no statements, then prints 'Vowel' and breaks.
Both break statements exit their immediate enclosing structure. In loops, break exits the loop; in switch, break exits the switch block.
switch statement is optimized for multiple discrete value checks and is more readable than chained if-else statements.
int n = 5;
switch(n) {
case 4: printf("Four");
case 5: printf("Five");
case 6: printf("Six");
break;
default: printf("Other");
}
n=5 matches case 5. Without break, it falls through to case 6 before encountering break. Prints 'FiveSix'.
int i = 0;
while(i++ < 5) {
printf("%d ", i);
}
i++ returns value before incrementing. Condition checks: 0<5(T), 1<5(T), 2<5(T), 3<5(T), 4<5(T), 5<5(F). Loop runs 5 times.
for(int i = 1; i
Nested loop: outer loop runs 3 times, inner loop runs i times. Prints 1, then 12, then 123 on separate lines.
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.