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

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.

Read about the break statement and statement labels.
Because it is a form of GOTO, a lot of people frown on its use.

just put a break statement after the 7th line and you will get your desired output.

Thanks for explaining with an example.

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.

Why does "one" appear twice?

This is an exam practise question and I need to understand why this happens

Why does "one" appear twice?

one appear twice because when the loop first executes with value 0 , case 0 is executed and also case 1 gets executed because there is no break after the case 0.

so now the output becomes

zero
one

Now , the i value is 1 . hence case 1 will be executed which prints one more "one"

finally the i value is 2 which makes case default case gets executed

Hence the output is

zero
one
one
Unknown number


I hope now you understand the reason why one printed twice..

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.

Thanks for the help folks

one appear twice because when the loop first executes with value 0 , case 0 is executed and also case 1 gets executed because there is no break after the case 0.

so now the output becomes

zero
one

Now , the i value is 1 . hence case 1 will be executed which prints one more "one"

finally the i value is 2 which makes case default case gets executed

Hence the output is

zero
one
one
Unknown number


I hope now you understand the reason why one printed twice..

Why does the case 1 get executed when the value of i is 0.

Yes. After the case 0: is executed, execution will FALL THRU to the code for case 1:

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:
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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.