Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By pbrockway2
  • 1 Post By Rameshwar Soni

Thread: for loop -anyone can explain me how it works

  1. #1
    zanetti77 is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default for loop -anyone can explain me how it works

    I am doing some exam past papers and have the following question

    What does the following program segment print?
    for(f=0; f<2;++f)
    for(g=0;g<2;++g)
    System.out.println(f+" "+g+"");

    Can someone explain me what it does pls?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: for loop -anyone can explain me how it works

    What does the following program segment print?
    Run it and see.

    But first make some sort of prediction. That way if it does something different you can post both your ideas and the (full, compilable) code you were using.
    Rameshwar Soni likes this.

  3. #3
    zanetti77 is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default Re: for loop -anyone can explain me how it works

    I runned it and got the following 0,1,10,11

    the full code is


    class ForFor{

    public static void main (String args[]) {


    for (int f=0;f<2;++f)
    for(int g=0;g<2;++g){
    System.out.println(f+ "" + g +" ");
    }
    }
    }

    i can understand how a for works but cant understand when there are two for. If there was one one for i would get 0 and 1

  4. #4
    Join Date
    Sep 2011
    Location
    Mumbai, India
    Posts
    35
    Rep Power
    0

    Default Re: for loop -anyone can explain me how it works

    I think taking a pen and paper and solving this will help you and it wouldn't be that hard since you already know how a single for-loop works.

  5. #5
    Join Date
    Sep 2011
    Location
    Mumbai, India
    Posts
    35
    Rep Power
    0

    Default Re: for loop -anyone can explain me how it works

    Or let me give a try.........

    1) First check if outer for-loop is correct (i mean all that condition etc), if yes then go the statements under your outer for-loop

    2) Now the first statement under your outer for-loop is another loop, so again check all the condition of this inner for-loop, if conditions are correct of this inner-loop
    then execute all statements under it. Now increment the counter variable of this inner for-loop and again check the condition, if correct then again execute all
    statements under inner for-loop. Again increment counter of inner for loop and check condition and keep doing this until and unless the condition of inner for-loop is false

    3) Once the condition of inner for loop is false, go and increment counter of outer for loop and check condition of this outer for loop, if true then repeat step 2 if false end of all loops.
    pbrockway2 likes this.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: for loop -anyone can explain me how it works

    Here's your code with a couple of small changes:

    Java Code:
    class ForFor {
        public static void main (String args[]) {
            for (int f=0;f<2;++f) {
                for(int g=0;g<2;++g) {
                    System.out.println(f+ "" + g +" ");
                }
            }
        }
    }
    I have used "code" tags so that the code appears here properly formatted. You put [code] at the start of the code and [/code] at the end when you write your post so the code ends up this way.

    I have added another pair of braces around the outside for loop. It is always a good idea to use braces and the indentation makes it clear that the loops are "nested" one inside the other.

    I runned it and got the following 0,1,10,11
    Are you sure about that? I thought it would be 00,01,10,12 with each one on its own line.

  7. #7
    edge241 is offline Member
    Join Date
    Jul 2012
    Posts
    6
    Rep Power
    0

    Default Re: for loop -anyone can explain me how it works

    I think when he said 0,1,10,11 I think he meant 00, just used the number 0 to represent 00, which you should never do if you have a program like this. But other than that he was right, it'd be 00,01,10,11.

Similar Threads

  1. can someone explain what this exactly does?
    By liluma in forum New To Java
    Replies: 4
    Last Post: 08-21-2011, 08:58 PM
  2. Replies: 2
    Last Post: 12-13-2010, 02:34 PM
  3. Replies: 3
    Last Post: 12-13-2010, 08:22 AM
  4. Please explain how this bit of code works.
    By Allspark in forum New To Java
    Replies: 4
    Last Post: 09-03-2010, 04:56 AM
  5. Tell me how this loop works.
    By hydride in forum New To Java
    Replies: 6
    Last Post: 05-13-2010, 05:50 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
  •