Govt Exams
goto cannot jump across function boundaries. It can only jump within the same function to a labeled statement either forward or backward.
for (int i = 0; i < 5; i++) {
if (i % 2 == 0) continue;
printf("%d ", i);
}
continue skips even numbers. Loop prints only odd values: 1 and 3.
int i = 0;
while (i < 3) {
printf("%d ", i++);
}
i starts at 0. Loop prints i then increments: prints 0, then 1, then 2. When i becomes 3, condition fails.
int n = 1;
do {
printf("%d ", n);
n++;
} while (n > 5);
The do-while executes once (prints 1), then n becomes 2. Since 2 > 5 is false, the loop exits.
int x = 5;
while (x-- > 0)
printf("%d ", x);
x-- returns 5 first, then decrements to 4. Loop runs while x > 0, printing 4, 3, 2, 1, 0.
for (int i = 0; i < 5; ++i) {
if (i == 2) continue;
if (i == 4) break;
}
Loop runs for i=0,1,2,3. When i=4, break is executed. The body executes 4 times (iterations for i=0,1,2,3).
int x = 2;
switch(x) {
case 1: printf("One"); break;
case 2: printf("Two");
case 3: printf("Three"); break;
default: printf("Other");
}
Case 2 matches. Since there's no break after case 2, fall-through occurs to case 3, printing 'TwoThree'.
int a = 10, b = 20;
if (a > b)
if (b > 0)
printf("X");
else
printf("Y");
The else clause pairs with the inner if. Since a > b is false, the else block executes printing 'Y'.
for (int i = 1; i
When i becomes 3, break is executed, exiting the loop. So only 1 and 2 are printed.
int x = 0;
if (x = 5)
printf("True");
else
printf("False");
x = 5 assigns 5 to x and evaluates to 5 (non-zero), which is true. So 'True' is printed.