1 / 35

BACS 287

BACS 287. Programming Logic 2. Sequence Construct. The sequence construct is the default execution mode for the CPU. The instructions are executed in the order they are encountered. Selection Construct. The selection construct is used to determine which logic branch to follow.

karsen
Download Presentation

BACS 287

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. BACS 287 Programming Logic 2 BACS 287

  2. Sequence Construct • The sequence construct is the default execution mode for the CPU. • The instructions are executed in the order they are encountered. BACS 287

  3. Selection Construct • The selection construct is used to determine which logic branch to follow. • The 2 basic forms of this construct are: • IF-ELSE-ENDIF • CASE • Several variations are possible within each type and equivalent logic can be expressed in different ways. BACS 287

  4. IF condition THEN true action1 ... true action n ELSE false action1 ... false action n ENDIF IF GPA > 4.0 THEN Print “GPA in Error” ELSE Print “GPA not over 4.0” ENDIF IF GPA > 3.3 THEN Print “Dean’s List” ENDIF Simple If-Then-Else Structure BACS 287

  5. Simple If-Then-Else Structure • The condition statement can be any valid statement that evaluates to True or False. • The condition can be compound and connected by the logical operators (And, Or, Not, Xor). • Evaluation of the condition follows the rules of Boolean Logic. BACS 287

  6. The ‘AND’ operator returns true only if both inputs are true 1 0 1 1 0 0 0 0 IF a=1 AND b=2 THEN Print “Both true” ELSE Print “Both not true” ENDIF IF gpa >= 0.0 AND gpa <= 4.0 THEN Print “GPA OK” ELSE Print “GPA Not OK” ENDIF ‘AND’ Boolean Logic BACS 287

  7. The ‘OR’ operator returns true if either input is true 1 0 1 1 1 0 1 0 IF a=1 OR b=5 THEN Print “It’s 1 or 5 or both” ELSE Print “It’s neither” ENDIF IF gpa<0.0 or gpa >4.0 THEN Print “GPA Error” ELSE Print “GPA OK” ENDIF ‘OR’ Boolean Logic BACS 287

  8. A ‘NOT’ Boolean operator inverts the condition evaluation NOT(1) = 0 NOT(0) = 1 - same as - NOT(True) = False NOT(False) = True IF NOT(a=1) then Print “A is not 1” ELSE Print “A is 1” ENDIF IF NOT End-Of-File THEN Read a record ELSE Close File ENDIF ‘NOT’ Boolean Logic BACS 287

  9. The ‘XOR’ operator returns true if only one input is true. False is returned otherwise 1 0 1 0 1 0 1 0 IF a=1 XOR b=5 THEN Print “It’s 1 or 5, not both” ELSE Print “It’s both or none” ENDIF IF class=“JR” XOR class=“SR” THEN Print “Its JR or SR” ELSE Print “neither” ENDIF ‘XOR’ Boolean Logic BACS 287

  10. Complex Boolean Expressions • Boolean expressions can be combined in complex ways. For example: • IF (a=1 and b=6) or (a=2 and b=3) then... • IF (ACT > 21 or SAT>900) and gpa>2.0 or gpa > 3.0 then... • Use () to determine order of evaluation • If () are not used, then NOT comes first followed by AND then OR • Equal logical operators are evaluated left to right within the expression BACS 287

  11. Complex Boolean Expressions • When a NOT Boolean operator is combined with AND and OR then De Morgan’s rule comes into play. • De Morgan’s rule: NOT(c OR d) = NOT c AND NOT d NOT(c AND d) = NOT c OR NOT d BACS 287

  12. De Morgan’s Rule Example NOT(c OR d) = NOT c AND NOT d Given: c = True and d = False not(T or F) = not T and not F not(T) = F and T F = F √ correct • It also works for the other 3 possible cases. Try it and see for yourself! BACS 287

  13. Complex Boolean Expressions • Another complexity occurs when NOTs are used with comparison operators. NOT(a > b) same as a <= b NOT(a < b) same as a >= b NOT(a = b) same as a <> b NOT(a <> b) same as a = b • The Moral: Be careful when you use the NOT operator BACS 287

  14. IF condition1 THEN IF condition2 THEN action 1 ELSE action 2 ENDIF ELSE action 3 ENDIF IF a=5 THEN IF b=2 THEN Print “a = 5, b = 2” ELSE Print “a = 5, b <> 2” ENDIF ELSE Print “a <> 5, b = ?” ENDIF Nested If-Then-Else Structure BACS 287

  15. Nested Code IF status=“Senior” Then IF hours>110 Then Print “2nd Sem. SR” Else Print “1st Sem. SR” ENDIF ELSE Print “Not a SR” ENDIF Compound Code IF status=“Senior” and hours > 110 then Print “2nd Sem. SR” ENDIF IF status = “Senior” and hours <= 110 then Print “1st Sem. SR” ENDIF IF status<>“Senior” then Print “Not a SR” ENDIF Nested vs. Compound Expression IFs BACS 287

  16. ElseIf Structure • There is a compromise structure that combines the ELSE and IF lines into a single statement. This is the ELSEIF structure. IF condition1 then action ELSEIF condition2 then action ELSEIF condition3then action ..... ENDIF BACS 287

  17. Compound Code IF status=“Senior” and hours > 110 then Print “2nd Sem. SR” ENDIF IF status = “Senior” and hours <= 110 then Print “1st Sem. SR” ENDIF IF status<>“Senior” then Print “Not a SR” ENDIF ELSEIF Code IF status=“Senior” and hours > 110 then Print “2nd Sem.SR” ELSEIF status=“Senior” and Hours <= 110 then Print “1st Sem. SR” ELSE Print “Not a SR” ENDIF ElseIf Structure vs. Compound Expressions BACS 287

  18. ElseIf Structure • The ELSEIF structure allows you to test a range of possibilities without the complexity of nesting. • In practice, only 1 branch of the ELSEIF is executed, so it is equivalent to a multi-branch IF statement. • Another multi-branch selection is the CASE statement. BACS 287

  19. IF Statement Summary • Simple If-Then-Else • Nested If-Then-Else • Compound If statements • If-ElseIf structure • All IFs can use complex Boolean logic BACS 287

  20. CASE Selection Structure • The CASE selection structure allows you to build a flexible multi-branch IF statement without the complexity of deeply nested IF groups. • The CASE is functionally equivalent to the ELSEIF, but normally preferred because it is more efficient and better documents the multi-branch nature of the logic. BACS 287

  21. SELECT CASE test-exp CASE expression1 action ... CASE expression2 action ... CASE ELSE action ... ENDSELECT SELECT CASE Color CASE “red” Print “red car” CASE “blue” Print “blue car” CASE “green” Print “green car” CASE Else Print “unknown color” ENDSELECT CASE Selection Structure BACS 287

  22. CASE Structure Example Get Input-Value Select Case Input-Value Case 1,2,5 Print “It’s either 1, 2, or 5” Case 7 to 14 Print “It’s in the range of 7 thru 14 inclusive” Case 15, 17, 19 to 25 Print “Either 15, 17, or 19 thru 25 inclusive” Case > 25 Print “It’s greater than 25” EndSelect BACS 287

  23. If-ElseIf vs. Case Structure • In some situations, the two structures are interchangeable; however, this is not always true. • The condition of an IF statement can be complex and evaluates to True or False (only). • The test-expression of the Case structure is a simple expression that evaluates to a value that is matched within the Case statement. BACS 287

  24. Practice Problem 1 • Write the pseudocode to accept the temperature and print a message telling if it is above or below freezing. You should assume that freezing is anything equal to or below 32 degrees. BACS 287

  25. Practice Solution 1 Get Temp If Temp > 32 then Print “It is above freezing” Else Print “It is equal to or below freezing” EndIf BACS 287

  26. Practice Problem 1A • Modify the previous problem to display a 3rd message if the temperature is below 0. BACS 287

  27. Get Temp If Temp > 32 then Print “Above freezing” ElseIf Temp >= 0 Print “Below freezing” Else Print “It is below 0” EndIf Get Temp Select Case Temp Case > 32 Print “Above Freezing” Case >= 0 Print “Below Freezing” Case Else Print “It is below 0” EndSelect Practice Solution 1A BACS 287

  28. Practice Problem 2 • Write the pseudocode to get 2 numbers from the user and print a message that tells if both were positive, both negative, the 1st negative (only), or the 2nd negative (only). Use good structured technique. Assume that positive is defined as 0 or above. BACS 287

  29. Practice Solution 2 Get A,B If A >= 0 then If B >= 0 then Print “Both positive” Else Print “Second number negative” EndIf ElseIf B < 0 then Print “Both negative” Else Print “First number negative” EndIf BACS 287

  30. Practice Problem 3 Write the pseudocode to calculate the tax using the ‘single filing status’ table below. $ RangeFormula 0-22,750 Income * 15% 22,751-55,100 $3,412 + Income * 28% 55,101-115,000 $12,470 + Income * 31% 115,001-250,000 $31,039 + Income * 36% > 250,000 $79,639 + Income * 39.6% BACS 287

  31. Practice Solution 3 Read Income If Income <= 22750 then Tax = Income * .15 ElseIf Income <= 55100 then Tax = 3412 + ((Income - 22750) * .28) ElseIf Income <= 115000 then Tax = 12470 + ((Income - 55100) * .31) ElseIf Income <= 250000 then Tax = 31039 + ((Income - 115000) * .36) Else Tax = 79639 + ((Income - 250000) * .396) Endif Print Tax BACS 287

  32. Practice Solution 3A Read Income Select Case Income Case <= 22750 Tax = Income * .15 Case <= 55100 Tax = 3412 + ((Income - 22750) * .28) Case <= 115000 Tax = 12470 + ((Income - 55100) * .31) Case <= 250000 Tax = 31039 + ((Income - 115000) * .36) Case Else Tax = 79639 + ((Income - 250000) * .396) EndCase Print Tax BACS 287

  33. Would This Work? Read Income Select Case Income Case > 250000 Tax = 79639 + ((Income - 250000) * .396) Case <= 250000 Tax = 31039 + ((Income - 115000) * .36) Case <= 115000 Tax = 12470 + ((Income - 55100) * .31) Case <= 55100 Tax = 3412 + ((Income - 22750) * .28) Case Else Tax = Income * .15 EndCase Print Tax BACS 287

  34. Practice Problem 4 Get a series of numbers from the user. When the user enters -999 stop collecting numbers. Print the largest positive number entered. Hint: This uses an iteration construct too… BACS 287

  35. Practice Solution 4 max_value = 0 Get a number Do While number <> -999 If number > max_value then max_value = number EndIf Get a number EndDo Print max_value BACS 287

More Related