Presentation is loading. Please wait.

Presentation is loading. Please wait.

AdvWeb-1/11 DePaul University SNL 262 Advanced Web Page Design Chapt 7 - Conditional Statements - IV Instructor: David A. Lash.

Similar presentations


Presentation on theme: "AdvWeb-1/11 DePaul University SNL 262 Advanced Web Page Design Chapt 7 - Conditional Statements - IV Instructor: David A. Lash."— Presentation transcript:

1 AdvWeb-1/11 DePaul University SNL 262 Advanced Web Page Design Chapt 7 - Conditional Statements - IV Instructor: David A. Lash

2 AdvWeb-2/11 Chapt 7 - Conditional Statements u Conditional statements are one of the most basic programming constructs. – They provide a way to test for values and status and take proper action – For example: u If phone rings then answer it u If X = 1 then y=2 Stmts exec’ed if condition true if (condition is true) { execute statements between the curly brackets } Test condition

3 AdvWeb-3/11 Chapt 7 - Conditional Example u Here is an example: x=0; if ( x == 1 ) { window.alert("Hey The Value of X is 1"); X = 2; } if ( x == 2 ) { window.alert("Hey The Value of X is 2"); x = 2; } u In reality, if there is only 1 statement to execute, the curly brackets '{' are not required: x=0; if ( x == 0 ) window.alert("Hey The Value of X is 0"); if ( x == 1 ) window.alert("Hey The Value of X is 1");

4 AdvWeb-4/11 Chapt 7 - Conditional Example u Here is an example: This is a title function writeit( invar ) { document.write( "variable=", invar, " " ); } This is the body of the document x=window.prompt(“Enter a number”); if ( x == 1 ) { window.alert("Hey The Value of X is 1"); } if ( x == 2 ) { window.alert("Hey The Value of X is 2”); } window.alert(“The really was value =“, x); See: http://www.depaul.edu/~dlash/extra/Advwebpage/examples/4_example7b. html http://www.depaul.edu/~dlash/extra/Advwebpage/examples/4_example7b. html

5 AdvWeb-5/11 Chapt 7 - Conditional Operators u Here are the conditional operators you can use == equal to if ( x == y) != not equal if (x != y ) < less than if (x < y ) > greater than if (x > y ) <= less than or equal to if (x <= y ) >= grtr than or equal to if (x >= y )

6 AdvWeb-6/11 Conditional Example x=window.prompt("Enter Number"); if ( x!= 0 ) document.write("X != 0\n "); if ( x == 0 ) document.write("X == 0\n "); if ( x "); See: http://www.depaul.edu/~dlash/extra/Advwebpage/examples/4_exa mple7c.html http://www.depaul.edu/~dlash/extra/Advwebpage/examples/4_exa mple7c.html

7 AdvWeb-7/11 The else clause u Used in conjunction with an if statement, instructs JavaScript what to do if condition is not true. x=window.prompt(“Enter Number”); if ( x == 1 ) window.alert("Hey X == 1"); else window.alert("Hey X is not == 1 ");

8 AdvWeb-8/11 The else-if clause u Combining The If-Then-Else Constructs. Instructs JavaScript what to do if condition is not true. x=15; if ( x == 1 ) window.alert("Hey X == 1"); else if ( x == 2 ) window.alert("Hey X is == 2 "); else if (x == 3 ) window.alert("Hey X is == 3 "); else window.alert("Hey X is not any of those "); See : http:// www.depaul.edu/~dlash/extra/Advwebpage/examples/ http:// www.depaul.edu/~dlash/extra/Advwebpage/examples/ 4_example7d.html

9 AdvWeb-9/11 Compound Conditional Can combine multiple if conditional testing using and (&&) and or (||) Compound IF x=window.prompt("Enter Number x="); y=window.prompt("Enter Number y="); if (x == 1 && y ==2) window.alert("Hey X = 1 and y = 2"); else if (x == 3 && y ==4) window.alert("Hey X = 3 and y = 4"); else if (x == 3 && y ==5) window.alert("Hey X = 3 and y = 5"); else window.alert("None Of The Above"); if ( x == 1 || y == 2 ) window.alert("hey either x=1 or y=2"); See : http:// www.depaul.edu/~dlash/extra/Advwebpage/examples/4_example7e.html http:// www.depaul.edu/~dlash/extra/Advwebpage/examples/4_example7e.html

10 AdvWeb-10/11 Switch Statement u Using Switch Statement case example button=window.prompt("Gimme a number"); switch (button) { case "1" : document.write("This is one"); break; case "2" : document.write("This is two"); break; case "3" : document.write("This is three"); break; default : { document.write("None of the above -- going to yahoo"); window.location="http://www.yahoo.com"; } See : http:// www.depaul.edu/~dlash/extra/Advwebpage/examples/4_caseexample.html http:// www.depaul.edu/~dlash/extra/Advwebpage/examples/4_caseexample.html

11 AdvWeb-11/11 An Example Switch Statement where = prompt("Where do you want to go today?"); switch (where) { case "Netscape" : window.location="http://www.netscape.com"; break; case "Microsoft" : window.location="http://www.microsoft.com"; break; case "Yahoo" : window.location="http://www.yahoo.com"; break; default : window.location="http://www.mcp.com"; } See: http:// www.depaul.edu/~dlash/extra/Advwebpage/examples/4_ list7- 5.html http:// www.depaul.edu/~dlash/extra/Advwebpage/examples/4_ list7- 5.html

12 AdvWeb-12/11 An Example Useful Function What's your browser? <!-- Hide script from old browsers if (navigator.appName == "Netscape") { document.write("You're running Netscape Navigator, a fine JavaScript-enabled browser.") } else { document.write("You're not running Netscape Navigator--maybe you should?") } // End hiding script from old browsers --> See: http:// www.depaul.edu/~dlash/extra/Advwebpage/examples/4_ list7- 6.html: http:// www.depaul.edu/~dlash/extra/Advwebpage/examples/4_ list7- 6.html


Download ppt "AdvWeb-1/11 DePaul University SNL 262 Advanced Web Page Design Chapt 7 - Conditional Statements - IV Instructor: David A. Lash."

Similar presentations


Ads by Google