chapter 4: descision structures and boolean logic

advertisement

CHAPTER 4: DESCISION STRUCTURES AND BOOLEAN LOGIC

4.1 Introduction to Decision Structures

4.2 Dual Alternative Decision Structures

4.3 Comparing Strings

4.4 Nested Decision Structures

4.5 The Case Structure

4.6 Logical Operators

4.7 Boolean Variables

4.1 Introduction to Decision Structures

Remember: sequence structure – goes in order, top to bottom p. 121 pseudocode

A decision (aka selection) structure allows a program to perform actions only under certain conditions

EXAMPLE: >40 hours for payroll

Different types of decisions include

If, also called single alternative (see coat example below)

If then else, also called dual alternative

Case structure for multiple alternative decisions

The if statement

Evaluate condition action only occurs if the decision is true

If condition Then

Statement

Statement

End If

A diamond symbol is used in flowcharts

Align IF and END IF; indent other statements as needed (page 128)

Combine sequence structures and decision structures (Figure 4-2)

Get temp Wear coat? Go outside

Sequence structures can be nested in decision structures (Figure 4-3)

Coat Hat Gloves

Build Single Decision (Cold / Wear Jacket) Raptor file.

Relational Operators

Determines whether a specific relationship exists between two values >40 = Pay overtime

Used within the condition, a Boolean expression true or false IF statements

• x > y x<y x >=y x<y x <=y x==y x!=y (Table 4-1 p. 125)

== is a comparison operator = is an assignment operator set bonus = 500

!= is written as <> in VisualBasic

Figures 4-4 through 4-6

Program 4-1 on page 129 is available in pseudocode as in the book AND as QBasic code at the following link: http://www.3mit.com/rcc/cis115/ch04qb.pdf

These examples will help you learn how to convert the pseudocode to syntax specifically for QBasic.

Here is a list of QBasic commands: http://www.freewebs.com/computerghost/QBasic/Commands.htm

Page 1 of 5

4.2 Dual Alternative Decision Structures

If then else statement Figure 4-8 p. 131 If, Else, End If must align on left

Executes one group of statements if its Boolean expression is true, or another group if its Boolean expression is false

If condition Then statement

If temperature < 40 Then

Display “A little cold” statement CONDITION IS TRUE

Else statement statement CONDITION IS FALSE

End if Don’t forget END IF

Program 4-2 on p. 133 illustrates IF, THEN, ELSE for payroll.

Else

End if

Display “Get a coat!”

Display “Nice weather”

Display “And sunny!”

Build Dual Alternative (Password) Raptor file.

4.3 Comparing Strings

Most languages allow you to compare strings

Other String Concerns

Strings can be compared name1 == name 2 test the value of a string

String and string literals can be compared

Month != “October”

String comparisons are generally case sensitive

You can also determine whether one string is greater than or less than another string (allows for sorting strings)

Letters are stored by ASCII characters A through Z = 69 through 90 p. 138 Blank space = 32

Codes for Mary and Mark on p. 139 Mary is “greater than” Mark because y has higher code than k

Program 4-3 on p. 137 illustrates IF, THEN, ELSE for testing a password entered by the user.

Program 4-4 on p. 140 illustrates IF, THEN, ELSE for alphabetizing names. NOTE: In QBasic, the name must be surrounded by double quotes if you want to separate last name and first name with a comma. “Smith, Jack”

Build Comparing Strings (Two Letters) Raptor file.

4.4 Nested Decision Structures

Decisions are nested in order to test more than one condition

The if then else if statement can make nested logic simpler to write

Page 2 of 5

If score < 60 Then

Display “Grade is F.”

Else If score < 70 Then

Display “Grade is D.”

Else If score < 80 Then

Display “Grade is C.”

Else If score < 90 Then

Display “Grade is B.”

Else

Display “Grade is A.”

End If

From Program 4-7 p. 147 (easier to read than Program 4-6)

Condition = true…ignores rest of conditions

Flowchart for Program 4-5 on p. 142

IMPORTANT: Look at proper indentation on lines 14 through 24 on p. 142 see p. 144 Also End Ifs on p. 146

Build Nested Decision (Loan Qualification) Raptor file.

Build Nested Decision 2 (Grades) Raptor file.

Build Nested Problem 2 (Area of Two Rectangles) Raptor file. See QBasic code below.

10 PRINT "What is the length of Rectangle 1?"

20 INPUT length1

30 PRINT "What is the width of Rectangle 1?"

40 INPUT width1

50 PRINT "What is the length of Rectangle 2?"

60 INPUT length2

70 PRINT "What is the width of Rectangle 2?"

80 INPUT width2

90 area1 = length1 * width1

100 area2 = length2 * width2

110 IF area1 > area2 THEN

120 PRINT "Rectangle 1 has a greater area."

130 ELSE

140 IF area1 < area2 THEN

150 PRINT "Rectangle 2 has a greater area."

160 ELSE

170 IF area1 = area2 THEN

180 PRINT "Both rectangles have equal area."

190 END IF

200 END IF

210 END IF

Notice the line numbers. Some instructors require those. Keep those IF’s aligned properly.

4.5 The Case Structure

The case structure lets the value of a variable or an expression determine which path of execution the program will take

Can be used as an alternative to nested decisions Case structure is known as “switch statement” in some languages.

Case structure is easier to read than Figure 4-19 on p. 150.

Page 3 of 5

SHIPPING CALCULATOR (IF-THEN-ELSE)

5 CLS

10 PRINT "What is the weight of the package?"

20 INPUT weight

30 IF weight <= 2 THEN

40 charge = weight * 1.1

50 PRINT "The rate to ship is $"; charge

60 ELSE

70 IF weight <= 6 THEN

80 charge = weight * 2.2

90 PRINT "The rate to ship is $"; charge

100 ELSE

110 IF weight <= 10 THEN

120 charge = weight * 3.7

125 PRINT "The rate to ship is $"; charge

130 ELSE

140 charge = weight * 3.8

150 PRINT "The rate to ship is $"; charge

160 END IF

170 END IF

180 END IF

See pages 149-150.

For QBasic, use CASE IS for comparison operators.

CASE ELSE is the default statement.

PRINT "Enter a number."

INPUT num

SELECT CASE num

CASE IS < 10

PRINT "The number is less than 10."

CASE 10 TO 20

PRINT "The number is between 10 and 20."

CASE ELSE

PRINT "The number you gave is greater than 20."

END SELECT

SHIPPING CALCULATOR (SELECT CASE)

PRINT "Welcome to UPS RCC."

PRINT "What is the weight of the package?"

INPUT num

SELECT CASE num

CASE IS <= 2

PRINT "Weight charge is $1.10 per pound."

charge = num * 1.1

CASE IS <= 6

PRINT "Weight charge is $2.20 per pound."

charge = num * 2.2

CASE IS <= 10

PRINT "Weight charge is $3.70 per pound."

charge = num * 3.7

CASE IS > 10

PRINT "Weight charge is $3.80 per pound."

charge = num * 3.8

END SELECT

PRINT " Your total shipping charge is $"; charge

Build Nested Problem 9 (Shipping Calculator: IF-THEN-ELSE) Raptor file. See QBasic code above.

4.6 Logical Operators

Logical Operators are used between conditions to create complex (compound) Boolean expressions

AND – Both conditions must be true

Truth table on p. 155

OR – Either condition must be true

Truth table on p. 155

NOT – Reverses the truth of an expression

Truth table on p. 156

If temperature < 20 AND minutes > 12 Then

Display “The temperature is in the danger zone.”

End If

If temperature < 20 OR temperature > 100 Then

Display “The temperature is in the danger zone.”

End If

If NOT (temperature > 100) Then

Display “This is below the maximum temperature.”

End If

Enclosed in ( ) to force correct order of operations

Page 4 of 5

Tables 4-3 and 4-4 on page 154

Often used for range checking

When checking for a number inside a range, use AND

If x >=20 AND x <=40 Then

Display “The value is in the acceptable range.”

When checking for a number outside a range, use OR

If x < 20 OR x >40 Then

Display “The value is outside the acceptable range.”

End If End If

Short circuit evaluation: AND statement If left side is false, condition will be false, skips 2nd part of condition

Short circuit evaluation: OR statement If left side is true, condition will be true, skips 2nd part of condition

Don’t get logic of logical operators confused!! ERROR: If x < 20 AND x > 40 (Mathematically impossible)

Problem 4-9 is written simpler than Problem 4-5 because of the AND logical operator.

4.7 Boolean Variables

A variable of the Boolean data type can hold one or two values: true or false

See sales quota example on page 161.

Page 5 of 5

Download