0

hello guys, i am very new here.

public class quiz{
	public static void main (String args[]){
		for (byte i = 1; i <= 5; i++){
			for (byte j = 1; j <= i; j++){
				System.out.println(i);
			}
		System.out.println();
		}
	}
}

I can't simply explain that code. and it gives me an output of this:

1

2
2

3
3
3

4
4
4
4

5
5
5
5
5

why does it print two '2s' when variable i will be two??
kindly explain.
advance thank you :))

by the way, I am just a student, I want to read your posts here, and some tutorials, they are just interesting :)

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by JamesCherrill
0

for (byte j = 1; j <= i; j++){ ...
If i is 2, this inner loop will be executed twice (j=1 and j=2)
If i is 3, this inner loop will be executed three times(j=1 and j=2 and j=3)
etc

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.