Entrance Exams
Govt. Exams
do-while executes the body first, then checks the condition, ensuring at least one execution.
The for loop syntax is for(init; condition; increment) allowing all three components in one statement.
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 the current iteration of the innermost loop and moves to the next iteration, while break exits the loop entirely.
continue skips all remaining statements in the current loop iteration and jumps to the increment/condition check for the next iteration.
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
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.
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 loop order: initialization → check condition → execute body → increment → repeat from condition check.