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.

4 Answers 4

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*/
}
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, that was one solution I approached but labels started to lose their readability when you have 10 switches :)
Are you sure you're writing a good code since you have 10 switches? ;)
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.
@Shomz It's a text-based game. I might have over-exaggerated the number of switches I have :)
4

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*/
    }
})();

Comments

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?

3 Comments

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.
That's why you have that extra check before the rest of your code after it. See above.
Of course, the function option is pretty good too, and very JS.
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*/
}

1 Comment

Same thing with Thought's solution: I have multiple switches so repeating the if statement to check for the value is impractical.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.