Web Hosting Deals Holiday Logo Design Webcam Chat Website Header Templates Register domain Search Engine Optimisation Web Hosting
Go Back   Talk Mania Forum > Tutorials > PHP / Perl / Java / JavaScript / CGI Tutorials

PHP / Perl / Java / JavaScript / CGI Tutorials PHP / Perl / Java / JavaScript / CGI Tutorials Please do not use this Forum to advertise your site or to link to tutorials.

 Image
Buy Sell Downloads

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-30-2007, 03:34 PM
Scorpian8867's Avatar
Junior Member
 
Join Date: Feb 2007
Location: Texas
Posts: 24
Scorpian8867 is on a distinguished road
Send a message via AIM to Scorpian8867 Send a message via MSN to Scorpian8867
[JS] Switch/Case Statements

Alright, JavaScript has many ways to execute certain things. However, some of these things might need to executed if a certain condition is present, and not at all if said condition isn't. Let's say that we need to alert a box that says a new message depending on the time of day. We've already defined the time of day previous in this code, and it will say a number representing the time span. 0 will mean morning, 1 will mean afternoon, and 2 will mean evening. However, the code we used isn't perfect. It might return completely different number. If this happens, we want to let the user know there was an error.

So, how could we do this? Well, let's try an if/else statement.

if(time == 0){
alert("Good morning!");
}else if(time == 1){
alert("Good afternoon!");
}else if(time == 2){
alert("Good evening!");
}else{
alert("Error!");
}


Yes, that code works. Yes, it will show the alert boxes. Yet, it's not really efficient, is it? How can we make it more efficient? No, not with one-line conditionals. We'll use a switch/case statement.

Whoa whoa! Wtf is a switch/case statement? It's a statement that will execute certain commands depending on what the value of a specified variable or property is. This can be extremely useful when you have a varied variable like the example above. So, how's it look?

switch(variable/property){
case "value": code to be executed break;
case "value": code to be executed break;
case "value": code to be executed break;
default: code to be executed
}

It's very important that you must break the code after executing it. Don't forget the ending break;. Now, you might be confused as to what "default" is. It's the code to be executed if none of the above cases are true. That fits perfectly into our example. Speaking of which, let's try the example using the switch/case method.

switch(time){
case "0": alert("Good morning!"); break;
case "1": alert("Good afternoon!"); break;
case "2": alert("Good evening!"); break;
default: alert("Error!");
}


See how much neater and more organized that looks as opposed to the if/else statement? It'll also load faster since it doesn't have to loop as much. Once you get the switch/case statement down, it can prove to be extremely useful, especially when working with arrays. This is just another one of those basic concepts that can save you loads of time and make a code more efficient.

I hope you learned something today.

__________________
RoddyInnovations.com

Last edited by Scorpian8867; 03-30-2007 at 03:36 PM. Reason: vBulletin doesn't render the ACSII for space
Reply With Quote
  #2 (permalink)  
Old 03-30-2007, 03:36 PM
Base's Avatar
Senior Member
 
Join Date: Mar 2007
Location: North Yorkshire, UK
Posts: 121
Base is on a distinguished road
Send a message via MSN to Base Send a message via Skype™ to Base
I would swear i have seen this tutorial before?

Base
__________________
http://www.pointserv.co.uk/gfx/deal_..._pointserv.png
PointServ.co.uk - One of the UK's cheapest webhosting companies.
Reply With Quote
  #3 (permalink)  
Old 03-30-2007, 03:40 PM
Scorpian8867's Avatar
Junior Member
 
Join Date: Feb 2007
Location: Texas
Posts: 24
Scorpian8867 is on a distinguished road
Send a message via AIM to Scorpian8867 Send a message via MSN to Scorpian8867
Quote:
Originally Posted by Base View Post
I would swear i have seen this tutorial before?

Base
I've submitted it to a few places on ProBoards before. Is that where you saw it, per chance?
__________________
RoddyInnovations.com
Reply With Quote
  #4 (permalink)  
Old 03-31-2007, 12:42 AM
Base's Avatar
Senior Member
 
Join Date: Mar 2007
Location: North Yorkshire, UK
Posts: 121
Base is on a distinguished road
Send a message via MSN to Base Send a message via Skype™ to Base
God knows, just looks familiar

Good tutorial tho!

Base
__________________
http://www.pointserv.co.uk/gfx/deal_..._pointserv.png
PointServ.co.uk - One of the UK's cheapest webhosting companies.
Reply With Quote
  #5 (permalink)  
Old 04-04-2007, 06:49 PM
Junior Member
 
Join Date: Mar 2007
Posts: 20
kraze101 is on a distinguished road
good one just done reading and i understand the switch statement alittle better. i did some in c programming but i didnt understand what the hell i was doing.

thanks
Reply With Quote
  #6 (permalink)  
Old 04-25-2007, 02:31 PM
Junior Member
 
Join Date: Apr 2007
Posts: 4
Ultimate` is on a distinguished road
sure is a nice tutorial.. these are really good basics :P
Reply With Quote
  #7 (permalink)  
Old 04-26-2007, 11:09 PM
Junior Member
 
Join Date: Apr 2007
Posts: 3
cime is on a distinguished road
Thanks
Good for beginners.

Reply With Quote
Reply

Bookmarks

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT +2. The time now is 01:45 AM.


Fake ID

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC8
Forums Copyright © Talk-Mania.com