my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem in break keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem in break keyword" Watch "Problem in break keyword" New topic
Author

Problem in break keyword

Randika Isuru
Ranch Hand

Joined: Sep 15, 2013
Posts: 38

hi all,
first of all i need to tell you the problem i' am going to tell you it is ask many time in java ranch moose forum. but i think my problem is different than compare with other's problems
may b my problem is mad question but i need to clear it..
this is my code




but my problem happens in break keyword. What understood about break keyword when code run it is encounter the break keyword it is jump in the innermost loop and continue the code.
What i need to understand is that switch case is some kind of looping stuff or not?
because i think it is not kind of looping thing.
then i think above code runtime it's encounter the break keyword then it's need to jump over the innermost loop and print the result. but above code again looping the for loop.
i know what i 'am understood in this case, that was still wrong. because i run this code and get result of book answer(K&B).
now i need know what is the correct theory about this situation

guys please help me

thanks
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1036
    
  15
Randika Isuru wrote:
but my problem happens in break keyword. What understood about break keyword when code run it is encounter the break keyword it is jump in the innermost loop and continue the code.
What i need to understand is that switch case is some kind of looping stuff or not?
because i think it is not kind of looping thing.
then i think above code runtime it's encounter the break keyword then it's need to jump over the innermost loop and print the result. but above code again looping the for loop.


An unlabeled break statement causes the control to branch out of the innermost switch, for, while, or do-while statement. In your case it is the switch statement. Remember the control is still within the for loop after it has branched out of the switch statement.

You get a certain output for the code you have. What do you think will happen if you change the case 10 slightly as follows.


Hint - Try it.

A switch case is not a loop, it is a control flow statement. Consider reading this.

Chan.
Randika Isuru
Ranch Hand

Joined: Sep 15, 2013
Posts: 38

thank q very much Chan Ag for your responding.
But still i' am little bit unclear about my problem.
so i realize something in your replay. in this case break keyword is not belong to for loop,.. it is belong to switch statement am i correct?
and i try your hint also and also thanks for it
because i understand something in that hint.

so regards your hint what can anybody say about this code



In this case break keyword jump over the for loop and output the result.
what is my problem is why this break keyword do not doing the same thing for switch statement because switch and if both are decision-making statements. i think your referring article say that too.

Thanks
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1036
    
  15
so i realize something in your replay. in this case break keyword is not belong to for loop,.. it is belong to switch statement am i correct?

Yes that is right.

and i try your hint also and also thanks for it
because i understand something in that hint.

So as you saw in the hint version of the code, when x is 9 case 9 is satisfied. Control branches to that case. and then falls through in all the other cases cause there is no break statement in case 9 and any case following case 9 ( the one in case 10 is not applicable cause x is not 10 ). So your String s has 9, 10, d and 13 at this point. When x becomes 10 in the next iteration of the for loop, the matching case is case 10. Although the innermost statement is the if statement, the control branches out of the inner most switch/for/do while/while statement. In your case it is the switch statement. so the control does not fall into the rest of the cases. It is the innermost switch/for/do while/while statements always that an unlabeled break branches out from.
Next y is 2 and x is 11. The first case that matches is the default case and after that control falls through to the next case statement cause there is no break statement.

so regards your hint what can anybody say about this code



In this case break keyword jump over the for loop and output the result.
what is my problem is why this break keyword do not doing the same thing for switch statement because switch and if both are decision-making statements. i think your referring article say that too.

Thanks


Of the four types of control flow statements ( do-while/for/while/switch ), which one is the innermost control flow statement for your break statement? It's the for loop. So the control branches out of the for loop in its first iteration and prints 9.
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1036
    
  15
Randika Isuru wrote:
what is my problem is why this break keyword do not doing the same thing for switch statement because switch and if both are decision-making statements. i think your referring article say that too.

Thanks


And that same link has sub-links to all the control flow statements, one of which is the switch statement. That page has some very relevant examples.

timo corn
Ranch Hand

Joined: Oct 26, 2013
Posts: 68

The question in the book of chapter 5 has nested control structures/flows: an outer for and an inner switch. The "break" only is valid in the inner flow (switch), as Chan explained well.


hth
Randika Isuru
Ranch Hand

Joined: Sep 15, 2013
Posts: 38

Thank you very very much Chan... you explain that very nicely..
now i understood all this scenario because of you,,

Thank again...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in break keyword