✏️ 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);

👁 60 Views
📘 Detailed Answer
🟢 Easy
No previous question
No next question
💡

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.
No previous question
No next question