English

Give the output of the following program segment and mention how many times the loop is executed. int K= 1; do {K+=2; System. out.println (K); } while (K<=6); - Computer Applications

Advertisements
Advertisements

Question

Give the output of the following program segment and mention how many times the loop is executed.

     int K= 1;

     do

     {K+=2;

     System. out.println (K);

     } while (K<=6);

Long Answer
Advertisements

Solution

3
5
7
Number of times the loop is executed
The loop is executed 3 times.
  • Initialisation: K starts at 1.
  • 1st Iteration: K becomes 1 + 2 = 3. It prints 3. The condition (3 ≤ 6) is true, so the loop continues.
  • 2nd Iteration: K becomes 3 + 2 = 5. It prints 5. The condition (5 ≤ 6 ) is true, so the loop continues.
  • 3rd Iteration: K becomes 5 + 2 = 7. It prints 7. The condition (7 ≤ 6) is false, so the loop terminates.
shaalaa.com
  Is there an error in this question or solution?
2025-2026 (March) Official Board Paper
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×