SlideShare a Scribd company logo
1 of 11
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

Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)heoff
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C ProgrammingKamal Acharya
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c languagechintupro9
 
Microsoft opened the source code of Xamarin.Forms. We couldn't miss a chance ...
Microsoft opened the source code of Xamarin.Forms. We couldn't miss a chance ...Microsoft opened the source code of Xamarin.Forms. We couldn't miss a chance ...
Microsoft opened the source code of Xamarin.Forms. We couldn't miss a chance ...PVS-Studio
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 SlidesRakesh Roshan
 
Fundamentals of programming)
Fundamentals of programming)Fundamentals of programming)
Fundamentals of programming)jakejakejake2
 
Fundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medinaFundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medinarurumedina
 
Javascript conditional statements 1
Javascript conditional statements 1Javascript conditional statements 1
Javascript conditional statements 1Jesus Obenita Jr.
 
3-Conditional-if-else-switch btech computer in c.pdf
3-Conditional-if-else-switch btech computer in c.pdf3-Conditional-if-else-switch btech computer in c.pdf
3-Conditional-if-else-switch btech computer in c.pdfArkSingh7
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)Jyoti Bhardwaj
 
Java input Scanner
Java input Scanner Java input Scanner
Java input Scanner Huda Alameen
 
Javascript sivasoft
Javascript sivasoftJavascript sivasoft
Javascript sivasoftch samaram
 
javase-1.4.2-docs-guide-lang-assert
javase-1.4.2-docs-guide-lang-assertjavase-1.4.2-docs-guide-lang-assert
javase-1.4.2-docs-guide-lang-assertlukebonham
 
05 Conditional statements
05 Conditional statements05 Conditional statements
05 Conditional statementsmaznabili
 

Similar to Javascript conditional statements 2 (20)

Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C Programming
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c language
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
 
Microsoft opened the source code of Xamarin.Forms. We couldn't miss a chance ...
Microsoft opened the source code of Xamarin.Forms. We couldn't miss a chance ...Microsoft opened the source code of Xamarin.Forms. We couldn't miss a chance ...
Microsoft opened the source code of Xamarin.Forms. We couldn't miss a chance ...
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 Slides
 
Final requirement
Final requirementFinal requirement
Final requirement
 
M C6java5
M C6java5M C6java5
M C6java5
 
Fundamentals of programming)
Fundamentals of programming)Fundamentals of programming)
Fundamentals of programming)
 
Fundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medinaFundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medina
 
Javascript conditional statements 1
Javascript conditional statements 1Javascript conditional statements 1
Javascript conditional statements 1
 
3-Conditional-if-else-switch btech computer in c.pdf
3-Conditional-if-else-switch btech computer in c.pdf3-Conditional-if-else-switch btech computer in c.pdf
3-Conditional-if-else-switch btech computer in c.pdf
 
Java unit 11
Java unit 11Java unit 11
Java unit 11
 
Vb script tutorial
Vb script tutorialVb script tutorial
Vb script tutorial
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
 
Java input Scanner
Java input Scanner Java input Scanner
Java input Scanner
 
Javascript sivasoft
Javascript sivasoftJavascript sivasoft
Javascript sivasoft
 
javase-1.4.2-docs-guide-lang-assert
javase-1.4.2-docs-guide-lang-assertjavase-1.4.2-docs-guide-lang-assert
javase-1.4.2-docs-guide-lang-assert
 
Javascript
JavascriptJavascript
Javascript
 
05 Conditional statements
05 Conditional statements05 Conditional statements
05 Conditional statements
 

More from Jesus Obenita Jr.

More from Jesus Obenita Jr. (20)

Organization and management 3 a Evolution of Management Theory
Organization and management 3 a Evolution of Management TheoryOrganization and management 3 a Evolution of Management Theory
Organization and management 3 a Evolution of Management Theory
 
Organization and management 2 Management Function
Organization and management 2 Management FunctionOrganization and management 2 Management Function
Organization and management 2 Management Function
 
Organization and management 1
Organization and management 1Organization and management 1
Organization and management 1
 
Designing web page marquee and img tag
Designing web page  marquee and img tagDesigning web page  marquee and img tag
Designing web page marquee and img tag
 
Ms excel 2013 formatting worksheets
Ms excel 2013 formatting worksheetsMs excel 2013 formatting worksheets
Ms excel 2013 formatting worksheets
 
Ms excel 2013 data management
Ms excel 2013 data managementMs excel 2013 data management
Ms excel 2013 data management
 
Microsoft Excel introduction
Microsoft Excel introductionMicrosoft Excel introduction
Microsoft Excel introduction
 
Word 2013 working with pictures
Word 2013 working with picturesWord 2013 working with pictures
Word 2013 working with pictures
 
Word 2013 Formatting Page
Word 2013 Formatting PageWord 2013 Formatting Page
Word 2013 Formatting Page
 
Word 2013 8
Word 2013 8Word 2013 8
Word 2013 8
 
Ms word 2013 7
Ms word 2013 7Ms word 2013 7
Ms word 2013 7
 
Ms word 2013 6
Ms word 2013 6Ms word 2013 6
Ms word 2013 6
 
Ms word 2013 4
Ms word 2013 4Ms word 2013 4
Ms word 2013 4
 
Ms word 2013 2
Ms word 2013 2Ms word 2013 2
Ms word 2013 2
 
Ms word 2013
Ms word 2013Ms word 2013
Ms word 2013
 
Parts of the ms word 2013 screen and
Parts of the ms word 2013 screen andParts of the ms word 2013 screen and
Parts of the ms word 2013 screen and
 
Word processor
Word processorWord processor
Word processor
 
Session 2 test construction.mt's
Session 2   test construction.mt'sSession 2   test construction.mt's
Session 2 test construction.mt's
 
Cooking ingredients
Cooking ingredientsCooking ingredients
Cooking ingredients
 
Color theory
Color theoryColor theory
Color theory
 

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"); }