why does 'break' traverse one more time
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ]
"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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
so can I say -->"without labels"..<--
break and continue are the 'same'
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks all
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
"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...
"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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
"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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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

-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thx..
I'm working on this last bit of info..
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
posting that qstn.
Sashi
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I am able to see the idea behind...
and 'ooops..' for getting your name wrong in the last reply.
Thx.
| Sasparilla and fresh horses for all my men! You will see to it, won't you tiny ad? Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |











