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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.