Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Working with Selection Structures.

Similar presentations


Presentation on theme: "CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Working with Selection Structures."— Presentation transcript:

1 CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Working with Selection Structures

2 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Goals By the end of this lecture, you should understand... When to use a single-alternative selection structure.When to use a single-alternative selection structure. When to use a dual-alternative selection structure.When to use a dual-alternative selection structure. How to use relational operators.How to use relational operators. How to use logical operators.How to use logical operators. When to use a Select-Case structure.When to use a Select-Case structure.

3 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science The 3 “Families” of Programming Structures Remember that we can choose from any of three families of programming structures to help us manipulate data to make meaningful information:Remember that we can choose from any of three families of programming structures to help us manipulate data to make meaningful information: –Sequential Structures –Selection Structures –Looping Structures

4 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Introducing Selection Structures Single-alternativeSingle-alternative –A single block of statements to be executed or skipped Dual-alternativeDual-alternative – Two blocks of statements, one of which is to be executed, while the other one is to be skipped Multiple-alternativeMultiple-alternative –More than two blocks of statements, only one of which is to be executed and the rest skipped.

5 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Single-alternative If something is true Then Do something ( any # of statements) End If If Age >= 18 Set Eligibility = ‘Yes’ End if

6 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Dual-alternative If something is true Then Do something Else Do something else End If If Age >= 18 set Eligibility = ‘Yes’ Else set Eligibility = ‘No’ End if

7 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Guidelines An else condition does not have to exist. Sometimes we only want to do something if something is trueAn else condition does not have to exist. Sometimes we only want to do something if something is true Do not manufacture alternative conditionsDo not manufacture alternative conditions In an If statement, the body of the if is executed if, and only if, the test condition is true.Otherwise, no action is takenIn an If statement, the body of the if is executed if, and only if, the test condition is true.Otherwise, no action is taken Be sure to indent for readabilityBe sure to indent for readability Do not use the word ‘Then’ after an ‘Else’Do not use the word ‘Then’ after an ‘Else’ Do use the word ‘Then’ after an ‘Else If’Do use the word ‘Then’ after an ‘Else If’

8 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Relational Operators Relational operators are the symbols used in the condition to be evaluated in If statements:Relational operators are the symbols used in the condition to be evaluated in If statements: = equal to <> not equal to < less than > greater than <= less than or equal to >= greater than or equal to

9 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Example The If statement:The If statement: ‘If A > B Then Write “A is greater than B” Write “A is greater than B” End If’ You can read the if statement above like this:You can read the if statement above like this: “ If it is true that A is greater than B, then write the words ‘A is greater than B’ to the screen.”

10 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Logical Operators Logical operators are used to connect simple conditions into a more complex condition called a ‘compound’ condition.Logical operators are used to connect simple conditions into a more complex condition called a ‘compound’ condition. The simple conditions each contain one relational operator.The simple conditions each contain one relational operator. Using compound conditions reduces the amount of code that must be written.Using compound conditions reduces the amount of code that must be written.

11 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Example Without Logical Operators Input X If X 10 Then Write “OK” End If

12 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Example With Logical Operators Input X If (X 10) Then Write “OK” End If

13 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Building Compound Conditions In a compound condition, it is necessary to use complete simple conditions. Test if each phrase in the compound condition can stand on its own.In a compound condition, it is necessary to use complete simple conditions. Test if each phrase in the compound condition can stand on its own. This is correct: If (X 10) Then …This is correct: If (X 10) Then … This is not correct: If (X 10) Then …This is not correct: If (X 10) Then …

14 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science The ‘And’ Operator A compound condition consisting of two simple conditions joined by an ‘And’ is true only if both simple conditions are true. It is false if even one of the conditions is false. If (X > 5) AND (X 5) AND (X<10) Then … Is true only if X is 6, 7, 8, or 9. It has to be both greater than 5 and less than 10 at the same time.Is true only if X is 6, 7, 8, or 9. It has to be both greater than 5 and less than 10 at the same time.

15 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science The ‘Or’ Operator A compound condition consisting of two simple conditions joined by an ‘Or’ is true if even one of the simple conditions is true. It is false only if both are false. If (Response = “Y”) OR (Response = “y”) Then …A compound condition consisting of two simple conditions joined by an ‘Or’ is true if even one of the simple conditions is true. It is false only if both are false. If (Response = “Y”) OR (Response = “y”) Then … Is true if Response is uppercase or lower case “y’. For the above condition to be false, Response would have to be something other than ‘y’.Is true if Response is uppercase or lower case “y’. For the above condition to be false, Response would have to be something other than ‘y’.

16 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science The ‘Not’ Operator ‘And’ and ‘Or’ affect 2 simple conditions.‘And’ and ‘Or’ affect 2 simple conditions. ‘Not’ affects only one condition.If you need to negate more than one simple condition, you will need more than one ‘Not’.‘Not’ affects only one condition.If you need to negate more than one simple condition, you will need more than one ‘Not’.

17 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science The ‘Not’ Operator A condition with the ‘Not’ operator is true only if the condition is false.A condition with the ‘Not’ operator is true only if the condition is false. –Not (A < B) is true only if B is greater than or equal to A.is true only if B is greater than or equal to A. –If (X > 100) And Not ( X = Y) Then… is true only if X is greater than 100 but not equal to the value of Y. is true only if X is greater than 100 but not equal to the value of Y.

18 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Using Nested Structures Sometimes, we must handle more than two options in a program. In many cases, we can use nested structures to accomplish this.Sometimes, we must handle more than two options in a program. In many cases, we can use nested structures to accomplish this. In a nested structure, we build a child structure inside the one branch (either the if branch or the else branch) or a parent structure. The child structure is completely enclosed inside a single branch.In a nested structure, we build a child structure inside the one branch (either the if branch or the else branch) or a parent structure. The child structure is completely enclosed inside a single branch.

19 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science If Statement 1 is TRUE … Perform Action A Else If Statement 2 is TRUE … Perform Action B Else Perform Action C End If End if Basic Nested Structure

20 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science If Age >= 18 Then Set Eligibility = ‘Yes’ Else If Age > 15 set Eligibility = ‘Maybe’ Else set Eligibility = “No” End If End if Pseudocode Example

21 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science More on Nesting … The number of ‘End If’s’ must equal the number of ‘If’s’.The number of ‘End If’s’ must equal the number of ‘If’s’. You can draw a line to connect them to check.You can draw a line to connect them to check. In the previous example, the check for age 5 will never be done if the age is > 18.In the previous example, the check for age 5 will never be done if the age is > 18. Regardless of how many possible conditions are included, only one will ever be executed.Regardless of how many possible conditions are included, only one will ever be executed.

22 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Case-type Statements ‘Case’ or ‘Switch’ statements can be used to more easily code multiple alternative ‘If’s’‘Case’ or ‘Switch’ statements can be used to more easily code multiple alternative ‘If’s’ Use the special or keyword ‘Case’.Use the special or keyword ‘Case’. Use ‘Select’ to start the Case statement.Use ‘Select’ to start the Case statement. Specify the expression to be evaluated.Specify the expression to be evaluated. List each possible value of the expression and what to do if that is that value is the true one.List each possible value of the expression and what to do if that is that value is the true one. Use ‘End Case’ to end the statementUse ‘End Case’ to end the statement The results will be the same as a correctly code multiple-alternative ‘If’.The results will be the same as a correctly code multiple-alternative ‘If’.

23 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Example Select Case of Choice Case 1: Set Ops = ‘Add’ Case 2: Case 2: Set Ops = ‘Subtract’ Case 3: Case 3: Set Ops = ‘Multiply’ Case 4: Case 4: Set Ops = ‘Divide’ End Case

24 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Defensive Programming Be sure to test your program by ‘playing computer’:Be sure to test your program by ‘playing computer’: Perform all calculations multiple times manually or with a calculator.Perform all calculations multiple times manually or with a calculator. Make sure each branch of each selection structure is executed at least once.Make sure each branch of each selection structure is executed at least once. Check for division by zero, negative values for a square root function, and any other special conditions.Check for division by zero, negative values for a square root function, and any other special conditions.

25 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Questions?

26 Resources Venit, Stewart. Extended Prelude to Programming: Concepts and Design. Scott/Jones, Inc., 2002.Venit, Stewart. Extended Prelude to Programming: Concepts and Design. Scott/Jones, Inc., 2002.


Download ppt "CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Working with Selection Structures."

Similar presentations


Ads by Google