Abyss Gazing (Switch Statement Logic)

A wise @mtf once said that beginners often make the mistake of trying to dive too deep into understanding the logic behind simple programming concepts, and they get lost. Lo and behold, that’s exactly where I am with switch statements in C and JavaScript — I just can’t help myself.

#include <stdio.h>

int main() {

  int randomNumber = 3;

  switch(randomNumber) {
    case 1:
      printf("One\n");
      break;
    case 2:
      printf("Two\n");
      break;
    case 3:
      printf("Three\n");
      break;
    case 4:
      printf("Four\n");
      break;
    case 5:
      printf("Five\n");
      break;
    default:
      printf("Unknown\n");
      break;
  }
}

I already understand that a switch statement only begins execution when it finds the case that matches the value of the switch variable. After that, if you don’t include break, it’ll run every line of code within the block, including default.

My question is: why is that so? Why not have an implementation like if ... else if ... else statements, where the execution exits the block by default, without needing to code it explicitly? Because it already operates like that for every case above the matching one. I tested removing break from every line, and it skips 1 and 2, but prints 4, 5, and default.

Forgive me Father @mtf, but your flock is stubborn. Help us see the light, and the (probably obvious) usefulness of that behavior.

1 Like

break is there by design to short-circuit another feature that is also there by design, fall through which is useful when we want to match multiple cases. That would be intentional. See what you can find on this feature in your reading and let us no if that clears up your understanding.

A silly demo:

const parity = function (x) {
    switch (x) {
    case 1:
    case 3:
    case 5:
    case 7:
    case 9: return 'Odd parity';
    case 0:
    case 2:
    case 4:
    case 6:
    case 8: return 'Even parity';
    default: return null
}
const randInt = function () {
    return Math.floor(Math.random() * 10)
}
let a = randInt()
console.log(a, parity(a))    //  4 Even parity
a = randInt()
console.log(a, parity(a))    //  7 Odd parity

This is not practical in terms of production code but it demonstrates case fall through. There may be other examples on the forums. Note that the switch is inside a function so instead of break we use return.

2 Likes

The advantage of a function is that it can give a value back to the program, rather than just an action that a switch can do. Combined, the two make a nice suite, with caveats and provisos. It’s a design consideration and must be considered along the lines of syntactic sugar versus plausible, efficient logic. There are pluses and minuses to nearly every approach, In optimal situations we don’t want to throw away the baby with the bath water.

As for help with ‘C’, not my turf. I get it, but don’t write or run it. It is something that at my age we tend to shy away from. Script is more in my line.

1 Like

Hitting the nail in the head as usual, Captain. Thank you. After seeing your example and doing some research from there, sure enough, it became pretty obvious the usefulness of having several cases “consolidated” around the same return value.

Programming is proving to be a fascinating topic to study more thoroughly. All fields of science I came across so far produced the same effect, but never have I experienced it so intensely: the merry-go-round effect. One moment you think you understand programming in its deepest aspects. You smile proudly and take the next step, realizing the truth is you can barely even read and write. You struggle, overcome, understand a bit more. Rinse and repeat. Love it.

1 Like