Codecademy Forums

CASE and all its exploits

Think back to when BASIC had this statement,

REM A is one of five possible valid inputs
ON A GOTO 10, 20, 30, 40, 50

Those are CASE statements (preceded by a remark, as in comment).

This to me is the first switch statement I ever encountered. Think on it.

switch (A) {
 case 1: B = 10; break;
 case 2: B = 20; break;
 // ...
}

This is an ES control flow statement.

Whichever case matches the switch parameter will determine the branch that is followed.

What is less obvious is that both situations involve values. A, above, is an expression, and anything we write into the case is an expression. In a switch statement the outcome is the case where the expressions resolve to the same value.

What this tells us is that the sky’s the limit where choosing how to match expressions is concerned. This should give one some latitude to explore.

I hope this topic brings about more discussion along these lines, regardless the language and would love to explore it…

2 Likes