Entrance Exams
Govt. Exams
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.
break terminates the loop entirely, while continue jumps to the next iteration without executing remaining statements.
for (int i = 0; i < 3; i++)
printf("%d ", i);
The loop runs from i=0 to i=2 (i < 3), printing '0 1 2 '.
foreach is not a standard C control flow statement. C has if-else, switch, for, while, do-while, and goto.
int x = 5;
if (x > 3)
printf("A");
else
printf("B");
Since x = 5 and 5 > 3 is true, the if block executes and prints 'A'.