• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Problem in break keyword

 
Ranch Hand
Posts: 38
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 1090
14
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 38
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1090
14
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Ranch Hand
Posts: 68
MyEclipse IDE PHP Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Randika Isuru
Ranch Hand
Posts: 38
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very very much Chan... you explain that very nicely..
now i understood all this scenario because of you,,

Thank again...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic