Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does break in a switch-statement terminates a for-loop?

I am trying to understand return, break and continue. I know that break will stop the (inner) for loop. For example:

for (int i = 1; i <= 3; i++) {
            // inner loop
            for (int j = 1; j <= 3; j++) {
                if (i == 2 && j == 2) {
                    // using break statement inside the inner loop
                    break;
                }
                System.out.println(i + " " + j);
            }
        }

I know the outer loop will go on.

What I don't understand is this here:

for (int i = 0; i < 3; i++) {
            String line = null;
            switch (a) { 
                case 0:
                    line = "Hello";
                    break;
                case 1:
                    line = "How are you?";
                    break;
                case 2:
                    line = "What are you doing?";
                    break;

So it goes to case 0 and then break and the for-loop continues, why? I thought it will break the loop since it's not nested. Or is it because of the switch-statement - it's different than if-statements?

And in this case, it will terminate the whole while-loop... I can't see the difference.

while(winner == false){

                input = menuInput.nextInt();

                try {
                    if(input == 0 || input >= 9){
                        System.out.println("Ungültige Nummer, versuche nochmal!");
                        break; 
                    }
like image 942
Haidepzai Avatar asked Sep 06 '25 06:09

Haidepzai


2 Answers

You can break a switch. You can't break an if. break is applied to the closest statement that could be breaked, so

for (int j = 1; j <= 3; j++) {
    if (i == 2 && j == 2) {
        // using break statement inside the inner loop
         break;
    }
    System.out.println(i + " " + j);
}

Here break refers to the for.

While here:

for (int i = 0; i < 3; i++) {
    String line = null;
    switch (a) { 
        case 0:
            line = "Hello";
            break;

It refers to the switch.

Statements that can be breaked are for, while, do...while, switch.

For further info, you can see the spec.

like image 148
Federico klez Culloca Avatar answered Sep 07 '25 21:09

Federico klez Culloca


The break applies to the inner-most thing which can be break'd (without a label). In this case, it's the switch - because the default switch behavior falls through. First, your code. Change,

for (int i = 0; i < 3; i++) {
    String line = null;
    switch (a) { 
        case 0:
            line = "Hello";
            break;
        case 1:
            line = "How are you?";
            break;
        case 2:
            line = "What are you doing?";
            break;

Could be changed to

loop: for (int i = 0; i < 3; i++) {
    String line = null;
    switch (a) { 
        case 0:
            line = "Hello";
            break loop;
        case 1:
            line = "How are you?";
            break loop;
        case 2:
            line = "What are you doing?";
            break loop;

Now, the second behavior would be fall-through. And that might look something like,

switch (a) {
case 0: case 2: case 4: case 6: case 8:
    System.out.println("even < 10");
    break;
case 1: case 3: case 5: case 7: case 9:
    System.out.println("odd < 10");
    break;
}
like image 39
Elliott Frisch Avatar answered Sep 07 '25 19:09

Elliott Frisch