Download presentation
Presentation is loading. Please wait.
Published byKristian Cummings Modified over 8 years ago
1
Copyright ©2005 Department of Computer & Information Science If-Then-Else Structures
2
Copyright ©2005 Department of Computer & Information Science Goals By the end of this lecture you should … Understand how to program a single alternative structure.Understand how to program a single alternative structure. Understand how to program a dual alternative structure.Understand how to program a dual alternative structure. Understand how to program nested conditional structures.Understand how to program nested conditional structures.
3
Copyright ©2005 Department of Computer & Information Science Types of Programming Structures All modern programming languages include support for three basic families of programming structures:All modern programming languages include support for three basic families of programming structures: –Sequential structures –Decision structures –Looping structures
4
Copyright ©2005 Department of Computer & Information Science What is a decision structure? Decision structures consist of:Decision structures consist of: –Some type of T/F test –One or more executable blocks of code Which block of code executes depends on the result of the T/F test (“the condition”).Which block of code executes depends on the result of the T/F test (“the condition”).
5
Copyright ©2005 Department of Computer & Information Science JavaScript Decision Structures JavaScript includes support for three types of decision structures:JavaScript includes support for three types of decision structures: –Single Alternative Decision Structures –Dual Alternative Decision Structures –Multiple Alternative Decision Structures
6
Copyright ©2005 Department of Computer & Information Science Relational Operators To develop valid conditions, we need to use relational operators.To develop valid conditions, we need to use relational operators. OperatorMeaningOperatorMeaning > Greater Than <= Less Than or Equal to >= Greater Than or Equal to ==Equality < Less Than !=Inequality
7
Copyright ©2005 Department of Computer & Information Science Creating Good Conditions Test values that are of the SAME DATA TYPE.Test values that are of the SAME DATA TYPE. Clarify your complex conditions, created with Boolean operators, with parentheses.Clarify your complex conditions, created with Boolean operators, with parentheses.
8
Copyright ©2005 Department of Computer & Information Science Single Alternatives Also called the “If-Then” structure.Also called the “If-Then” structure. Includes a condition and 1 executable block.Includes a condition and 1 executable block. The executable block runs ONLY when the condition evaluates to TRUE; nothing happens for a FALSE.The executable block runs ONLY when the condition evaluates to TRUE; nothing happens for a FALSE.
9
Copyright ©2005 Department of Computer & Information Science Single Alternative – General Form if(condition) { /* executable block goes here */ goes here */ }//end if
10
Copyright ©2005 Department of Computer & Information Science Take the next few minutes to examine the file called ifThenElse_01.html.
11
Copyright ©2005 Department of Computer & Information Science Comparing Strings & Casing When comparing strings, case matters: elephant!=ELEPHANT!=ElephantWhen comparing strings, case matters: elephant!=ELEPHANT!=Elephant When can make comparisons case- insensitive by using the String.toUpperCase() method …When can make comparisons case- insensitive by using the String.toUpperCase() method …
12
Copyright ©2005 Department of Computer & Information Science Take the next few minutes to examine the file called ifThenElse_02.html.
13
Copyright ©2005 Department of Computer & Information Science Dual Alternatives Also called the “If-Then-Else” structure.Also called the “If-Then-Else” structure. Includes a condition and 2 executable blocks – the “if” block and the “else” block.Includes a condition and 2 executable blocks – the “if” block and the “else” block. The “if” block executes when the condition tests to TRUE; the “else” block executes when the condition tests to FALSE.The “if” block executes when the condition tests to TRUE; the “else” block executes when the condition tests to FALSE.
14
Copyright ©2005 Department of Computer & Information Science Dual Alternative – General Form if(condition) { /* true block */ }else{ /* false block */ }//end if
15
Copyright ©2005 Department of Computer & Information Science Take the next few minutes to examine the file called ifThenElse_03.html.
16
Copyright ©2005 Department of Computer & Information Science The Conditional Operator For simple if-then-else branches, JavaScript provides a shortcut called the conditional operator.For simple if-then-else branches, JavaScript provides a shortcut called the conditional operator. General Form:General Form: Condition ? Action If True : Action If False
17
Copyright ©2005 Department of Computer & Information Science Take the next few minutes to examine the file called ifThenElse_04.html.
18
Copyright ©2005 Department of Computer & Information Science Nesting Conditional Structures Sometimes, we need to test more than one factor before we write our executable code. One way to do this is by using nested conditionals …Sometimes, we need to test more than one factor before we write our executable code. One way to do this is by using nested conditionals … By nesting conditional structures, we write ONE COMPLETE conditional structure inside a SINGLE BRANCH of a parent structure.By nesting conditional structures, we write ONE COMPLETE conditional structure inside a SINGLE BRANCH of a parent structure.
19
Copyright ©2005 Department of Computer & Information Science Take the next few minutes to examine the file called ifThenElse_05.html.
20
Copyright ©2005 Department of Computer & Information Science The else if Clause In some cases, we can simplify a nested branch by using an else if clause.In some cases, we can simplify a nested branch by using an else if clause. if(cond1) { //execute if cond1 is TRUE }else if(cond2){ //execute if cond1 is FALSE, //but cond2 is TRUE }else{ //cond1 & cond2 are both FALSE }//end if if(cond1) { //execute if cond1 is TRUE }else if(cond2){ //execute if cond1 is FALSE, //but cond2 is TRUE }else{ //cond1 & cond2 are both FALSE }//end if
21
Copyright ©2005 Department of Computer & Information Science Take the next few minutes to examine the file called ifThenElse_06.html.
22
Copyright ©2005 Department of Computer & Information Science Summary Modern programming languages support sequential, decision and looping structures.Modern programming languages support sequential, decision and looping structures. JavaScript provides support for single alternative if structures and dual alternative if structures.JavaScript provides support for single alternative if structures and dual alternative if structures. Continued …
23
Copyright ©2005 Department of Computer & Information Science Summary We can use the conditional operator in place of simple if-then-else (dual alternative) structures.We can use the conditional operator in place of simple if-then-else (dual alternative) structures. We can nest entire conditional structures inside a single branch of a parent structure.We can nest entire conditional structures inside a single branch of a parent structure.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.