Reputation: 25
I have the solution to the code, but I don't understand how it works. Can somebody explain?
for (int i = 1; i <= 3; i++)
for (int j = i; j <= 4; j++)
System.out.print(j + " ");
The output for the code is 1 2 3 4 2 3 4 3 4
Upvotes: 0
Views: 87
Reputation: 25
At the beginning I is at 1 so j goes to 1 to 4 right
Than I pass to 2 so j goes to 2 to 4 cuz I=j remember
Than I pass to 3 so j goes to 3 to 4 and voila
You get: 1234 234 34...
Upvotes: 0
Reputation: 191701
j
starts at i=1
and goes to 4.
i
increments.
j
starts at i=2
and goes to 4.
Rinse, repeat...
Maybe this visual helps delineate the loops
1 2 3 4 | 2 3 4 | 3 4
Upvotes: 1