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

1. Member
Join Date
Jul 2012
Posts
2
Rep Power
0

## 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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12

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

3. Member
Join Date
Jul 2012
Posts
2
Rep Power
0

## 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. Member
Join Date
Sep 2011
Location
Mumbai, India
Posts
35
Rep Power
0

## 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. Member
Join Date
Sep 2011
Location
Mumbai, India
Posts
35
Rep Power
0

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

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

## 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. Member
Join Date
Jul 2012
Posts
6
Rep Power
0

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

#### Posting Permissions

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