Results 1 to 3 of 3
  1. #1
    cs8scv is offline Member
    Join Date
    May 2012
    Posts
    1
    Rep Power
    0

    Unhappy Nest for loop problem

    I don't understand the output....can someone please explain?

    for(int i=0; i<10; i++) {
    for(int j=0; j<100; j++) {
    if(j==41) continue;
    System.out.println(j);

    Output:
    # of lines printed = 990
    Largest number printed = 99


    Can someone please break it down for me?

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

    Default Re: Nest for loop problem

    Consider the inner loop: all values of j (0,1,2, ...,99) are printed except value 41; that makes 99 different values and the highest number printed is 99. That entire loop itself is executed 10 time because of the outer loop, so in total 10*99 == 990 lines are printed.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default Re: Nest for loop problem

    jos's explained very clear. btw, you have to, first, understand the code... it's very simple... :)

Similar Threads

  1. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  2. For Loop Problem
    By saqib15 in forum New To Java
    Replies: 6
    Last Post: 02-20-2010, 12:59 AM
  3. Nest Try
    By arefeh in forum New To Java
    Replies: 20
    Last Post: 02-05-2010, 05:02 PM
  4. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM
  5. While-loop problem
    By jimmy-lin in forum New To Java
    Replies: 6
    Last Post: 11-02-2009, 03:10 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
  •