VERY basic question - if loop
Hello everyone,
I started studying Java today and couldn't understand why this code would give the result bellow it:
INPUT:
Code Java:
public class SimpleLoop {
public static void main(String[] args) {
for(int i=0; i<20; i++) {
for(int j=0; j<i; j++) {
System.out.print(" ");
}
System.out.println("*");
}
}
}
OUTPUT:
*
(1 space) *
(2 spaces) *
(3 spaces) *
(4) *
(5) *
and so on...
there is no instruction for how many (' ') (space) to type before the ('*').
I mean, shouldn't it be like this?
*
(1 space) *
(1 space)*
(1 space) *
(1 space)*
and so on...?
In the first run - no space, but from the second - there is space, but only one.... isn't it?
Thanks for helping...
Kobi.
Re: VERY basic question - if loop
Take a look at the nested loop. It's printing out the number of spaces depending on which line you're on.
So the first line has no spaces, the second line has 1 space, and so on and so forth.
Re: VERY basic question - if loop
But isn't the 'j' symbolize the number of repetitions and not the number of the spaces...?
if i=4 and j=3 isn't it should print only one space as i ordered it?
Re: VERY basic question - if loop
ohhhhhh!!!!
i got it now...!
the nested "if" keep running before it goes another round of the "main if"...
thanks! :)