Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old January 11th, 2006, 01:33 PM
Friend of Wrox
Points: 8,019, Level: 38
Points: 8,019, Level: 38 Points: 8,019, Level: 38 Points: 8,019, Level: 38
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2005
Location: Mauchline, East Ayrshire, Scotland
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default 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
__________________
_______________________
Ayrshire Minis - a Mini E-Community
http://www.ayrshireminis.com
http://www.crmpicco.co.uk
  #2 (permalink)  
Old January 11th, 2006, 01:54 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

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
  #3 (permalink)  
Old January 18th, 2006, 06:45 AM
Friend of Wrox
Points: 8,019, Level: 38
Points: 8,019, Level: 38 Points: 8,019, Level: 38 Points: 8,019, Level: 38
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2005
Location: Mauchline, East Ayrshire, Scotland
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

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

www.crmpicco.co.uk
  #4 (permalink)  
Old March 9th, 2015, 10:37 PM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2015
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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?


Similar Threads
Thread Thread Starter Forum Replies Last Post
Switch-statement grstad C# 3 June 11th, 2008 06:53 AM
Using Switch statement in XHTML Lizane Java Basics 1 June 15th, 2007 06:46 AM
switch statement seymour_glass C# 5 January 24th, 2007 12:08 AM
C# switch statement nalla General .NET 4 December 14th, 2005 05:31 AM
build switch statement to make dynamic crmpicco Javascript How-To 0 October 27th, 2005 05:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.