3

My code is not so long so I am pasting all of it here.

The code is not complete but when I run it it first jumps to case "start" which it is supposed to, and then jumps to case "end". I can see it because it prints both blocks' console log texts. Why is it jumping to the "end" case?

<html>
    <body>
        <script>
            function stepStream(stream,step){
                switch (stream[step]){
                    case "start":
                        console.log("Started reading stream...");
                    case "end":
                        var success = "Finished reading dataStream.";
                        console.log(success);
                        return success;
                    default:
                        throw "Data stream format is bad";                  
                    case "gesture":
                        console.log("Running case gesture! But why?");
                        step+=1;
                        stepStream(stream,step);
                    case "say":
                        step+=1;
                        stepStream(stream,step);
                    case "sleep":
                        step+=1;
                        stepStream(stream,step);
                }

            }

            var sentence1 = "Where are my bananas? I thought you put them in my bag?";
            var sentence2 = "This is a rather irritating situattion.";  
            var dataStream = ["start","gesture","banzai","sleep",1.0,"say",sentence1,
                                "say",sentence2,"gesture","kubikasige","end"];
            stepStream(dataStream,0);//Second parameter sets where to start reading the dataStream.


        </script>
    </body>
</html>
Kanerva Peter
  • 237
  • 5
  • 13
  • You need a `break` at the end of each `case` (except for the ones already ending with `return` or `throw`): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch. – Ed Gibbs Feb 18 '18 at 22:02
  • Possible duplicate of [JavaScript switch strange behavior](https://stackoverflow.com/questions/32369133/javascript-switch-strange-behavior) – Sebastian Simon Feb 18 '18 at 22:17

4 Answers4

5

The problem is that you are missing the break keyword after your case code. Without the break, subsequent blocks will be executed, that is why end is executed after start code. You can read more about this on this W3Schools link.

Additionally, from the JS reference:

The optional break statement associated with each case label ensures that the program breaks out of switch once the matched statement is executed and continues execution at the statement following switch. If break is omitted, the program continues execution at the next statement in the switch statement.

So your code should look like:

function stepStream(stream,step){
                switch (stream[step]){
                    case "start":
                        console.log("Started reading stream...");
                        break;
                    case "end":
                        var success = "Finished reading dataStream.";
                        console.log(success);
                        return success;
                    default:
                        throw "Data stream format is bad";                  
                    case "gesture":
                        //commUGesture(stream[i+1]);
                        //createLogLine("robot:CommU","event:gesture:"+stream[i+1]);
                        console.log("Running case gesture! But why?");
                        step+=1;
                        stepStream(stream,step);
                        break;
                    case "say":
                        step+=1;
                        stepStream(stream,step);
                        break;
                    case "sleep":
                        step+=1;
                        stepStream(stream,step);
                        break;
                }

Your "end" case has a return at the end, hence the code doesn't fall through to the other cases. Ideally, there should be a break at the end of each.

Stuti Rastogi
  • 1,162
  • 2
  • 16
  • 26
  • Ah, okay, got it, but why is it going to the "end" case when the only case I pass is "start"?? – Kanerva Peter Feb 18 '18 at 22:05
  • Because you do not have a break. It doesn't matter if case is matched after that or not. All the code till the next break (or return, throw) will be executed. I have added some more information in the answer. Hope it helps! – Stuti Rastogi Feb 18 '18 at 22:06
  • Thanks, the switch function feels like a risky function to use though... – Kanerva Peter Feb 18 '18 at 22:23
  • Not really, it becomes a habit to put break after every case the more you use it. Just like it becomes a habit to put semi colons or closing braces. I am sure you will find it useful :) – Stuti Rastogi Feb 18 '18 at 22:24
0

You forgot to add a break statement in the start block, therefore it falls through to the end block.

TheBeardedOne
  • 292
  • 3
  • 6
0

Problem is simple all switch case have to end with break or return statements in your case that is missing.

switch(var1)
{
    case "start":
        console.log("Started");
        break;
    case "end":
         console.log("stopped");
         return "";
     .
     .
     .
}
ashok19r91d
  • 474
  • 6
  • 17
0

The code will start running at the first matching "case", but it only stops running when it has reached a "break" or "return" statement;

Maiya
  • 932
  • 2
  • 10
  • 26