The execution of a while loop can be pictured with time on a horizontal axis and code vertical. You may enter a subset of legal java code below.
x = y = 3or its ilk)
Java Code | → Time → |
---|---|
int x = 7; int collatz = 0; while (x > 1) { if ((x % 2) == 0) x /= 2; else x = ((x * 3) + 1) / 2; collatz += 1; System.out.println(x); } System.out.println(); System.out.println(collatz); |
|
Mouse-over times to see how things change. The bottom row of the times is something like a slider, allowing you to more easily run
the program.
Below is the memory and output as it looks after completing the highlighted part of the code.
type | name | value |
---|---|---|
int | x | 7 |
Program Output: