Вы находитесь на странице: 1из 23

Process Control-I

VKS-LEARNING HUB

DECISION
MAKING
Selection

VKS-LEARNING HUB

Decision makingis the most important part of making any


programming or scripting language interactive. While writing a
program, there may be a situation when you need to adopt one out of
a given set of paths. In such cases, you need to use conditional
statements that allow your program to make correct decisions and
perform right actions.
If/if-else
In JavaScript condition or logical expression is used with if-else. ifelse statement provides a way to change program flow based on a
condition. We can have if statement without else but we cannot
have else without if.
Rule 1: if (condition) {
statement / block }

Rule 2: if (condition) { statement1


/ block1 }
else { statement2 / block2 }

VKS-LEARNING HUB

a) If the condition is TRUE then the statement1 or block1 is


executed and the statement or the block after the else is
ignored.
b) If the condition is FALSE then the statement or block after
the condition is ignored and the statement2 or block2 is
executed.

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

The condition can be of any type


The statement can be:
Single statement ending with a semicolon
Block enclosed in braces

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

What if we want to execute multiple


statements in case the condition is true?

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

NOTE: Although the statements within the block end in


semicolons, the block itself doesnt
6

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

Explanation of output: there are 4 single If Conditions are checked


without else in script
First if condition check whether Two Number are equal of not in our
which is FALSE so
document.writeln( "<TR><TD>" + firstNumber + " = " +
secondNumber +
"</TD></TR>" ); is ignored
Second if condition check whether Two Number are not equal which
is TRUE so document.writeln( "<TR><TD>" + firstNumber + " Not
equal to " + secondNumber +
"</TD></TR>" ); is executed
Third If Condition check whether First No is less than Second which
is again FALSE so document.writeln( "<TR><TD>" + firstNumber +

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

You may also compound the statements using else if to


have multiple conditions tested in sequence. You
should use this construction if you want to select one of
many sets of lines to execute.
if ( grade == A )
if (condition_1)
points = 4.0 ;
else {
statement_1
if ( grade == B )
else if (condition_2)
points = 3.0 ;
statement_2]
else {
if ( grade == C )
...
points = 2.0 ;
else if (condition_n_1)
else {
if ( grade == D )
statement_n_1]
points = 1.0 ;
else
else
statement_n
points = 0.0 ;
}
}
}

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

expression" is used to generate a test value that will


be tested against with expected values specified in
"case" clauses. The execution of a "switch case"
statement goes like this:
"case" clauses are possible execution starting
points. Each "case" clause will be visited
sequentially from top to bottom.
If the expected value of a "case" clause equals to the
test value resulted from the switch expression, the
execution starts from those statements listed in this
"case" clause.
Once the execution started, it will continue until a
break statement is reached or the end of the
"switch" block is reached.
The "default" clause is optional. But if it is provided,
it will become the execution starting point when all
case tests before the "default" clause are failed.

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 break statement is


the exit point

This is a case
label

The expression
enclosed in parentheses
is evaluated and
matched with case
labels

The default statement acts like the


else clause in the ifelse structurew
21

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>

Вам также может понравиться