This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Another question regarding the Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Another question regarding the "for" loop" Watch "Another question regarding the "for" loop" New topic
Author

Another question regarding the "for" loop

Derek Harper
Greenhorn

Joined: Aug 02, 2006
Posts: 25
Greetings,

I have another question regarding a "for" loop. Take a look at the following program:

class ForLoop{
public static void main(String[]args){

int i;
OuterLoop: for(i = 3; i > 0; i--){
InnerLoop: for(int j = 0; j < 4; j++){
System.out.println("i = " + i + " and j = " + j);
if(i == j)
break InnerLoop;
}
}
}
}


The output is:

i = 3 and j = 0
i = 3 and j = 1
i = 3 and j = 2
i = 3 and j = 3
i = 2 and j = 0
i = 2 and j = 1
i = 2 and j = 2
i = 1 and j = 0
i = 1 and j = 1

I'm trying to make sure that i'm understanding why i get the output i did. My understanding is when starting out, i = 3, it goes through the OuterLoop, the condition is checked (true), and it is then passed into the value of 'i':

i = 3

Next, in the InnerLoop, j starts out as '0' and goes through the loop, the condition is checked(true) and is passed into the value of 'j':

j = 0

Looking at the if statement, the lnnerLoop continues through the loop, increments according to the rule until 'j' becomes '3':

i = 3 and j = 1
i = 3 and j = 2
i = 3 and j = 3

(i == j) <-------- code breaks when i = 3 and j = 3

The InnerLoop stops ("break InnerLoop") and the OuterLoop starts over as i decrements (i--) and becomes 2, goes through the OuterLoop again, the condition is checked (true) and passed into 'i' again:

i = 2

Then the InnerLoop continues it's loop as j starts out again as 0, increments according to the rule until 'j' becomes '2':


i = 2 and j = 0
i = 2 and j = 1
i = 2 and j = 2

(i == j) <-------- code breaks when i = 2 and j = 2

.....and so on and so forth.


Am i correct in my assessment? Thanks in advance.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Derek Harper:
... Am i correct in my assessment? ...

Yes, good job!


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Now, having learnt how the "break label" statement works, you should forget all about it! It's not nice programming style at all.
[ April 04, 2008: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
Originally posted by Peter Chase:
Now, having learnt how the "break label" statement works, you should forget all about it! It's not nice programming style at all.

[ April 04, 2008: Message edited by: Peter Chase ]
I'm trying my hardest to forget about break without a label, too

Derek Harper
Greenhorn

Joined: Aug 02, 2006
Posts: 25
Thanks Marc.

Peter,

I'm curious about your statement. Why is it not good programming style? Please enlighten me.


Thanks in advance.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11164
    
  16

some people consider the 'break label' to be nothing more than a camouflaged 'goto' statement. that is arguable, in my opinion. there is not a STRONG argument, but i believe one could be made.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Another question regarding the "for" loop
 
Similar Threads
Increment operators in a "for" loop
Dans mock test on control flow
Code using Loops
JQ+ Question ID :952739443290
Flow control!