int i = 0, j = 0;
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
if(i + j == 2) break;
printf("%d%d ", i, j);
}
}
Inner break only exits the inner loop. i=0,j=0: prints 00; i=0,j=1: prints 01, breaks when j=2; i=1,j=0: prints 10, breaks when j=1; i=2,j=0: breaks immediately.
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.
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.
Dangling else refers to ambiguity when else could bind to multiple if statements. C resolves this by binding else to the nearest if.
int i = 0;
while(i < 5) {
printf("%d ", i++);
if(i == 3) break;
}
Post-increment: prints 0 (i becomes 1), prints 1 (i becomes 2), prints 2 (i becomes 3). When i==3, break executes. Output: 0 1 2
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
if(i == j) continue;
printf("Done");
continue skips current iteration but doesn't affect outer loop. After nested loops complete, 'Done' prints once.
for(int i = 1; i
continue skips when i==j. i=1: prints 12 (skips 11); i=2: prints 21 (skips 22). Output: 12 21
Each break statement exits only its immediately enclosing loop. To exit multiple nested loops, you need one break for each level you want to exit.
When continue is executed in a for loop, the post-increment (i++) still executes before the condition is checked again. Only the body statements after continue are skipped.
int x = 1, y = 2;
if (x < y) {
x++; y--;
if (x == y)
printf("Equal");
else
printf("NotEqual");
}
x=1, y=2. Since 1 < 2, x becomes 2 and y becomes 1. Now x != y, but after operations x=2, y=1. Wait: x++ makes x=2, y-- makes y=1. So x==y is false, printing 'NotEqual'. Rechecking: 1 < 2 is true, so enter if. x becomes 2, y becomes 1. 2 == 1 is false, so else executes, printing 'NotEqual'.