This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why does 'break' traverse one more time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why does Watch "why does New topic
Author

why does 'break' traverse one more time

Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131


why is the output i=1,j=0
I would understand, at the begin i=0 and then j=0, at this moment it satisfies if(i==j) and the it should 'break'...i.e go back to "for(int i=0;i<2;i++) and stop and do nothing....but it traverses one more time i=1,j=0 , prints it out and then stops.


([C0DE][/C0DE] tags added, reformatted, missing if(i==j) added)
[ September 28, 2004: Message edited by: Barry Gaunt ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I gather there's supposed to be an "if(i==j)" before the "break"...

If I'm correct, then there are two things going on here: 1) behavior of the break statement; and 2) behavior of the variable j.

"break" exits the current loop -- which, in this case, is the inner loop. It does not affect the outer loop.

We start the first iteration of the outer loop with i=0. For this outer-loop iteration, we start the first iteration of the inner loop with j=0. Because i==j, we break out of the inner loop and execute the println, which outputs i = 0 j = 0.

Now we proceed with the next iteration of the outer loop, so i=1. And for this outer-loop iteration, we start fresh with another first iteration of the inner loop, so j=0. This time, i==j is false, so we don't break. Instead, j increments to j=1. Now, i==j is true, so we break out of the inner loop and execute the println.

But here, something odd happens. Our output is i = 1 j = 0. Why is j zero?

Note that our println is not inside the inner loop -- it's in the outer loop. Now look at the scope of the variable j in the inner loop. Is this the same j that we've declared above as static? Or is this a local j shadowing the class j?

Consider the declaration: What would happen if we used "for(j = 0..." rather than "for(int j = 0..."?

At this point, we increment i again, and the outer loop stops.
[ September 26, 2004: Message edited by: marc weber ]

"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
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Yes, "break stops execution of the loop," but only the current loop. Execution of whatever code is outside of that loop continues. So if that loop is nested within other loops, then execution of those outside loops continues.

This example has only 2 levels, so using break within the inner loop is the same as labeling the outer loop (with something like "outerLoop:") and then saying "continue outerLoop;". Either way, you're breaking out of the inner loop, and then continuing with the outer code. By using break, you don't need to label the outer loop.

The real advantage of labels is when you need to be able to break out of multiple levels and/or continue with a particular outer iteration. As fred said above, if you're within an inner loop and you want to break out of the entire structure of nested loops, then you can put a label on the outermost loop, and use break with that label.
[ September 27, 2004: Message edited by: marc weber ]
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
Okay....
so can I say -->"without labels"..<--
break and continue are the 'same'
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
and I also think I got it now

Thanks all
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Well, let's make sure we're clear...

"break" will stop execution of the current loop, then continue with whatever code is immediately outside of that loop (which might mean continuing with an outer loop). In other words, "break" throws you back out to the next level.

"continue" will stop execution of the current iteration of the current loop, then continue with the next iteration of the same loop. In other words, "continue" does not throw you out to the next level, but instead moves on to the next iteration at the same level.

So, in a nested loop situation, breaking out of an inner loop can be the same as continuing with an outer loop.

Try compiling and running the following code...

marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Here's the other big idea in this code...

Notice that the static variable i increments, but the static j remains zero. This is because the i used in the loop is, in fact, the static i declared above. However, the j used in the loop is a local variable "shadowing" the static j declared above. The difference is in the "for" statements. In the outer loop, i is simply set to zero. But in the inner loop, a new variable j is declared with the type "int" and set to zero.
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
Marc
you are right, I missed the if(i==j) part...

okay...
when it first 'broke' with '0==0'

it then abandons the inner loop , falls to the outer with i=0 and j=0 and meets the "print ln" and prints "i=0 and j=0" [ so far so good ]

in one of your last mails you mentioned that 'without labels..."break" stops execution of the loop'..

shan't I understand here that execution of both inner and outer is now stopped as of this moment. ..?

Because now if the outer one is continued anew[ thus making i=1]...then what is the purpose and difference between the 'continue' st. which also does the same ?

I know I am wrong all the way because the truth is out there glaring at me on the dos box saying
C:\>java Hi
i = 0 j = 0
i = 1 j = 0

so where am I making this gross mistake

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

shan't I understand here that execution of both inner and outer is now stopped as of this moment. ..?


a break statement only stops execution of ONE loop - the innermost one the statement is in. In your code, it will stop the execution of the (j) loop. the outer loop (with the i variable) is COMEPLETLY UNNAFFECTED by the break statement.

you can get around this by using a labeled loop. i've never used one, but i think the syntax is something like


now, the i-loop has a label "Outer". the break statement says "break out of the loop that is labeled "Outer".


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
Marcus and all

Thx..

I'm working on this last bit of info..
sashi balu
Greenhorn

Joined: Jul 23, 2004
Posts: 16
Thanks a lot Marc for that beautiful explanation, and thanks to Netty for
posting that qstn.

Sashi
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
Thank you Mark...!

I am able to see the idea behind...

and 'ooops..' for getting your name wrong in the last reply.

Thx.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: why does 'break' traverse one more time
 
Similar Threads
Flow Control
switch statement
Label???
Some problem
finalize help plzzz