0

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

2
  • It's called a for-each loop. Commented Nov 28, 2013 at 18:21
  • That continue should be break in your old-school example. Commented Nov 28, 2013 at 18:37

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Worked like a charm but since using label is not a good practice im gonna go with old school
@Fuzaill'Corder: No, it's good practice since you haven't goto/label as same as GW-Basic, it's a polite way to break nested loops and everyone by seeing that label can find out the reason.
1

Use statement labeling:

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

        switch (now) {

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

Comments

0

Yes, with the following syntax:

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

This works for all kinds of loops in Java.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.