What is the output of the following C program? # include int main () { int…
2018
What is the output of the following C program?
# include
int main ()
{
int index;
for (index = 1; index <= 5; index ++)
{
printf ("%d", index);
if (index == 3)
continue;
}
}
- A.
1245
- B.
12345
- C.
12245
- D.
12354
Attempted by 220 students.
Show answer & explanation
Correct answer: B
index = 1:The condition
1 <= 5is true.printf ("%d", index);prints1.The
if (index == 3)condition is false, so it skips thecontinue.
index = 2:The condition
2 <= 5is true.printf ("%d", index);prints2.The
if (index == 3)condition is false, so it skips thecontinue.
index = 3:The condition
3 <= 5is true.printf ("%d", index);prints3.The
if (index == 3)condition is true, socontinue;executes.Crucial Point: Since the
printfstatement happened before theifblock, the number3is already printed. Thecontinuetriggers now, but there are no more statements left after it in the loop body anyway. The program jumps toindex++.
index = 4:The condition
4 <= 5is true.printf ("%d", index);prints4.
index = 5:The condition
5 <= 5is true.printf ("%d", index);prints5.
The loop terminates when index becomes 6.
Final Output
The characters printed sequentially are 1, 2, 3, 4, and 5.
Therefore, the correct option is 2) 12345.