What is the switch statement?

Are you preparing for IT certification? With practice questions, study notes, interactive quizzes, tips and technical articles, uCertify PrepKits ensure that you get a solid grasp of core technical concepts to ace your certification exam in first attempt.

What is the switch statement?

Rating:

The switch statement is a conditional statement. It is similar to the if statement. The switch statement compares an expression with cases, and if any case returns true, the associated statements are run. If none of the cases evaluate to true, the default statement is run. The switch statement can be used for writing code that makes decisions. The decision-making process is used to execute a program according to one or more conditions.

The syntax of using the switch statement is as follows:

switch(expression)
{
   case label:
      statements;
      [break]
   case label:
      statements;
      [break]
   [default
      statements;
      break]
}


In the above syntax, the break statement and the default statement are optional. The break statement stops the execution of the switch statement after the execution of a case statement. If the break statement is removed, all the case statements following the correct case statement will be executed.

For example:

var a="b"
switch(a){
   case "a":
      trace("You have entered a")
      break
   case "b":
      trace("You have entered b")
      break
   case "c":
      trace("You have entered c")
      break
   default:
      trace("You have entered the wrong character")
      break
}


The above code will return "You have entered b", as the variable is b.


Rating:



Other articles

Click here to Article home

 
uCertify.com | Our Company | Articles | Privacy | Security | Contact Us | News and Press Release | uCertify India
MCSE: MCSA, MCTS, MCITP    JAVA Certification: SCJP, SCWCD Cisco Certification: CCNA, CCENT, A+, Network+, Security+
Oracle Certification: OCP 9i, OCP 10g, OCA 9i, OCA 10g CIW foundation    EC-212-32    CISSP    Photoshop ACE    Adobe Flash ACE
© 2008 uCertify.com. All rights reserved. All trademarks are the property of their respective owners.