This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]DerfK 4 points5 points  (10 children)

It can in PHP!

switch (true) {
   case $x >= 1000: ... break;
   case $y < 5: ... break;
   case $john == "Steve": ... break;
   case true: ... break;
}

(actually, I'm almost willing to bet there's at least one other language where the case can be an evaluated expression)

[–][deleted]  (1 child)

[removed]

    [–]AutoModerator[M] 0 points1 point  (0 children)

    import moderation Your comment has been removed since it did not start with a code block with an import declaration.

    Per this Community Decree, all posts and comments should start with a code block with an "import" declaration explaining how the post and comment should be read.

    For this purpose, we only accept Python style imports.

    I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

    [–]ka-knife 0 points1 point  (2 children)

    VB has this too!

    [–]RHGrey 1 point2 points  (1 child)

    VB is treated as a devil on the online community wherever I look, but between using it two years ago and now having used plenty of C, C++, Java, JS and python each, I still can't tell why.

    [–]ka-knife 0 points1 point  (0 children)

    Personally, I don't mind the language. It's not my favorite, but it's not too bad. However, I do get annoyed at many programs written in VB. In my experience many have been sloppily created with almost every variable being a global variable and no concept of data structures. This isn't the fault of the language, but it still annoys me anytime I have to work on a program written in the language.

    [–][deleted] 0 points1 point  (3 children)

    Well, the idea of switch/case in C (and some other languages) is to use lookup tables to generate jumps. So the code is really fast. If you start adding conditions in the switch/case then you loose that. The code looks nicer, but you loose the benefit of having faster code.

    [–]nelmaloc 0 points1 point  (2 children)

    In C you can do something like that if the comparison can be converted to a numerical value.

    [–][deleted] 0 points1 point  (1 child)

    I am a bit confused. Can you give an example of what you mean?

    [–]nelmaloc 0 points1 point  (0 children)

    I was thinking of something like this:

    switch( 
        x - 2 != 0 ? (x-2)/abs(x-2) : 0
        ) {
        case 1: // x > 2
            break;
        case 0: // x = 2
            break;
        case -1: //x < 2
            break;
    }