Govt Exams
int x = 5;
if(x > 3)
if(x < 10)
printf("Pass");
else
printf("Fail");
The inner if condition (x < 10) is true, so 'Pass' is printed. The else is associated with the inner if, not the outer one.
continue skips all remaining statements in the current loop iteration and jumps to the increment/condition check for the next iteration.
switch(x) {
case 1:
case 2:
case 3: printf("A"); break;
default: printf("B");
}
What advantage does stacking cases provide?
Stacking cases (without break between them) allows multiple values to execute the same code block, reducing redundancy.
int i = 0;
do {
printf("%d ", i);
} while(++i < 3);
do-while executes body first (prints 0), then checks ++i < 3. Loop continues for i=1,2. When i=3, condition fails. Output: 0 1 2
switch typically uses jump tables for efficient multi-way branching with integral values, while if-else evaluates boolean expressions sequentially.
int x = 0;
while(1) {
if(++x > 3) break;
printf("%d ", x);
}
What is the output?
Pre-increment ++x happens first. When x becomes 4, condition is true and loop breaks. Values printed: x=1,2,3 (before x reaches 4).
break statement exits only the innermost loop it's in. To break outer loop, you need additional logic or goto statement.
for(int i = 1; i
Outer loop: i=1,2,3. Inner loop repeats i times. When i=1: print '1' once. i=2: print '2' twice. i=3: print '3' thrice. Each iteration separated by space.
Switch works with integral types only. Multiple case labels before statements (fall-through) is valid. Strings and floats are not allowed.
int i = 0;
while(i < 5) {
if(i == 2) {
i++;
continue;
}
printf("%d ", i);
i++;
}
When i==2, continue skips printf but still increments i. So 2 is not printed. Output: 0 1 3 4