• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

why does 'break' traverse one more time

 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay....
so can I say -->"without labels"..<--
break and continue are the 'same'
 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and I also think I got it now

Thanks all
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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".
 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marcus and all

Thx..

I'm working on this last bit of info..
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Marc for that beautiful explanation, and thanks to Netty for
posting that qstn.

Sashi
 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Mark...!

I am able to see the idea behind...

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

Thx.
 
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic