1 / 26

Working with Selection Structures

Working with Selection Structures. Goals. By the end of this lecture, you should understand ... When to use a single-alternative selection structure. When to use a dual-alternative selection structure. How to use relational operators. How to use logical operators.

dpope
Download Presentation

Working with Selection Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Working with Selection Structures

  2. Goals By the end of this lecture, you should understand ... • When to use a single-alternative selection structure. • When to use a dual-alternative selection structure. • How to use relational operators. • How to use logical operators. • When to use a Select-Case structure.

  3. 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: • Sequential Structures • Selection Structures • Looping Structures

  4. Introducing Selection Structures • Single-alternative • A single block of statements to be executed or skipped • Dual-alternative • Two blocks of statements, one of which is to be executed , while the other one is to be skipped • Multiple-alternative • More than two blocks of statements, only one of which is to be executed and the rest skipped.

  5. Single-alternative If something is true Then Do something ( any # of statements)End If If Age >= 18 Set Eligibility = ‘Yes’End if

  6. Dual-alternative If something is true Then Do somethingElse Do something elseEnd If If Age >= 18 set Eligibility = ‘Yes’Else set Eligibility = ‘No’End if

  7. Guidelines • An else condition does not have to exist. Sometimes we only want to do something if something is true • Do 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 taken • Be sure to indent for readability • Do not use the word ‘Then’ after an ‘Else’ • Do use the word ‘Then’ after an ‘Else If’

  8. Relational Operators • 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. Example • The If statement: ‘If A > B Then Write “A is greater than B” End If’ • 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. Logical Operators • 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. • Using compound conditions reduces the amount of code that must be written.

  11. Example Without Logical Operators Input XIf X < 5 Then Write “OK”End IfIf X > 10 Then Write “OK”End If

  12. Example With Logical Operators Input XIf (X < 5) OR (X > 10) Then Write “OK”End If

  13. 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. • This is correct:If (X < 5) OR (X > 10) Then … • This is not correct:If (X < 5 OR > 10) Then …

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

  15. 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 … • 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. The ‘Not’ Operator • ‘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’.

  17. The ‘Not’ Operator • 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. • 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.

  18. 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. • 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. Basic Nested Structure 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

  20. Pseudocode Example If Age >= 18 Then Set Eligibility = ‘Yes’ Else If Age > 15 set Eligibility = ‘Maybe’ Else set Eligibility = “No” End If End if

  21. More on Nesting … • The number of ‘End If’s’ must equal the number of ‘If’s’. • 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. • Regardless of how many possible conditions are included, only one will ever be executed.

  22. Case-type Statements • ‘Case’ or ‘Switch’ statements can be used to more easily code multiple alternative ‘If’s’ • Use the special or keyword ‘Case’. • Use ‘Select’ to start the Case statement. • 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. • Use ‘End Case’ to end the statement • The results will be the same as a correctly code multiple-alternative ‘If’.

  23. Example Select Case of Choice Case 1: Set Ops = ‘Add’ Case 2: Set Ops = ‘Subtract’ Case 3: Set Ops = ‘Multiply’ Case 4: Set Ops = ‘Divide’ End Case

  24. Defensive Programming • Be sure to test your program by ‘playing computer’: • Perform all calculations multiple times manually or with a calculator. • 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.

  25. Questions?

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

More Related