# for loop -anyone can explain me how it works

• 07-21-2012, 08:32 AM
zanetti77
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?
• 07-21-2012, 08:50 AM
pbrockway2
Re: for loop -anyone can explain me how it works
Quote:

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.
• 07-21-2012, 09:05 AM
zanetti77
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
• 07-21-2012, 09:24 AM
Rameshwar Soni
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.
• 07-21-2012, 09:31 AM
Rameshwar Soni
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.
• 07-21-2012, 09:39 AM
pbrockway2
Re: for loop -anyone can explain me how it works
Here's your code with a couple of small changes:

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.

Quote:

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.
• 07-21-2012, 02:37 PM
edge241
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.