+ All Categories
Home > Documents > JAVA SCRIPT Processcontorl

JAVA SCRIPT Processcontorl

Date post: 12-Feb-2016
Category:
Upload: vinod-srivastava
View: 223 times
Download: 0 times
Share this document with a friend
Description:
JAVA SCRIPT Processcontorl
23
Process Control-I
Transcript
Page 2: JAVA SCRIPT Processcontorl

VKS-LEARNING HUB

DECISION MAKINGSelection

Page 3: JAVA SCRIPT Processcontorl

VKS-LEARNING HUB

Decision making is 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-elseIn JavaScript condition or logical expression is used with if-else. if-else 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 }

Page 4: JAVA SCRIPT Processcontorl

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) If there is no else, then statement immediately after if is executed.

•The condition can be:Boolean variableBoolean logical expressionComparison expressionInteger, object, function… everything

•The condition can be of any type•The statement can be:

Single statement ending with a semicolonBlock enclosed in braces

Page 5: JAVA SCRIPT Processcontorl

VKS-LEARNING HUB

5

if: Example 1if ( day == “Sunday” ) John = “Cool” ;

The condition enclosed in parentheses semicolon

Set the value of the variable ‘John to ‘Cool’ if the ‘day’ is equal to ‘Sunday’

What if we want to execute multiple statements in case the condition is true?

Page 6: JAVA SCRIPT Processcontorl

VKS-LEARNING HUB

6

if: Example 2if ( day == “Sunday” ) {

John = “Cool” ;mood = “Great” ;clothing = “Casual” ;

}These curly braces group the multiple statements into a single compound statement

Note: No semicolon after the closing curly brace

3. NOTE: Although the statements within the block end in semicolons, the block itself doesn’t

Page 7: JAVA SCRIPT Processcontorl

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></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 .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 condition is tested (marks>=80), condition is FALSE therefore document.write("Grade="+“A") is ignored Condition (mark<80) is TRUE therefor document.write("Grade="+“B") is executed.

Page 8: JAVA SCRIPT Processcontorl

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 FALSE. Therefore document.write("Grade="+“A") is ignored and the statement after else, document.write("Grade="+“B") is executed.

Page 9: JAVA SCRIPT Processcontorl

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 tableif ( firstNumber == secondNumber ) document.writeln( "<TR><TD>" + firstNumber + " = " + secondNumber + "</TD></TR>" ); // Creates rows and columnsif ( 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 + " > " + secondNumber +"</TD></TR>" );document.writeln( "</TABLE>" );</SCRIPT></body> <html>

Page 10: JAVA SCRIPT Processcontorl

VKS-LEARNING HUB

Explanation of output: there are 4 single If Conditions are checked without else in scriptFirst 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 executedThird If Condition check whether First No is less than Second which is again FALSE so document.writeln( "<TR><TD>" + firstNumber + " < " + secondNumber +"</TD></TR>" ); is ignoredFourth If Condition check whether First No is greater than Second which is again TRUE so document.writeln( "<TR><TD>" + firstNumber + " > " + secondNumber +"</TD></TR>" ); is executed

Page 11: JAVA SCRIPT Processcontorl

VKS-LEARNING HUB

Usage of && Operator with if-else

Rule:if (Condition1 && Condition2 [&& Condition3 … ]){ Statement1 / Block1 }else { Statement2 / Block2 }

Page 12: JAVA SCRIPT Processcontorl

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")elsealert(" Not a Equilateral Triangle")</script></body></html></html>

Page 13: JAVA SCRIPT Processcontorl

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>

Page 14: JAVA SCRIPT Processcontorl

VKS-LEARNING HUB

Usage of || Operator with if-else

Rule:if (Condition1 || Condition2 [|| Condition3 … ]){ Statement1 / Block1 }else { Statement2 / Block2 }

Page 15: JAVA SCRIPT Processcontorl

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[0-100]“)elsealert(" Valid input" + "\nMarks= "+m)</script></body></html></html>

Page 16: JAVA SCRIPT Processcontorl

VKS-LEARNING HUB

Nested if Statements

Page 17: JAVA SCRIPT Processcontorl

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 (condition_1) statement_1else if (condition_2) statement_2]...else if (condition_n_1) statement_n_1]else statement_n

if ( grade == “A” )points = 4.0 ;

else {if ( grade == “B” )

points = 3.0 ;else {

if ( grade == “C” )points = 2.0 ;

else { if ( grade == “D” )

points = 1.0 ;else points = 0.0 ;

}}

}

Page 18: JAVA SCRIPT Processcontorl

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>

Page 19: JAVA SCRIPT Processcontorl

VKS-LEARNING HUB

SWITCH CASE

Page 20: JAVA SCRIPT Processcontorl

VKS-LEARNING HUB

switch case" statements are alternative ways of executing statements selectively based on certain conditions.

Syntaxswitch (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.

SWITCH CASE

Page 21: JAVA SCRIPT Processcontorl

VKS-LEARNING HUB

21

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 ;

}

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

This is a case labelA colon

following the case label is required

This ‘break’ statement is the exit point

The ‘default’ statement acts like the ‘else’ clause in the ‘if…else’ structurew

Page 22: JAVA SCRIPT Processcontorl

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>

Page 23: JAVA SCRIPT Processcontorl

VKS-LEARNING HUB

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

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


Recommended