Examveda

What will be the output of the following code: for (int i = 5; i >= 0; --i) { cout << i << " "; if (i == 3) break; } in C++?

A. Compilation error

B. 5 4 3

C. 5 4 3 2 1 0

D. 5 4 3 0 1

Answer: Option C

Solution (By Examveda Team)

The given code is a for loop in C++:

for (int i = 5; i >= 0; --i) {
    cout << i << " ";
    if (i == 3) break;
}

This loop initializes i to 5 and decrements it in each iteration until i becomes less than 0.

On each iteration, it prints the current value of i followed by a space.

When i becomes 3, the if condition becomes true and the break statement terminates the loop.

Therefore, the values printed are: 5 4 3

No further values (like 2, 1, 0) are printed because the loop exits when i == 3.

Hence, the correct output is: 5 4 3

Join The Discussion

Comments (1)

  1. Farouk Mohammed
    Farouk Mohammed:
    4 months ago

    the correct answer is
    5 4 3

    becuse the loop is stop when ( i = 3 )

Related Questions on Control Flow Statements in C plus plus

What is the correct syntax for the 'switch' statement in C++?

A. switch (expression) { case constant1: statement1; case constant2: statement2; }

B. switch (expression) { case constant1: statement1; break; case constant2: statement2; break; default: defaultStatement; }

C. switch (expression) { case constant1: statement1; default: defaultStatement; }

D. None of the above