Are Switch Statements Useful/Practical?


#1

Or are switch statements basically almost as useless as alert() and prompt() in “real” code?

Like, I can’t imagine how to incorporate a switch within a loop for anything more complicated than “demonstration” code…it seems like switch is the kind of thing in its own little world (like alert() and prompt()), with very narrow use-cases…am I right?

I ask 'cause I can see how switch would be sooooo much more “elegant” than using a bunch of if, if-else, and if-else-if-else statements but the problem for me lies in being able to break in and out of switch cases…

Thanks for all insight!!


Can't Output Array Element & How Do I "Recycle" My Prompt()
#2

Switch statements and if-else statements work basically the same. The reason it is still used is because it improves the readability.

example:

int value = // some value
if (value == 1) {
    doThis();
} else if (value == 2) {
    doThat();
} else {
    doTheOther();
}

In this case i would recommend a switch statement since it will be easyer to see that you are just checking the value of 1 variable.

Its a matter what you feel comfortable with tho. If you dont want to use it you dont have to.


#3

Here’s an example of a switch being used

switch(choice)
    {
        case(1):
            topScore(1);
            scores.tallyBonus();
            break;
        case(2):
            topScore(2);
            scores.tallyBonus();
            break;
        case(3):
            topScore(3);
            scores.tallyBonus();
            break;
        case(4):
            topScore(4);
            scores.tallyBonus();
            break;
        case(5):
            topScore(5);
            scores.tallyBonus();
            break;
        case(6):
            topScore(6);
            scores.tallyBonus();
            break;
        case(7):
            threeKind();
            break;
        case(8):
            fourKind();
            break;
        case(9):
            fullHouse();
            break;
        case(10):
            smStraight();
            break;
        case(11):
            lgStraight();
            break;
        case(12):
            yahtzee();
            break;
        case(13):
            chance();
            break;
        default: //something horrible happened
            std::cout << "Program terminating.";
            exit(0);

The user is prompted with a menu of options 1-13 to pick how they want to score a roll of dice
The switch statement executes the appropriate statements based on their selection.

Breaking makes sense here. I only want the user to make one choice. I don’t want them to evaluate and score a full house and then later via an else if, also evaluate a lgStraight.

The switch is inside a while loop that ensures proper input, the default statement should never be reached.


#4

The example above looks like C but is still applicable to JS save for the last case, the default. To clarify,

break terminates the flow through the switch which is top down. Once an action (branch) is chosen we don’t want to proceed any further down the list but hand flow to the next statement following the switch.

In the case above, the branches are all function calls. One of them will be executed and flow from that function will return to that point in the switch where break will interrupt it.

The only time we wouldn’t use break is when each branch has a return, meaning the switch is inside a function.

The default case is not necessarily an indication that something went wrong as much as it is literally the default action if no match is found among the given cases. It is the else in an if statement.


#5

Thanks for all that, people…let me revisit the crux of the matter for me: is there a way to get in and out of a switch statement besides wrapping it in a function that’s called by some other code?

It appears not…I think now that my original post is misleading and part of my frustration has nothing to do with the switch statement itself after all…I think I’m really wondering whether the only way to jump in and out of subroutines is to wrap a function around them (indeed, I guess a JavaScript function is really just what’s been traditionally called a “subroutine” in computer science, right?)?..


#6

When code is boiled down to machine level, it is all in one block of memory and everything is located by address. All functionality is housed in that block of memory, and the isolated bits of repeatable code are called sub-routines.

Functions, on the other hand are script using tokens and keywords, and their code is wrapped in a function body. Functions are objects and come with an assortment of useful properties not the least of which are prototype, this, arguments, return, and more. A learner doesn’t need to know all this. It surfaces slowly, and should. We don’t want learner’s being bogged down by things they can’t possibly hope to understand right now.


#7

Hmmm, but I keep bumping up against it all…

I understand that one doesn’t need to know quantum physics in order to use what’s essentially the quantum-based device that is a computer, but it’s like how it really helps to know human psychology even though one can certainly do quite well in life without ever knowing why people – including oneself – behave the way they do…

But we’re getting into pedagogy here and I see your POV though I’d enjoy learning about the wider context of things myself.


#8

Remember the old adage, “To a hammer, everything is a nail.”


#9

LOL – indeed…which is why to an idiot, everything’s a problem (my case! [though it’s arguable that to a really idiotic idiot, nothing’s a problem]).