bacs 287 programming logic 2. bacs 287 sequence construct the sequence construct is the default...

36
BACS 287 BACS 287 Programming Logic 2

Upload: joleen-watts

Post on 20-Jan-2016

237 views

Category:

Documents


0 download

TRANSCRIPT

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

BACS 287

BACS 287

Programming Logic 2

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

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.

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

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.

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

BACS 287

Simple If-Then-Else Structure

IF condition THENtrue action1

...

true action n

ELSEfalse 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

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

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.

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

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 THENPrint “Both true”

ELSEPrint “Both not true”

ENDIF

IF gpa >= 0.0 AND gpa <= 4.0 THEN

Print “GPA OK”ELSE

Print “GPA Not OK”ENDIF

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

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 THENPrint “It’s 1 or 5 or both”

ELSEPrint “It’s neither”

ENDIF

IF gpa<0.0 or gpa >4.0 THEN

Print “GPA Error”

ELSE

Print “GPA OK”

ENDIF

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

BACS 287

‘NOT’ Boolean Logic

A ‘NOT’ Boolean operator inverts the condition evaluation

NOT(1) = 0NOT(0) = 1

- same as -

NOT(True) = FalseNOT(False) = True

IF NOT(a=1) thenPrint “A is not 1”

ELSEPrint “A is 1”

ENDIF

IF NOT End-Of-File THEN

Read a record

ELSE

Close File

ENDIF

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

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 THENPrint “It’s 1 or 5, not both”

ELSEPrint “It’s both or none”

ENDIF

IF class=“JR” XOR class=“SR” THEN

Print “Its JR or SR”ELSE

Print “neither”ENDIF

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

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

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

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 dNOT(c AND d) = NOT c OR NOT d

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

BACS 287

De Morgan’s Rule Example

NOT(c OR d) = NOT c AND NOT d

Given: c = True and d = Falsenot(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!

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

BACS 287

Complex Boolean Expressions

Another complexity occurs when NOTs are used with comparison operators.NOT(a > b) same as a <= bNOT(a < b) same as a >= bNOT(a = b) same as a <> bNOT(a <> b) same as a = b

The Moral: Be careful when you use the NOT operator

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

BACS 287

Nested If-Then-Else Structure

IF condition1 THENIF condition2 THEN

action 1ELSE

action 2

ENDIFELSE

action 3ENDIF

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

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

BACS 287

Nested vs. Compound Expression IFs

Nested CodeIF status=“Senior” Then

IF hours>110 Then

Print “2nd Sem. SR”

Else

Print “1st Sem. SR”

ENDIF

ELSE

Print “Not a SR”

ENDIF

Compound CodeIF status=“Senior”

and hours > 110 thenPrint “2nd Sem. SR”

ENDIFIF status = “Senior”

and hours <= 110 thenPrint “1st Sem. SR”

ENDIFIF status<>“Senior” then

Print “Not a SR”ENDIF

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

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 thenaction

ELSEIF condition3 thenaction

.....ENDIF

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

BACS 287

ElseIf Structure vs. Compound Expressions

Compound CodeIF status=“Senior”

and hours > 110 thenPrint “2nd Sem. SR”

ENDIFIF status = “Senior”

and hours <= 110 thenPrint “1st Sem. SR”

ENDIFIF status<>“Senior” then

Print “Not a SR”ENDIF

ELSEIF CodeIF 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

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

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.

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

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

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

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.

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

BACS 287

CASE Selection Structure

SELECT CASE test-expCASE expression1

action...

CASE expression2action...

CASE ELSEaction...

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

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

BACS 287

CASE Structure Example

Get Input-ValueSelect Case Input-Value

Case 1,2,5Print “It’s either 1, 2, or 5”

Case 7 to 14Print “It’s in the range of 7 thru 14 inclusive”

Case 15, 17, 19 to 25Print “Either 15, 17, or 19 thru 25 inclusive”

Case > 25Print “It’s greater than 25”

EndSelect

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

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.

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

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.

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

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

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

BACS 287

Practice Problem 1A

Modify the previous problem to display a 3rd message if the temperature is below 0.

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

BACS 287

Practice Solution 1A

Get TempIf 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 > 32Print “Above Freezing”

Case >= 0Print “Below Freezing”

Case Else

Print “It is below 0”

EndSelect

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

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.

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

BACS 287

Practice Solution 2

Get A,BIf A >= 0 then

If B >= 0 thenPrint “Both positive”

ElsePrint “Second number negative”

EndIfElseIf B < 0 then

Print “Both negative”Else

Print “First number negative”EndIf

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

BACS 287

Practice Solution 2 alternative

Get A,BIf A >= 0 and B >= 0 then

Print “Both positive”EndifIf A < 0 and B < 0 then

Print “Both negative”EndifIf A >= 0 and B < 0 then

Print “Second number negative”EndifIf A < 0 and B >= 0 then

Print “First number negative”EndIf

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

BACS 287

Practice Problem 3

Write the pseudocode to calculate the tax using the ‘single filing status’ table below. $ Range Formula

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%

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

BACS 287

Practice Solution 3

Read IncomeIf Income <= 22750 then

Tax = Income * .15ElseIf 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)EndifPrint Tax

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

BACS 287

Practice Solution 3A

Read IncomeSelect Case Income

Case <= 22750Tax = Income * .15

Case <= 55100Tax = 3412 + ((Income - 22750) * .28)

Case <= 115000 Tax = 12470 + ((Income - 55100) * .31)

Case <= 250000 Tax = 31039 + ((Income - 115000) * .36)

Case ElseTax = 79639 + ((Income - 250000) * .396)

EndCasePrint Tax

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

BACS 287

Would This Work?

Read IncomeSelect 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 * .15EndCasePrint Tax

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

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…

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

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