SlideShare a Scribd company logo
3
Most read
Switch Statements
EXERCISE/COMPUQUIZ
 Write the JavaScript Code using if…else if statements
var n = Enter a number
var entered = "You entered a number between";
WRITE THE EXPRESSION AND STATEMENTS
You entered a number between 0 and 10
You entered a number between 9 and 20
You entered a number between 19 and 30
You entered a number between 29 and 40
You entered a number between 39 and 100
You entered a number less than 1 greater than 100
You did not enter a number
<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“)
}
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>
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 expression's
  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
• 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…………………………
SYNTAX            The program first looks for a case
                         clause with a label matching the
switch (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.
<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;
case "orchid" :
   alert(flower + " costs Php 1.50");
   break;
  default :
   alert("There is no such flower in our shop");
   break;
}
</script>
EXERCISE/COMPUQUIZ
Write the JavaScript source code using the
  switch statements.
var favoritemovie= Enter the title of your
  favorite movie
case 1 = Titanic   Not a bad choice
case 2= Water World No comment
case 3= Scream 2       It has its moments
default statement I’m sure it was great
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("I'm sure it was great");
  }

More Related Content

Similar to Javascript conditional statements 2 (20)

PDF
web programming javascriptconditionalstatements.pdf
Jayaprasanna4
 
PPTX
Javascript conditional statements
nobel mujuji
 
PPTX
Programming Fundamentals lec 08 .pptx
hanzalaahmad787
 
PPTX
Switch Statements.pptx
EnricoValdez3
 
PPTX
week 3 Programming lecture 06 (1) .pptx
ZainabNoor83
 
PPTX
If else statement 05 (js)
AbhishekMondal42
 
PPTX
Javascript conditional statements 1
Jesus Obenita Jr.
 
PPTX
Switch Case Statement
Ducat India
 
PPT
Decision Making and Branching in C
RAJ KUMAR
 
PPT
Final requirement
Faye Salosagcol
 
PDF
Lecture 7 Control Statements.pdf
SalmanKhurshid25
 
PPT
C language control statements
suman Aggarwal
 
PPTX
C# conditional branching statement
baabtra.com - No. 1 supplier of quality freshers
 
DOCX
Janakiram web
MARELLA CHINABABU
 
PPTX
What is Switch Case?
AnuragSrivastava272
 
PPTX
Presentation on C Switch Case Statements
Dipesh Panday
 
PPTX
Java Decision Control
Jayfee Ramos
 
PDF
Java Programming - Conditional Statements (Switch).pdf
ROWELL MARQUINA
 
PDF
itft-Decision making and branching in java
Atul Sehdev
 
PPTX
Switch Case in C Programming
Sonya Akter Rupa
 
web programming javascriptconditionalstatements.pdf
Jayaprasanna4
 
Javascript conditional statements
nobel mujuji
 
Programming Fundamentals lec 08 .pptx
hanzalaahmad787
 
Switch Statements.pptx
EnricoValdez3
 
week 3 Programming lecture 06 (1) .pptx
ZainabNoor83
 
If else statement 05 (js)
AbhishekMondal42
 
Javascript conditional statements 1
Jesus Obenita Jr.
 
Switch Case Statement
Ducat India
 
Decision Making and Branching in C
RAJ KUMAR
 
Final requirement
Faye Salosagcol
 
Lecture 7 Control Statements.pdf
SalmanKhurshid25
 
C language control statements
suman Aggarwal
 
C# conditional branching statement
baabtra.com - No. 1 supplier of quality freshers
 
Janakiram web
MARELLA CHINABABU
 
What is Switch Case?
AnuragSrivastava272
 
Presentation on C Switch Case Statements
Dipesh Panday
 
Java Decision Control
Jayfee Ramos
 
Java Programming - Conditional Statements (Switch).pdf
ROWELL MARQUINA
 
itft-Decision making and branching in java
Atul Sehdev
 
Switch Case in C Programming
Sonya Akter Rupa
 

More from Jesus Obenita Jr. (20)

PPTX
Organization and management 3 a Evolution of Management Theory
Jesus Obenita Jr.
 
PPTX
Organization and management 2 Management Function
Jesus Obenita Jr.
 
PPTX
Organization and management 1
Jesus Obenita Jr.
 
PPTX
Designing web page marquee and img tag
Jesus Obenita Jr.
 
PPTX
Ms excel 2013 formatting worksheets
Jesus Obenita Jr.
 
PPTX
Ms excel 2013 data management
Jesus Obenita Jr.
 
PPTX
Microsoft Excel introduction
Jesus Obenita Jr.
 
PPTX
Word 2013 working with pictures
Jesus Obenita Jr.
 
PPTX
Word 2013 Formatting Page
Jesus Obenita Jr.
 
PPTX
Word 2013 8
Jesus Obenita Jr.
 
PPTX
Ms word 2013 7
Jesus Obenita Jr.
 
PPTX
Ms word 2013 6
Jesus Obenita Jr.
 
PPTX
Ms word 2013 4
Jesus Obenita Jr.
 
PPTX
Ms word 2013 2
Jesus Obenita Jr.
 
PPTX
Ms word 2013
Jesus Obenita Jr.
 
PPTX
Parts of the ms word 2013 screen and
Jesus Obenita Jr.
 
PPTX
Word processor
Jesus Obenita Jr.
 
PPT
Session 2 test construction.mt's
Jesus Obenita Jr.
 
PPTX
Cooking ingredients
Jesus Obenita Jr.
 
PPTX
Color theory
Jesus Obenita Jr.
 
Organization and management 3 a Evolution of Management Theory
Jesus Obenita Jr.
 
Organization and management 2 Management Function
Jesus Obenita Jr.
 
Organization and management 1
Jesus Obenita Jr.
 
Designing web page marquee and img tag
Jesus Obenita Jr.
 
Ms excel 2013 formatting worksheets
Jesus Obenita Jr.
 
Ms excel 2013 data management
Jesus Obenita Jr.
 
Microsoft Excel introduction
Jesus Obenita Jr.
 
Word 2013 working with pictures
Jesus Obenita Jr.
 
Word 2013 Formatting Page
Jesus Obenita Jr.
 
Word 2013 8
Jesus Obenita Jr.
 
Ms word 2013 7
Jesus Obenita Jr.
 
Ms word 2013 6
Jesus Obenita Jr.
 
Ms word 2013 4
Jesus Obenita Jr.
 
Ms word 2013 2
Jesus Obenita Jr.
 
Ms word 2013
Jesus Obenita Jr.
 
Parts of the ms word 2013 screen and
Jesus Obenita Jr.
 
Word processor
Jesus Obenita Jr.
 
Session 2 test construction.mt's
Jesus Obenita Jr.
 
Cooking ingredients
Jesus Obenita Jr.
 
Color theory
Jesus Obenita Jr.
 
Ad

Javascript conditional statements 2

  • 2. EXERCISE/COMPUQUIZ Write the JavaScript Code using if…else if statements var n = Enter a number var entered = "You entered a number between"; WRITE THE EXPRESSION AND STATEMENTS You entered a number between 0 and 10 You entered a number between 9 and 20 You entered a number between 19 and 30 You entered a number between 29 and 40 You entered a number between 39 and 100 You entered a number less than 1 greater than 100 You did not enter a number
  • 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. } 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. 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 expression's 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. • 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. SYNTAX The program first looks for a case clause with a label matching the switch (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. <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. case "orchid" : alert(flower + " costs Php 1.50"); break; default : alert("There is no such flower in our shop"); break; } </script>
  • 10. EXERCISE/COMPUQUIZ Write the JavaScript source code using the switch statements. var favoritemovie= Enter the title of your favorite movie case 1 = Titanic Not a bad choice case 2= Water World No comment case 3= Scream 2 It has its moments default statement I’m sure it was great
  • 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("I'm sure it was great"); }