Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: VERY basic question - if loop

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:
    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.
    Last edited by helloworld922; October 12th, 2011 at 06:26 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!

Similar Threads

  1. Basic Java question
    By fred2028 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 13th, 2011, 03:54 PM
  2. Basic java question
    By erosgol in forum Java Theory & Questions
    Replies: 5
    Last Post: September 2nd, 2011, 05:24 PM
  3. Basic treemap loop
    By Trunk Monkeey in forum Java Theory & Questions
    Replies: 3
    Last Post: May 1st, 2011, 10:11 AM
  4. please answer some basic question
    By togaurav in forum Java Theory & Questions
    Replies: 5
    Last Post: April 16th, 2011, 07:58 AM
  5. Basic loop issue
    By Nismoz3255 in forum Loops & Control Statements
    Replies: 3
    Last Post: February 23rd, 2011, 05:10 PM