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;

}

}

  1. A.

    1245

  2. B.

    12345

  3. C.

    12245

  4. D.

    12354

Attempted by 220 students.

Show answer & explanation

Correct answer: B

  • index = 1:

    • The condition 1 <= 5 is true.

    • printf ("%d", index); prints 1.

    • The if (index == 3) condition is false, so it skips the continue.

  • index = 2:

    • The condition 2 <= 5 is true.

    • printf ("%d", index); prints 2.

    • The if (index == 3) condition is false, so it skips the continue.

  • index = 3:

    • The condition 3 <= 5 is true.

    • printf ("%d", index); prints 3.

    • The if (index == 3) condition is true, so continue; executes.

    • Crucial Point: Since the printf statement happened before the if block, the number 3 is already printed. The continue triggers now, but there are no more statements left after it in the loop body anyway. The program jumps to index++.

  • index = 4:

    • The condition 4 <= 5 is true.

    • printf ("%d", index); prints 4.

  • index = 5:

    • The condition 5 <= 5 is true.

    • printf ("%d", index); prints 5.

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.

Explore the full course: Up Lt Grade Assistant Teacher 2025