*
The moose likes Beginning Java and the fly likes Can someone help me to understand the break statment Specification ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can someone help me to understand the break statment Specification ?" Watch "Can someone help me to understand the break statment Specification ?" New topic
Author

Can someone help me to understand the break statment Specification ?

Ranji Sura
Ranch Hand

Joined: Oct 28, 2012
Posts: 84
Hi..

Can some one help me to clear this out ?

http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.15

A break statement with no label attempts to transfer control to the innermost enclosing switch, while, do, or for statement of the immediately enclosing method or initializer; this statement, which is called the break target, then immediately completes normally.


What does it mean by " immediately enclosing method or initializer" ?
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 219

It breaks the iteration of a loop and transfers control to the innermost method i.e the method in which the loop was defined..

I hope it helps.


OCPJP
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38441
    
  23
Ishan Pandya wrote:It breaks the iteration of a loop and transfers control to the innermost method i.e the method in which the loop was defined..

I hope it helps.
Not at all. It transfers control to the end of the innermost loop (or switch) which contains the break; and that loop completes normally (i.e. does not throw an Exception or return anything).
The immediately enclosing method is, as you said, the method in which the loop (or switch) is defined.
Ranji Sura
Ranch Hand

Joined: Oct 28, 2012
Posts: 84
Campbell Ritchie wrote: It transfers control to the end of the innermost loop (or switch) which contains the break; and that loop completes normally (i.e. does not throw an Exception or return anything).
The immediately enclosing method is, as you said, the method in which the loop (or switch) is defined.


If you don't mind , I would you like to ask you.. how are you really sure about this ... ?
It transfers control to the end of the innermost loop (or switch ).

Has it been specified... ?

Because please look at this,



One more question... ?
this statement, which is called the break target,

What does it mean by this statement... ? for.. do.. while.. switch.. statements or break statement ?
Ranji Sura
Ranch Hand

Joined: Oct 28, 2012
Posts: 84
Furthermore if you look at the specification closely...

A break statement with no label attempts to transfer control to the innermost enclosing switch, while, do, or for statement


It says "It attempts to transfer control to the innermost enclosing switch, while, do, or for statement" rather than "It transfers control to the end of the innermost loop (or switch) which contains the break"

What do you think sir... ?

Look at this please ... specification for continue statement , First part of declaration is similar to break statement... Isn't it ?

A continue statement with no label attempts to transfer control to the innermost enclosing while, do, or for statement of the immediately enclosing method, constructor, or initializer;this statement, which is called the continue target, then immediately ends the current iteration and begins a new one.


Do you still really think it transfer control to the end of innermost loop (or switch) sir ?
Ranji Sura
Ranch Hand

Joined: Oct 28, 2012
Posts: 84
How I missed this...

First line,

"A break statement transfers control out of an enclosing statement. "


Sorry... Sorry... Sorry... Extremely Sorry...
I really misunderstood it..

And thank you for the support... I have got it correct now...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38441
    
  23
The JLS is not easy to understand, but there it simply says what happens. It is a lot easier to understand when you consider it along with the continue quote you posted later. In one case it completes normally, in the other it starts the next iteration.
Completion in this context means the iterations are regarded as entirely finished.
Ranji Sura
Ranch Hand

Joined: Oct 28, 2012
Posts: 84
Campbell Ritchie wrote:The JLS is not easy to understand, but there it simply says what happens. It is a lot easier to understand when you consider it along with the continue quote you posted later. In one case it completes normally, in the other it starts the next iteration.
Completion in this context means the iterations are regarded as entirely finished.


First of all it is extremely thank you for answering me...

The JLS is not easy to understand. I 100% accept it.

and It may be also.. JLS has not specified some of the specification clearly.. If you don't agree.. Check this out please... I found this today...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.12.1
If execution of the Statement completes abruptly because of a continue with no label, then the entire while statement is executed again.


and please don't mind to check this code..


According to the specification, this loop should not stop, if it just executes entire the while statement..
But actually, what happens before the execution is, it evaluates the expression and then it decides whether it should execute entire while statement again depending on the value..

It has been specified clearly in both do...while and for loops


If execution of the Statement completes abruptly because of a continue with no label, then the Expression is evaluated. Then there is a choice based on the resulting value:

If the value is true, then the entire do statement is executed again.

If the value is false, no further action is taken and the do statement completes normally.



But, in while loop , it hasn't specified as clearly as someone can understand easily..
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3011
    
  10
Ranjith Suranga wrote:According to the specification, this loop should not stop, if it just executes entire the while statement..
But actually, what happens before the execution is, it evaluates the expression and then it decides whether it should execute entire while statement again depending on the value..

But that's what a while loop does. When you execute it, it first evaluates the expression, and decides whether to execute the rest of the while statement depending on that value. When the spec says "executes the entire while statement", that execution should start with an evaluation of the expression, based on current values of the variables.
Ranji Sura
Ranch Hand

Joined: Oct 28, 2012
Posts: 84
Mike Simmons wrote:
But that's what a while loop does. When you execute it, it first evaluates the expression, and decides whether to execute the rest of the while statement depending on that value. When the spec says "executes the entire while statement", that execution should start with an evaluation of the expression, based on current values of the variables.


Yes, you can argue like that.. But if you don't mind, i would like to show you this point..

Check this...
http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.13.1

If execution of the Statement completes abruptly because of a continue with no label, then the Expression is evaluated. Then there is a choice based on the resulting value:

If the value is true, then the entire do statement is executed again.

If the value is false, no further action is taken and the do statement completes normally.


if you says, when the entire do statement is executed, this is, what happens.. then why JLS has specified this in "14.13.1. Abrupt Completion of do Statement ". There is no need of it, because it already has specified how the do statement works, in here. 14.13. The do Statement ()

If execution of the Statement completes normally, then the Expression is evaluated. If the result is of type Boolean, it is subject to unboxing conversion (ยง5.1.8).

If evaluation of the Expression or the subsequent unboxing conversion (if any) completes abruptly for some reason, the do statement completes abruptly for the same reason.

Otherwise, there is a choice based on the resulting value:

If the value is true, then the entire do statement is executed again.

If the value is false, no further action is taken and the do statement completes normally.


According to your point sir, it doesn't need to duplicate such a thing in Abrupt Completion of do Statement, but it has been specified...
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7684
    
  19

Ranjith Suranga wrote:if you says, when the entire do statement is executed, this is, what happens.. then why JLS has specified this in "14.13.1. Abrupt Completion of do Statement ". There is no need of it, because it already has specified how the do statement works, in here.

I think you're missing the point.

while and do..while are NOT the same. One evaluates 'expression' before it executes its contents, the other one after.
Furthermore, even though they can contain several statements inside their braces, each one (including their contents) is also regarded as a single statement. Therefore, there could be all sorts of reasons that the loop ends abruptly that are quite separate from evaluation of the expression.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ranji Sura
Ranch Hand

Joined: Oct 28, 2012
Posts: 84
while and do..while are NOT the same. One evaluates 'expression' before it executes its contents, the other one after.

Yes, it is, but it is not what I am trying to suggest...

and I think this is pointless thing to talk..

Before quit.. I would like to ask, dear "Winston Gutkowski"

http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.15
A break statement with no label attempts to transfer control to the innermost enclosing switch, while, do, or for statement of the immediately enclosing method or initializer; this statement, which is called the break target, then immediately completes normally.


the innermost enclosing switch, while, do, or for statement of the immediately enclosing method or initializer
What does it mean... like to know what do you think above line... ?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7684
    
  19

Ranjith Suranga wrote:What does it mean... like to know what do you think above line... ?

As simply as I can describe it:
HIH

Winston
Ranji Sura
Ranch Hand

Joined: Oct 28, 2012
Posts: 84
Dear sir, then I would like to ask you,,, What is the innermost enclosing for statement of the immediately enclosing method here... ?

First for loop or second for loop ?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7684
    
  19

Ranjith Suranga wrote:First for loop or second for loop ?

What do you think? I think the example is fairly explicit.

Winston
Ranji Sura
Ranch Hand

Joined: Oct 28, 2012
Posts: 84
OK. This is what I want to know...

Let's put your for loops in a method ... OK.. like this...

and please keep in mind this definition for the innermost

innermost : - Farthest in

Now, can you guess.. ?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7684
    
  19

Ranjith Suranga wrote:and please keep in mind this definition for the innermost
innermost : - Farthest in

Yes, but the specification says 'innermost enclosing'. So: what's the innermost enclosing loop?

Winston
Ranji Sura
Ranch Hand

Joined: Oct 28, 2012
Posts: 84
Winston Gutkowski wrote:
Yes, but the specification says 'innermost enclosing'. So: what's the innermost enclosing loop?


OK. Now I have a question.. ? (By the way, You makes me really learning..)

Let's take your example again..




Which loop is the innermost enclosing loop here ? Line number 6 or Line number 8 ?
Really looking forward your reply now..
And furthermore like to know what is break target here ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can someone help me to understand the break statment Specification ?