Switch Statements
EXERCISE/COMPUQUIZ Write the JavaScript Code using if…else if statementsvar n = Enter a numbervar entered = "You entered a...
<script>var n = prompt("Enter a number", "5");var entered = "You entered a number between";if (n >= 1 && n < 10){alert(""+...
}else if (n >= 40 && n <= 100){alert(""+entered+ " 39 and 100")}else if (n < 1 || n > 100){alert("You entered a number les...
Switch Statements• Switch statements work the same as if statements.  However the difference is that they can check for  m...
• The switch statement always begin with  the keyword "switch", plus a required  parameter that contains the expression  (...
SYNTAX            The program first looks for a case                         clause with a label matching theswitch (expre...
<script >var flower = prompt("What flower do you like",   "rose");switch (flower){  case "rose" :    alert(flower + " cost...
case "orchid" :   alert(flower + " costs Php 1.50");   break;  default :   alert("There is no such flower in our shop");  ...
EXERCISE/COMPUQUIZWrite the JavaScript source code using the  switch statements.var favoritemovie= Enter the title of your...
switch (favoritemovie){  case "Titanic":  alert("Not a bad choice!")  break;  case "Water World":  alert("No comment")  br...
Upcoming SlideShare
Loading in …5
×

Javascript conditional statements 2

509 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
509
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Javascript conditional statements 2

  1. 1. Switch Statements
  2. 2. EXERCISE/COMPUQUIZ Write the JavaScript Code using if…else if statementsvar n = Enter a numbervar entered = "You entered a number between";WRITE THE EXPRESSION AND STATEMENTSYou entered a number between 0 and 10You entered a number between 9 and 20You entered a number between 19 and 30You entered a number between 29 and 40You entered a number between 39 and 100You entered a number less than 1 greater than 100You did not enter a number
  3. 3. <script>var n = prompt("Enter a number", "5");var entered = "You entered a number between";if (n >= 1 && n < 10){alert(""+entered+ " 0 and 10")}else if (n >= 10 && n < 20){alert(""+entered+ " 9 and 20")}else if (n >= 20 && n < 30){alert(""+entered+ " 19 and 30")}else if (n >= 30 && n < 40){alert(""+entered+ " 29 and 40“)
  4. 4. }else if (n >= 40 && n <= 100){alert(""+entered+ " 39 and 100")}else if (n < 1 || n > 100){alert("You entered a number less than 1 or greater than 100")}else{alert("You did not enter a number!")}</script>
  5. 5. Switch Statements• Switch statements work the same as if statements. However the difference is that they can check for multiple values. Of course you do the same with multiple if..else statements, but that really doesn’t look good.• A switch statement allows a program to evaluate an expression and attempt to match the expressions value to a case label. If a match is found, the program executes the associated statement. Reference: WebCheat.com• The switch statement is basically an enhanced version of the "if-else" statement that is more convenient to use when you have code that needs to choose a path from many to follow. Reference: JavaScriptKit.com
  6. 6. • The switch statement always begin with the keyword "switch", plus a required parameter that contains the expression (or variable) you wish to evaluate. This expression is then matched against the value following each "case", and if there is a match, it executes the code contained inside that case. If no match is found, it executes the default statement at the end of the switch statement. Lets take a look with the example follows…………………………
  7. 7. SYNTAX The program first looks for a case clause with a label matching theswitch (expression) value of expression and then{ transfers control to that clause, case value1: executing the associated statements. If no matching label is statement; found, the program looks for the break; optional default clause, and if case value2: found, transfers control to that statement; clause, executing the associated break; statements. If no default clause is found, the program continues " execution at the statement " following the end of switch. Use default : statement; break to prevent the code from } running into the next case automatically.
  8. 8. <script >var flower = prompt("What flower do you like", "rose");switch (flower){ case "rose" : alert(flower + " costs Php 2.50"); break; case "daisy" : alert(flower + " costs Php 1.25"); break;
  9. 9. case "orchid" : alert(flower + " costs Php 1.50"); break; default : alert("There is no such flower in our shop"); break;}</script>
  10. 10. EXERCISE/COMPUQUIZWrite the JavaScript source code using the switch statements.var favoritemovie= Enter the title of your favorite moviecase 1 = Titanic Not a bad choicecase 2= Water World No commentcase 3= Scream 2 It has its momentsdefault statement I’m sure it was great
  11. 11. switch (favoritemovie){ case "Titanic": alert("Not a bad choice!") break; case "Water World": alert("No comment") break; case "Scream 2": alert("It has its moments") break; default : alert("Im sure it was great"); }

×