ExamVeda
Login
Home
Control Flow Statements in C plus plus
?

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

Answer & Solution
Correct Answer: Option B
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
Examveda
Question posted by Examveda
Community

Join the Discussion

2 Comments
Saad Mansoor
Saad Mansoor 8 months ago
The given answer (Option C: 5 4 3 2 1 0) is incorrect.

The loop starts with i = 5 and decrements i each iteration:

i = 5 → prints 5

i = 4 → prints 4

i = 3 → prints 3

When i == 3, the break statement executes, which immediately terminates the loop. So the loop does not continue to 2, 1, or 0.

Therefore, the output is:

5 4 3

The correct answer is Option B (5 4 3).
Farouk Mohammed
Farouk Mohammed 1 year ago
the correct answer is
5 4 3

becuse the loop is stop when ( i = 3 )