Break statement
Can someone please explain break statements to me, as far as i understand a break statement leaves the loop before it has ended.
But i run this code and expect to get
"Zero"
"One"
But I get
"Zero"
"One"
"One"
"Unknown number"
for (int i = 0; i < 3; i++)
{
switch(i)
{
case 0:
{
System.out.println("zero ");
}
case 1:
{
System.out.println("one");
break;
}
default:
{
System.out.println("unknown number");
}
}
}
Help please, can someone explain this to me in steps as i have confused myself badly
bangor_boy
Junior Poster in Training
67 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
break inside a switch breaks out of the switch - ie stops any later cases being executed. So in this case is does NOT break out of the loop, just the switch.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
Read about the break statement and statement labels.
Because it is a form of GOTO, a lot of people frown on its use.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
break inside a switch breaks out of the switch - ie stops any later cases being executed. So in this case is does NOT break out of the loop, just the switch.
More generally, a break statement will break out of the containing block only, not out of any higher level containing blocks.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
Why does "one" appear twice?
This is an exam practise question and I need to understand why this happens
bangor_boy
Junior Poster in Training
67 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
and if not, make for yourself a list of when each element will be printed.
check the locations of the break statements and, this is propably the issue they want you to figure out, explain the consequences of the break statements, those who are there, and those that were omitted.
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
Thanks for the help folks
bangor_boy
Junior Poster in Training
67 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
Yes. After the case 0: is executed, execution will FALL THRU to the code for case 1:
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
there is no break in case 0, therefore the iteration will continue to iterate to print out the case 1 and since it has no break it print out the case 0 and case 1 output, then the case 0 will now fall to case 1 that will print out the output of case 1 only, the only question is that why did you use a switch statement in this code although it is easier to get those kind of result in if-else statement?
decade
Junior Poster in Training
62 posts since Jun 2011
Reputation Points: 12
Solved Threads: 14
@decade:
it might be a school project on which the teacher instructed them to use a switch, so they could get familiar with the working of it.
yes, in this case working with a switch does not give (that much) advantage over working with nested if-statements (if you were talking about not-nexted statements, well ... checking one expression, or checking all of them, makes the code just that little more efficient, so in that case, the switch is of help) but can you see this code when we're not talking about four or five possible values, but eighty? would you like to start reading that code if it were constructed with nested if-statements, let alone would you want to be the one who has to maintain that code?
IMHO, even with a reasonable good indentation, when it concerns a lot of possibilities, a decent switch-statement is a lot easier to read and maintain than a bunch of nested if's. if you only have a few possibilities, it may look redundant, but you should look as to what will happen with it after you hand it over. is it possible that the range of valid expressions will increase? if so, putting it in a switch makes it all the more easier for the next guy to work on the code.
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433