Академический Документы
Профессиональный Документы
Культура Документы
VKS-LEARNING HUB
DECISION
MAKING
Selection
VKS-LEARNING HUB
VKS-LEARNING HUB
c)The
condition
be
:
If there
is no else,can
then
statement
immediately after if is
Boolean variable
executed.
Boolean logical expression
Comparison expression
Integer, object, function everything
VKS-LEARNING HUB
if: Example 1
if ( day == Sunday )
John = Cool ;
The
condition
enclosed in
parenthese
s Set the value of the variable John
semicolo
n
to Cool if the day is equal
to Sunday
VKS-LEARNING HUB
if: Example 2
if ( day == Sunday ) {
John = Cool ;
mood = Great ;
clothing = Casual
;
These
curly braces
group the multiple
}
3.
Note: No semicolon
after the closing curly
brace
statements into a
single compound
statement
VKS-LEARNING HUB
<html>
<head>
</head>
<body>
<script language=JavaScript">
mark=window.prompt("Enter mark:","0")
if(mark>=80)
document.write("Grade="+"A")
if(mark<80)
document.write("Grade="+B")
</script>
Explanation of output: Inputted marks is 85,
</body>
that is, variable marks has a value 85. if
condition is tested (marks>=80), condition is
TRUE. Therefore
document.write("Grade="+"A") is
executed .Condition is tested (marks<80) is False
threfore document.write("Grade="+B")
ignored
Explanation of output: Inputted marks is 70,
that is, variable marks has a value70. if
VKS-LEARNING HUB
<html>
<head>
</head>
<body>
<script language=JavaScript">
mark=window.prompt("Enter mark:","0")
if(mark>=80)
document.write("Grade="+"A")
else document.write("Grade="+B")
</script>
</body>
Explanation of output: Inputted marks is 85,
that is, variable marks has a value 85. if
condition is tested (marks>=80), condition is
TRUE. Therefore
document.write("Grade="+"A") is executed
and the statement after else is ignored.
Explanation of output: Inputted marks is 70,
that is, variable marks has a value70. if
condition is tested (marks>=80), condition is
VKS-LEARNING HUB
<html>
<body>
<SCRIPT LANGUAGE = "JavaScript">
firstNumber = window.prompt( "Enter first Number:", "0" );
secondNumber = window.prompt( "Enter second integer:", "0" );
document.writeln( "<H1>Comparison Output</H1>" );
document.writeln( "<TABLE BORDER = '1' WIDTH = '100%'>" ); //
Creates table
if ( firstNumber == secondNumber )
document.writeln( "<TR><TD>" + firstNumber + " = " +
secondNumber +
"</TD></TR>" ); // Creates rows and columns
if ( firstNumber != secondNumber )
document.writeln( "<TR><TD>" + firstNumber + " Not equal to " +
secondNumber +
"</TD></TR>" );
if ( firstNumber < secondNumber )
document.writeln( "<TR><TD>" + firstNumber + " < " +
secondNumber +
"</TD></TR>" );
if ( firstNumber > secondNumber )
document.writeln( "<TR><TD>" + firstNumber + " > " +
VKS-LEARNING HUB
VKS-LEARNING HUB
Usage of && Operator with if-else
Rule:
if (Condition1 && Condition2 [&& Condition3 ])
{ Statement1 / Block1 }
else
{ Statement2 / Block2 }
VKS-LEARNING HUB
<html>
<head>
<title>&& operator with if -else</title>
</head>
<body>
<script type="text/javascript">
a=window.prompt("Enter First Angle,"0");
b=window.prompt("Enter Second Angle","0");
c=window.prompt("Enter Third Angle","0");
if (a==60 && b ==60 && c ==60 )
alert(" Equilateral Triangle")
else
alert(" Not a Equilateral Triangle")
</script>
</body>
</html>
</html>
VKS-LEARNING HUB
<html>
<head>
<title>Using the && opertor with if -else</title>
</head>
<body>
<script type="text/javascript">
a=window.prompt("Enter First Value","0");
b=window.prompt("Enter Second Value","0");
c=window.prompt("Enter Third Value","0");
if (a>=b && a>=c)
max=a;
if (b>=a && b>=c)
max=b;
if (c>=a && c>=b)
max=c;
document.write( 3 Nos="+a+","+b+","+c +"<br>")
document.write(" Highest no="+ max)
</script>
</body>
</html>
</html>
VKS-LEARNING HUB
Usage of || Operator with if-else
Rule:
if (Condition1 || Condition2 [|| Condition3 ])
{ Statement1 / Block1 }
else
{ Statement2 / Block2 }
VKS-LEARNING HUB
<html>
<head>
<title>
Using the || operator with if else
</title>
</head>
<body>
<script type="text/javascript">
m=window.prompt("Enter
Marks[0-100]");
if (m<0 || m>100)
alert("Input error, Enter Marks[0100])
else
alert(" Valid input" + "\nMarks=
"+m)
</script>
</body>
</html>
</html>
VKS-LEARNING HUB
Nested if Statements
VKS-LEARNING HUB
VKS-LEARNING HUB
<html> <head>
<title> Using Nested if else </title>
</head> <body>
<script type="text/javascript">
day=window.prompt("Enter Day[0-6]");
if (day == 0 )
{ alert ("Sunday") ; }
else if (day == 1 )
{ alert ("Monday") ; }
else if (day == 2)
{ alert ("Tuesday") ; }
else if (day == 3)
{ alert ("Wednesday") ; }
else if (day == 4)
{ alert ("Thursday") ; }
else if (day == 5)
{ alert ("Friday") ; }
else if (day == 6)
{ alert ("Saturday") ; }
else { alert ("Error - invalid day.") ; }
</script>
</body> </html>
VKS-LEARNING HUB
SWITCH CASE
VKS-LEARNING HUB
SWITCH
CASE
switch case" statements are alternative ways of
executing statements selectively based on certain
conditions.
Syntax
switch (expression )
{ case value1 :
statement(s)
break ;
case value2 :
statement(s)
break ;
...
default :
statement(s)
break ; }
VKS-LEARNING HUB
switch ( grade ) {
case A :
points = 4.0 ;
break ;
case B :
points = 3.0 ;
break ;
case C :
points = 2.0;
break ;
case D :
points = 1.0 ;
break ;
default :
points = 0.0 ;
}
A colon
following the
case label is
required
This is a case
label
The expression
enclosed in parentheses
is evaluated and
matched with case
labels
VKS-LEARNING HUB
<html> <head>
<title> Using Nested if else </title>
</head> <body>
<script type="text/javascript">
day=window.prompt("Enter Day[0-6]");
switch(day )
{ case 0 : alert ("Sunday") ; break;
case 1 : alert ("Monday") ; break;
case 2 : alert (Tuesday") ; break;
case 3 : alert (Wednesday") ; break;
case 4 : alert (" Thursday ") ; break;
case 5 : alert (Friday") ; break;
case 6 : alert (Saturday") ; break;
default: alert ("Error - invalid day.") ;
break;
}
</script>
</body> </html>
VKS-LEARNING HUB
The break statements indicate the end of a particular case. If they were
omitted, the interpreter would continue executing each statement in
each of the following cases
<html>
<head>
<title> Switch without Break</title>
</head>
<body>
<script type="text/javascript">
day=window.prompt("Enter Day[0-6]");
switch(day )
{ case 0 : document.write ("Sunday") ;
case 1 : document.write ("Monday") ;
case 2 : document.write (Tuesday") ;
case 3 : document.write (Wednesday") ;
case 4 : document.write (" Thursday ") ;
case 5 : document.write (Friday") ;
case 6 : document.write (Saturday") ;
default: document.write ("Error - invalid day.") ;
}
</script>
</body> </html>