Presentation is loading. Please wait.

Presentation is loading. Please wait.

BACS 287 Programming Logic 2. BACS 287 Sequence Construct The sequence construct is the default execution mode for the CPU. The instructions are executed.

Similar presentations


Presentation on theme: "BACS 287 Programming Logic 2. BACS 287 Sequence Construct The sequence construct is the default execution mode for the CPU. The instructions are executed."— Presentation transcript:

1 BACS 287 Programming Logic 2

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

3 BACS 287 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.

4 BACS 287 Simple If-Then-Else Structure 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

5 BACS 287 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.

6 BACS 287 ‘AND’ Boolean Logic 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

7 BACS 287 ‘OR’ Boolean Logic 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 4.0 THEN Print “GPA Error” ELSE Print “GPA OK” ENDIF

8 BACS 287 ‘NOT’ Boolean Logic 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

9 BACS 287 ‘XOR’ Boolean Logic 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

10 BACS 287 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

11 BACS 287 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

12 BACS 287 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!

13 BACS 287 Complex Boolean Expressions Another complexity occurs when NOTs are used with comparison operators. NOT(a > b) same as a <= b NOT(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

14 BACS 287 Nested If-Then-Else Structure 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

15 BACS 287 Nested vs. Compound Expression IFs 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

16 BACS 287 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 condition3 then action..... ENDIF

17 BACS 287 ElseIf Structure vs. Compound Expressions 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

18 BACS 287 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.

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

20 BACS 287 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.

21 BACS 287 CASE Selection Structure 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

22 BACS 287 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

23 BACS 287 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.

24 BACS 287 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.

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

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

27 BACS 287 Practice Solution 1A 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

28 BACS 287 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.

29 BACS 287 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

30 BACS 287 Practice Solution 2 alternative Get A,B If A >= 0 and B >= 0 then Print “Both positive” Endif If A < 0 and B < 0 then Print “Both negative” Endif If A >= 0 and B < 0 then Print “Second number negative” Endif If A = 0 then Print “First number negative” EndIf

31 BACS 287 Practice Problem 3 Write the pseudocode to calculate the tax using the ‘single filing status’ table below. $ RangeFormula 0-22,750Income * 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%

32 BACS 287 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

33 BACS 287 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

34 BACS 287 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

35 BACS 287 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…

36 BACS 287 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


Download ppt "BACS 287 Programming Logic 2. BACS 287 Sequence Construct The sequence construct is the default execution mode for the CPU. The instructions are executed."

Similar presentations


Ads by Google