171k views
4 votes
public class Ex0506 { public static void main (String args[]) { int x = 2; while (x < 10) { if (x % 2 == 0) x+=3; else x+=2; } System.out.println("x = " + x); } }

User Connorhd
by
5.4k points

1 Answer

5 votes

Answer:

It will print x = 11

Step-by-step explanation:

x starts with a value of 2 and goes inside the while loop. The loop will continue until x<10. Inside the loop, If x is even number, 3 will be added to x. Otherwise, 2 will be added. When the loop is done, x will be printed.

First iteration:

x = 2, x becomes x + 3 = 5, Is x smaller than 10? YES

Second iteration:

x = 5, x becomes x + 2 = 7, Is x smaller than 10? YES

Third iteration:

x = 7, x becomes x + 2 = 9, Is x smaller than 10? YES

Forth iteration:

x = 9, x becomes x + 2 = 11, Is x smaller than 10? NO

Stop the loop and print x

User Kritzefitz
by
5.3k points