computer-made decisions chapter 3 & 4. overview u variable scope u conditionals relational...

27
Computer-made Decisions Chapter 3 & 4

Upload: doris-burke

Post on 29-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

Computer-made Decisions

Chapter 3 & 4

Page 2: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

Overview

Variable Scope Conditionals

Relational Operators AND, OR, NOT

If Statements MsgBox function

Page 3: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

Conditionals

Sometimes it is necessary to alter one’s behavior, depending on existing conditions

Examples: If the light is red, stop If it is Labor Day, don’t come to class, otherwise attend

lecture and lab If there is a quiz on Monday, then read the book Sunday

Page 4: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

Conditionals

True or False conditions are created when comparing variables with relational operators by evaluating a boolean variable

Relational Operators> Greater than < Less than

= Equal to <> Not equal to

>= Greater than or equal to <= Less than or equal to

Page 5: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

Compound Conditions

You may join separate conditions with the ‘AND’ and the ‘OR’ to create compound conditions The AND is data reducing; it reduces choices that fit The OR is data expanding; it increases the choices that fit

Blue Cars

Four Doors

A B C

Blue Cars AND Four Doors is ?????

Blue Cars OR Four Doors is ?????

Page 6: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

Compound Conditions

And Or

T

T

T T

T

T

T

T

F

F

F FF F F

F

Union = Both Alternation = Either

Page 7: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

Compound Conditions

Evaluate Negations (NOTs) first Evaluate ANDs; left to right Evaluate ORs after all ANDs; left to right

Parentheses can, as usual override this precedence

Page 8: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

The If Test

A conditional statement that permits portions of code to be executed only if and when some special condition applies I'd like root beer if they have it, otherwise I'll have a cola If it is raining, bring an umbrella

Page 9: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

If Test

If (Condition) Then

' (do true stuff)

Else

' (do false stuff)

End If

Page 10: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

If Test (short form)

If (Condition) Then

' (do true stuff)

End If

use only if there is nothing to do in the Else branch

Page 11: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

IF Test Example

Private Sub cmdDo_Click() If cmdDo.Caption = "Talk" Then lblBox.Caption = "ding ding" cmdDo.Caption = "clear" Else 'in this case, cmdDo = "clear" lblBox.Caption = "" cmdDo.Caption = "Talk" End IfEnd Sub

Page 12: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

IF Test Example

<< alternate on clicking >>

Page 13: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

IF Test Example

Private Sub cmdBlink_Click() If lblBox.Visible = True then lblBox.Visible = False Else lblBox.Visible = True End IfEnd Sub

Since lblBox.Visible is boolean why not use

If lblBox.Visible then

Page 14: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

ElseIf Clause

If condition1 Thenstatements to process when condition1 is true

ElseIf condition2 Thenstatements to process when condition1 is false and condition2 is true

Elsestatements to process when condition1 is false and condition2 is false

End If 'ElseIf is much like select-case

Page 15: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

Nested Selection Structure

A nested selection structure is one in which either

the true path or the false path includes yet another

selection structure

Any of the statements within either the true or false

path of one selection structure may be another

selection structure

Page 16: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

Nested If Example Write an if statement that assigns a sales tax

rate to the sngTax variable The tax rate is determined by the state code stored in

the intCode variable: Codes of 1 and 3 represent a 4% rate A code of 2 represents a 5% rate All other codes represent a 2% rate

Page 17: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

Nested If Example

If intCode = 1 Or intCode = 3 ThensngTax = .04

ElseIf intCode = 2 Then

sngTax = .05Else

sngTax = .02End If

End If

Page 18: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

Nested If Example Write a selection structure that assigns a bonus to the sngBonus variable The bonus is determined by the salesperson’s code (intCode) and, in

some cases, by the sales amount (sngSales) If the code is 1

and the salesperson sold at least $10,000, then the bonus is $500 otherwise these salespeople receive $200

If the code is 2 and the salesperson sold at least $20,000, then the bonus is $600; otherwise these salespeople receive $550

All others receive $150

Page 19: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

Nested If ExampleIf intCode = 1 Then If sngSales >= 10000 Then

sngBonus = 500Else

sngBonus = 200End If

ElseIf intCode = 2 ThenIf sngSales >= 20000 Then

sngBonus = 600Else

sngBonus = 550End If

ElsesngBonus = 150

End If

Page 20: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

Nested If ExampleIf intCode = 1 And sngSales >= 10000 Then sngBonus = 500Else

If intCode = 1 And sngSales < 10000 ThensngBonus = 200

ElseIf intCode = 2 And sngSales >= 20000 Then

sngBonus = 600Else

If intCode = 2 And sngSales < 20000 ThensngBonus = 550

ElsesngBonus = 150

End IfEnd If

End IfEnd If

Page 21: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

ElseIf ExampleIf intCode = 1 And sngSales >= 10000 Then sngBonus = 500ElseIf intCode = 1 And sngSales < 10000 Then

sngBonus = 200ElseIf intCode = 2 And sngSales >= 20000 Then

sngBonus = 600ElseIf intCode = 2 And sngSales < 20000 Then

sngBonus = 550Else

sngBonus = 150End If

Page 22: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

The Message Box

VB allows the user to generate message boxes as a part of a user program. When invoked they look something like this:

Page 23: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

Format of MsgBox

A simple example of the MsgBox function

Dim intX As Integer

intX = MsgBox("Read the Password")

Page 24: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

The Details

Dim intX As Integer

intX = MsgBox("Do this now", _

vbCritical, "Prompting Issue")

Page 25: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

Response Choices

The value of “intZ” above depends on which response is chosen. “Yes” - intZ = 6“No” - intZ = 7(the numerical values are defined by Visual Basic.)

Dim intZ As IntegerintZ = MsgBox(“Should I Do This”, _ vbYesNo ,”Prompting Issue”)

Page 26: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

Icon Choices

vbExclamation

vbQuestion

vbCritical

Page 27: Computer-made Decisions Chapter 3 & 4. Overview u Variable Scope u Conditionals  Relational Operators  AND, OR, NOT u If Statements u MsgBox function

Possible values for the 2nd argumentConstant Value Description

vbOKOnly 0 Display OK button only

vbOKCancel 1 Display OK and Cancel buttons

vbAbortRetryIgnore 2 Display Abort, Retry, and Ignore buttons

vbYesNoCancel 3 Display Yes, No, and Cancel buttons

vbYesNo 4 Display Yes and No buttons

vbRetryCancel 5 Display Retry and Cancel buttons

vbCritical 16 Display Critical Message icon

vbQuestion 32 Display Warning Query icon

vbExclamation 48 Display Warning Message icon

vbInformation 64 Display Information Message icon