vl ch2 boolean

23
Chapter 2 A Guide to Working with Visual Logic 1 Making Decisions

Upload: humanonkalimon

Post on 04-Jan-2016

235 views

Category:

Documents


0 download

DESCRIPTION

text

TRANSCRIPT

Chapter 2

A Guide to Working with Visual Logic 1

Making Decisions

Making Decisions - Examples

A Guide to Working with Visual Logic 2

Problem: What to spend your allowance on? If you buy a shirt you wont have enough money left over for a CD. Pseudocode: If you buy a shirt Then you cannot buy a CD

Making Decisions - Examples

A Guide to Working with Visual Logic 3

Problem: Which show to go to? If you go to the early show of a movie, you will be

home in time to watch your favorite program on TV. If you choose the later show, then you should set your TiVo to record the program.

Pseudocode: If I go to the early show Then I can reach home and watch my favorite program Else If I go to the later show Then I should set my TiVo to record my favorite program

EndIf (Note: there is no Else for this If) EndIf

Boolean - Data Type

A Guide to Working with Visual Logic 4

�  Another data type is boolean �  A boolean variable has only 2 possible values:

�  true �  false

�  In an assignment: x = (2 > 3), then x will be a boolean variable and its value will be FALSE

Conditions – Boolean Expression

A Guide to Working with Visual Logic 5

�  Boolean expressions can have only 2 possible values: true or false �  Examples:

A < B (is A less than B?) num1 = 0 (is num1 equal to 0?) �  This is confusing because num1=0 looks like an assignment statement �  In Visual Logic = is used for

�  “gets” (assignment: A = 3 can be read as “A gets 3 assigned to it”) �  “Is equal to” (boolean expression: A = 3 can be read as “Is A equal to 3 true?)

�  A condition is a boolean expression �  Conditions usually involve Relational Operators

< (less than), > (greater than), == (equals) Note: in most programming languages == is used for “Is equal to”

Relational Operators

A Guide to Working with Visual Logic 6

�  6 Relational Operators

Operator (Visual Logic)

Description Boolean Expression

Result (assume X = 2, Y = 3)

= Equals X = 2 X = Y

TRUE FALSE

< > Not Equal Y < > 5 Y < > 3

TRUE FALSE

> Greater Than X > 1 X > Y

TRUE FALSE

< Less Than X < Y X < 2

TRUE FALSE

>= Greater Than Or Equal

X >= 2 X >= Y

TRUE FALSE

<= Less Than Or Equal

X <= 2 X <= 1

TRUE FALSE

Demo: Odd or Even?

A Guide to Working with Visual Logic 7

� Problem: Display a message that says whether an input is odd or even

Demo: Weekly Paycheck - Overtime

A Guide to Working with Visual Logic 8

IF Statement

A Guide to Working with Visual Logic 9

�  IF statements are used to choose between actions �  In Visual Logic, the IF flowchart element is a diamond that

contains a condition . The diamond branches into a true and false path.

�  A condition is a boolean expression that usually involves one of six relational operators

�  When executed, the condition is evaluated. If the condition is true, control flows along the true arrow. If the condition is false, control flows along the false arrow

�  An IF statement ends where the true and false branches reconnect

Nested IF Statements

A Guide to Working with Visual Logic 10

�  The true and false branches of an IF statement may contain any number of statements, including other IF statements

�  The term nested IF is used for an IF statement contained within the true or false branch of another IF statement

�  (Demo) Figure 2-3: Read two input values and determine if they are equal, if the first is greater than the second, or if the first is smaller than the second

A Guide to Working with Visual Logic 11

PseudoCode – for Figure 2-3

A Guide to Working with Visual Logic 12

�  Input: First �  Input: Second

IF (First = Second)

THEN Output: The values are equal ELSE IF (First < Second) THEN Output: The first number is less than the second number

ELSE Output: The first number is greater than the second number ENDIF

ENDIF

Long Distance Billing Problem

A Guide to Working with Visual Logic 13

�  The billing rate for a long distance phone call �  If the call is between 6:00 am and 6:00pm, then the billing rate

is 10 cents per minute � Nights and mornings are free

�  Input: Time will be in military time (6 am è 600, 6:00pm è 1800, 7:45 am è 745) (demo : Figure 2-4)

A Guide to Working with Visual Logic 14

Compound Conditions

A Guide to Working with Visual Logic 15

�  Complex conditions require multiple comparisons

�  4 Logical Operators: AND, OR, XOR, NOT

�  A compound condition consists of two conditions within parenthesis joined by a logical operator

�  Example: (A >= 0) AND (A <= 99) will be true if A is a number between 0 and 99 including both 0 and 99, otherwise it will be false

A Guide to Working with Visual Logic 16

LOGICAL OPERATOR

DESCRIPTION EXAMPLE RESULT (Assume: A=5, B=8)

NOT Returns the opposite of the condition

NOT (A < 3) NOT (B = 8)

TRUE FALSE

AND Returns true if and only if both the conditions are true

(A = 1) AND (B = 9) (A = 5) AND (B = 9) (A = 1) AND (B = 8) (A = 5) AND (B = 8)

FALSE FALSE FALSE TRUE

OR Returns true if at least one condition is true

(A = 1) OR (B = 9) (A = 5) OR (B = 9) (A = 1) OR (B = 8) (A = 5) OR (B = 8)

FALSE TRUE TRUE TRUE

XOR Returns true if the conditions have opposite values

(A = 1)XOR (B = 9) (A = 5) XOR (B = 9) (A = 1) XOR (B = 8) (A = 5) XOR (B = 8)

FALSE TRUE TRUE FALSE

Examples of Logical Operators - AND

A Guide to Working with Visual Logic 17

� Male drivers under the age of 25: (Gender = “Male”) AND (Age < 25) �  Is a student is a smart Senior ;-) (TotalCredits > 90) AND (GPA > 3.25) � Aways use parentheses around each condition in a

logical operator �  (5 > 4) AND (4 > 3) evaluates to true , but would be false

without parentheses � Demo – Fig 2.5 – long distance billing with AND (Time > 600) AND (Time < 1800)

A Guide to Working with Visual Logic 18

Examples of Logical Operators - OR

A Guide to Working with Visual Logic 19

�  Higher insurance rates for young drivers and (?) senior citizens (Age < 25 ) OR (Age > 65)

�  Pre-requisite to take a Senior level course: a student must be a Senior or have a high GPA (TotalCredits > 90) OR (GPA > 3.25) •  Aways use parentheses around each condition in a

logical operator

A Guide to Working with Visual Logic 20

Begin

End

Input: A

Input: B

Input: C

A < BFalse True

A < CFalse True

Output:A & " is smallest"

Output:C & " is smallest"

B < CFalse True

Output:B & " is smallest"

Output:C & " is smallest"

Smallest Number Solution 1:

Nested Conditions

A Guide to Working with Visual Logic 21

Smallest Number Solution 2: Begin

End

Input: A

Input: B

Input: C

(A < B) AND (A < C)False True

Output:A & " is smallest"

(B < A) AND (B < C)False True

Output:B & " is smallest"

(C < A) AND (C < B)False True

Output:C & " is smallest"

Sequential Compound Conditions

A Guide to Working with Visual Logic 22

Smallest Number Solution 3: Begin

End

Input: A

Input: B

Input: C

(A < B) AND (A < C)False True

Output:A & " is smallest"B < CFalse True

Output:B & " is smallest"

Output:C & " is smallest"

Nested and Compound Conditions

A Guide to Working with Visual Logic 23

Smallest Number Solution 4: Begin

End

Input: A

Input: B

Input: C

Smallest = A

B < SmallestFalse True

Smallest = B

C < SmallestFalse True

Smallest = C

Output:Smallest & " is smallest"

Place Holder Variable