Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #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
Reply With Quote
  #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 5 Times in 5 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
Reply With Quote
  #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
Reply With Quote
  #4 (permalink)  
Old March 9th, 2015, 11: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?
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Switch-statement grstad C# 3 June 11th, 2008 07:53 AM
Using Switch statement in XHTML Lizane Java Basics 1 June 15th, 2007 07: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 06:44 AM



All times are GMT -4. The time now is 07:44 PM.


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