Govt Exams
The break statement only exits the innermost loop that contains it. It does not affect the outer loop's execution. The outer loop will continue its iterations normally.
Only the break statement whose associated if condition evaluates to true will be executed, exiting the loop immediately.
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).