Order of operations in switch statements

Hi!!

Working through Basic JavaScript now, and I’m finding myself needing to look at the hints and even the solution for a few of the modules. Just wondering if someone could let me know how the code is read when calling the function in the switch statement below. Does it go from case 2 → case 3 → case 4 and so on until count++ which it then calculates the increment? Or does it go from case 1 → count++ and then back to case 2 and so on so forth.

The switch statement above is from the counting cards module, I’m also wondering how this switch statements works because would the ‘break;’ not stop the rest of the cases to be calculated? Any replies would be appreciated, don’t feel like you need to be overly polite if this is a dumb question.

Thanks, Aaron

the count++ or count-- is executed only once for each function call

the function has as input one card at a time, so for each card/function call the switch statement evaluate if count needs to be changed and then an output is given based on count

1 Like

Ah! That makes sense, I guess I didn’t understand the concept of card counting completely. Appreciate the info!

I guess I misunderstood your question. I though you were asking about how the case statements are dealt with. It goes until it finds a matching case statement, then goes down the code, ignoring case statements until it encounters a break or the default case.

We can show it with this to see what case statements are being evaluated:

const returnNum = num => { console.log('here', num); return num }

switch(2) {
  case returnNum(1):
  case returnNum(2):
    console.log('howdy')
  case returnNum(3):
    console.log('bon jour')
    break
  case returnNum(4):
    console.log('hola')
    break
  case returnNum(5):
    console.log('guten tag')
    break
  default:
    console.log('konichiwa')
}

This outputs:

here 1
here 2
howdy
bon jour

So:

  • the 1st case doesn’t match, go onto next line.
  • 2nd case matches, go onto next line.
  • log howdy, go onto next line
  • case statement, ignore, go to next line
  • log bon jour
  • break - exit switch statement.

So, you asked:

I’m also wondering how this switch statements works because would the ‘break;’ not stop the rest of the cases to be calculated?

If I understand the question, then yes, it keep the rest of the cases from being calculated - it’s supposed to. I was curious if those lines even get evaluated - my test indicates that they do not.

Any replies would be appreciated, don’t feel like you need to be overly polite if this is a dumb question.

The only dumb questions are the ones people don’t ask because they’re embarrassed. This is hard stuff. There’s no question you can ask that 10 other people want to ask but are too chicken.

1 Like

Incredibly insightful response, thank you so much! I also appreciate the positive affirmation on asking questions. Your reply really helped me to further understand switch statements, thanks again

1 Like

Also though, really quickly, could you explain to me why cases returnNum(3),(4),(5) didn’t output a here 3, here 4, and a here 5? even though 1 and 2 did?

Sure. Because after we encountered the break statement, it exited the switch operation. It was done at that point. That is the point of break - it is saying, “I am completely done, exit is this flow control”.

1 Like

Okay, got it, I understand that once we encounter a break statement it is completely done. In that case why wasn’t “here 3” outputted? It’s before the break as far as I can tell?
Thanks, again for replying so quickly.

In that case why wasn’t “here 3” outputted?

That was really the question that interested me the most. I wanted to know if that line would even be evaluated. The JS parser sees that a case has been matched so it doesn’t matter if any other case matches so it stops checking them. It just works down line by line, ignoring any new case statement, until it hits a break or default. Also return would effectively end it, too.