Results 1 to 7 of 7
- 07-21-2012, 08:32 AM #1
Member
- Join Date
- Jul 2012
- Posts
- 2
- Rep Power
- 0
- 07-21-2012, 08:50 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: for loop -anyone can explain me how it works
Run it and see.What does the following program segment print?
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 #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
- 07-21-2012, 09:24 AM #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.
- 07-21-2012, 09:31 AM #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.
- 07-21-2012, 09:39 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: for loop -anyone can explain me how it works
Here's your code with a couple of small changes:
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.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 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.
Are you sure about that? I thought it would be 00,01,10,12 with each one on its own line.I runned it and got the following 0,1,10,11
- 07-21-2012, 02:37 PM #7
Member
- Join Date
- Jul 2012
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
can someone explain what this exactly does?
By liluma in forum New To JavaReplies: 4Last Post: 08-21-2011, 07:58 PM -
Please Explain me how i got this output.. I am new to java .. so please Explain me...
By vicky82 in forum New To JavaReplies: 2Last Post: 12-13-2010, 01:34 PM -
Please Explain me how i got this output.. I am new to java .. so please Explain me...
By vicky82 in forum New To JavaReplies: 3Last Post: 12-13-2010, 07:22 AM -
Please explain how this bit of code works.
By Allspark in forum New To JavaReplies: 4Last Post: 09-03-2010, 03:56 AM -
Tell me how this loop works.
By hydride in forum New To JavaReplies: 6Last Post: 05-13-2010, 04:50 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks