Fuzail l'Corder
Fuzail l'Corder

Reputation: 90

how to break an enhanced for loop from a switch in java

I found this interesting thing you can break a enhance for loop with a break; statment but i want to know if it is possible to break a enhanced For loop from a switch statement inside that statement

For Example

String test = "(2.0+4.0)";

    for (char now : test.toCharArray()){

        switch (now) {

        case '(':
            // i want the loop to stop from this point
            break;
        case ')':
        case '/':
        case '*':
        case '+':
        case '-':
        }
    }

Edited

Found the answer labeled break. but im going Old school

String test = "(2.0+4.0)";
    boolean found = false;
    for (char now : test.toCharArray()){

        switch (now) {

        case '(':
            // i want the loop to stop from this point
            System.out.println(now);
            found = true;
            continue;
        case ')':
        case '/':
        case '*':
        case '+':
        case '-':
        }

        System.out.println("im out of switch");
        if(found)break;
    }
    System.out.println("out of Loop");

}

That Did it

Thanks For All the Answers

Upvotes: 0

Views: 3415

Answers (4)

masoud
masoud

Reputation: 56479

You can break the block:

the_block:
for (char now : test.toCharArray()){
   switch (now) {
   case '(':
       break the_block;

   // ...
   }
}

 

What it means? Is it a goto/label mechanism? ... No!

Maybe someone get confused by this syntax, many expect after break the_block; everything after the label must be executed again. No. In fact, the_block is a name for the following block. break the_block; is not a goto the_block;. It means, break the block which is named the_block. So, by breaking the block, you will expect the control goes out of the block and it correct.

To make it a bit more clear I've put a {} around the loop:

the_block: {
    for (char x : array){
       switch (x) {
       case '(':
           break the_block;

       // ...
     }
}

And, since it's not a goto/labal mechanism, it's not a bad practice, everything is clear and readable.

Upvotes: 0

sergeyan
sergeyan

Reputation: 1183

Use statement labeling:

    a: for (char now : test.toCharArray()){

        switch (now) {

        case '(':
            break a;
        case ')':
        case '/':
        case '*':
        case '+':
        case '-':
        }
    }

Upvotes: 1

Torsten Kammer
Torsten Kammer

Reputation: 16

Yes, with the following syntax:

label:
for (char now : array) {
    switch(now) {
    case '(':
        break label;
    }
}

This works for all kinds of loops in Java.

Upvotes: 0

user2336315
user2336315

Reputation: 16067

You can add a label in the for loop and break it within the switch statement.

public static void main (String args[]){
        String test = "))(2.0+4.0)";
        int i = 0;
        labelLoop :
            for (char now : test.toCharArray()){
                switch (now) {
                case '(':
                    break labelLoop;
                case ')':
                case '/':
                case '*':
                case '+':
                case '-':
                i++;
                }
            }
        System.out.println(i);
    }

This will print 2.

Upvotes: 4

Related Questions