KnowledgeBoat Logo
|

Computer Applications

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

Java Iterative Stmts

2 Likes

Answer

Number of times the loop is executed: 3 times

Output
3
5
7
Explanation
  • The do-while loop executes the block first and then checks the condition.
  • Initial value: K = 1
    • 1st iteration: K = 1 + 2 = 3 → printed
    • Condition: 3 <= 6 → true
  • 2nd iteration: K = 3 + 2 = 5 → printed
    • Condition: 5 <= 6 → true
  • 3rd iteration: K = 5 + 2 = 7 → printed
    • Condition: 7 <= 6 → false → loop stops

Hence, the loop executes 3 times.

Answered By

1 Like


Related Questions