Entrance Exams
Govt. Exams
int x = 0;
while(x < 3) {
printf("%d ", x++);
if(x == 2) continue;
printf("X ");
}
Iteration 1: prints 0, x becomes 1, continue not triggered, prints X. Iteration 2: prints 1, x becomes 2, continue triggered, skips X. Iteration 3: prints 2, x becomes 3, condition fails.
Both break statements exit their immediate enclosing structure. In loops, break exits the loop; in switch, break exits the switch block.
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
if(i == j) continue;
printf("Done");
continue only skips the current iteration of innermost loop, not the outer. After both loops complete, 'Done' is printed once.
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.
The ternary operator (condition ? true_value : false_value) is a concise alternative to if-else for simple conditional assignments.
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).