p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Javascript How-To (http://p2p.wrox.com/forumdisplay.php?f=87)
-   -   switch statement (http://p2p.wrox.com/showthread.php?t=37079)

crmpicco January 11th, 2006 01:33 PM

switch statement
 
Code:

default:   
break;

Is there a need for these two lines at the bottom of my JS switch statement?

www.crmpicco.co.uk

richard.york January 11th, 2006 01:54 PM

No, a default case is not required.

You also don't have to break on a default case, it is implied because you have reached the end of the switch construct.

Regards,
Rich

--
[http://www.smilingsouls.net]
Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design

crmpicco January 18th, 2006 06:45 AM

thanks Richard, that worked fine without the default: statement.

www.crmpicco.co.uk

msossoman March 9th, 2015 10:37 PM

switch with MANY cases
 
I wrote the following code for the book "Beginning JavaScript" Chapter 3, Example 3:

Code:

        <script>
                var secretNumber = parseInt (prompt ("Pick a number between 1 and 5.", ""), 10);
               
                switch (secretNumber) {
                        case 1:
                        case 2:
                                document.write ("Too low.");
                                break;
                        case 3:
                                document.write ("You guessed the number!");
                                break;
                        case 4:
                        case 5:
                                document.write ("Too high.");
                                break;
                        default:
                                document.write ("That's not a number between 1 and 5.");
                                break;
                };
        </script>

and it works just fine. But what if I want to have the user pick a number and not set any parameters (or my parameters were say 1 to 1 million)? I know the most efficient thing to do would be to write an if/else if/if statement like so:

Code:

if (secretNumber == 42) {
        document.write ("You guessed the number!");
} else if (secretNumber < 42) {
        document.write ("Too low.");       
} else {
        document.write ("Too high.");
};

Here's my question - is there a way to write a switch statement with more than just a few case statements and not have to type every single case statement? If I asked the user to try and guess the secretNumber 42 and they could choose any number between 1 and 100, instead of typing my code:
Code:

case 1:
case 2:
case 3:
 // and so on all the way to
case 41:
    break;
case 42:
    break;
case 43:
case 44:
// and so on all the way to
case 100:
    break;

is there a way I could do something like:
Code:

case (secretNumber < 42):
    document.write ("Too low.");
    break;
case 42:
    document.write ("You guessed the number!");
    break;
case (secretNumber > 42 && secretNumber <= 100):
    document.write ("Too high.");
    break;
default:
    document.write ("That's not a number between 1 and 5.");
    break;

If there is a way to do a switch statement like this, and if so, what is the syntax for the case statements?


All times are GMT -4. The time now is 11:33 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.