Results 1 to 6 of 6

Thread: for loop output

  1. #1
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default for loop output

    hi guys we are learning loops this week at uni and a tute was to get the output
    1 2 3.... 10
    11 12 13.... 20
    ........
    91 92 93...... 100

    i got this working but not sure if i did it right... Did i miss something here or is this how it would be done?

    Java Code:
    int i;
    int j;
    
    for( i=1; i <= 10; ){
       for( j=1; j <= 10; j++){
          System.out.print(i + " ");
           i++
       }
       System.out.println();
    }

    might be typos... My net is down so typing this on mobile

    thanks guys

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by maknib View Post
    hi guys we are learning loops this week at uni and a tute was to get the output
    1 2 3.... 10
    11 12 13.... 20
    ........
    91 92 93...... 100

    i got this working but not sure if i did it right... Did i miss something here or is this how it would be done?

    Java Code:
    int i;
    int j;
    
    for( i=1; i <= 10; ){
       for( j=1; j <= 10; j++){
          System.out.print(i + " ");
           i++
       }
       System.out.println();
    }

    might be typos... My net is down so typing this on mobile

    thanks guys
    The code is fine (ignoring a few little typos).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    The code is fine (ignoring a few little typos).
    Ahem, Jos!

    The outer loop counter is being incremented inside the inner loop (ignoring the missing semicolon) -- and take a closer look at what's being printed .

    db

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Ahem, Jos!

    The outer loop counter is being incremented inside the inner loop (ignoring the missing semicolon) -- and take a closer look at what's being printed .

    db
    It must've been 'wishful reading' on my part, sorry about that. I must've mentally constructed the correct code given the pieces that were shown, how silly of me ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default

    Hint: Only one loop is required, and the modulus operator will come in handy.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

Similar Threads

  1. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 06:46 PM
  2. Replies: 0
    Last Post: 09-30-2010, 09:16 PM
  3. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 12:44 AM
  4. Replies: 3
    Last Post: 02-16-2009, 10:20 PM
  5. how to print output on same line in 'while loop'?
    By acidblue in forum New To Java
    Replies: 5
    Last Post: 12-13-2007, 03:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •