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