Home / Questions / Give the output of the following program segment. How many times is the loop executed? for(x=10; x>20; x++) System.out.println(x); System.out.println(x*2);
Explanatory Question

Give the output of the following program segment. How many times is the loop executed?

for(x=10; x>20; x++)
System.out.println(x);
System.out.println(x*2);

👁 59 Views
📘 Detailed Answer
🕒 Easy to Read
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.

Answer with Explanation

Step-by-Step Execution:

  1. Initialization: x = 10
  2. Condition Check: x > 20
    • Since 10 > 20 is false, the loop never executes.
  3. Loop Execution:
    • Since the condition fails in the first check, the loop does not run even once.
  4. After the Loop:
    • The statement System.out.println(x*2); is outside the loop, so it executes.
    • Since x was initialized to 10 and was never modified, x*2 = 10 * 2 = 20 is printed.