What will be the output of the following code snippet?
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 5; i++) {
if (i == i)
continue;
printf("%d", i);
}
return 0;
}
Explanation
Correct Option: (D) No output
Explanation
This code snippet demonstrates the use of a control flow statement, specifically the continue keyword, within a for loop.
Loop Initialization: The loop starts with i=1.
Condition Check: The loop condition i≤5 is evaluated.
The if Statement: Inside the loop, there is a conditional check: if (i == i).
The continue Statement: Because the condition is always true, the continue statement is executed in every single iteration.
The continue statement skips the remainder of the code inside the loop body (in this case, the printf function) and immediately jumps to the next iteration (incrementing i).
Mathematical logic:
∀i∈{1,2,3,4,5},(i==i) is True
Therefore, printf is never reached.
Consequently, the loop completes its iterations from 1 to 5 without ever executing the printf command, resulting in no output.