3

There is a similar question concerning this problem in C++, but I'm using JavaScript here. I'm basically in the same situation as the OP in the other post.

var input = prompt();
while(true) {
    switch(input) {
        case 'hi':
        break;
        case 'bye':
            //I want to break out of the switch and the loop here
        break;
    }
    /*Other code*/
}

Is there anyway to do that?

I also have multiple switches in the /*Other code*/ section where the code should also break.

Community
  • 1
  • 1
unbindall
  • 514
  • 1
  • 13
  • 29

4 Answers4

6

You can use labels with the break statement in js

var input = prompt();

outside:
while(true) {
    switch(input) {
        case 'hi':
        break;
        case 'bye':
            //I want to break out of the switch and the loop here
        break outside;
    }
    /*Other code*/
}
Calummm
  • 819
  • 1
  • 8
  • 21
  • Yes, that was one solution I approached but labels started to lose their readability when you have 10 switches :) – unbindall Nov 30 '14 at 22:55
  • Are you sure you're writing a good code since you have 10 switches? ;) – Shomz Nov 30 '14 at 23:12
  • Do you mean you have 10 different exit points/loops or 10 exit points in the switch statement that break the same loop. If the latter there is nothing stopping you breaking to the same label multiple times. – Calummm Nov 30 '14 at 23:57
  • 1
    @Shomz It's a text-based game. I _might_ have over-exaggerated the number of switches I have :) – unbindall Mar 06 '15 at 01:44
3

Wrap the whole thing in a function, then you can simply return to break out of both.

var input = prompt();
(function () {
    while(true) {
        switch(input) {
            case 'hi':
            break;
            case 'bye':
            return;
        }
        /*Other code*/
    }
})();
Etheryte
  • 24,589
  • 11
  • 71
  • 116
1

It's the same answer as in C++, or most other languages. Basically, you just add a flag to tell your loop that it's done.

var input = prompt();
var keepGoing = true;
while(keepGoing) {
    switch(input) {
        case 'hi':
            break;
        case 'bye':
            //I want to break out of the switch and the loop here
            keepGoing = false;
            break;
    }
    // If you need to skip other code, then use this:
    if (!keepGoing)  break;
    /*Other code*/
}

Make sense?

Thought
  • 700
  • 2
  • 9
  • 21
  • I've tried this in my original code, but the `while` loop only checks the argument after everything inside of it has run, making it pretty much useless for stopping mid-execution. – unbindall Nov 30 '14 at 22:46
  • That's why you have that extra check before the rest of your code after it. See above. – Thought Nov 30 '14 at 22:56
  • Of course, the function option is pretty good too, and very JS. – Thought Nov 30 '14 at 22:57
0

Don't diminish readability in the name of "one less line of code". Make your intentions clear:

while (true) {
  var quit = false;

  switch(input) {
    case 'hi':
      break;

    case 'bye':
      quit = true;
      break;
  }

  if (quit)
    break;

  /*Other code*/
}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • Same thing with Thought's solution: I have multiple switches so repeating the `if` statement to check for the value is impractical. – unbindall Mar 06 '15 at 01:47