Decisions and Conditions
description
Transcript of Decisions and Conditions
110 F-1
Decisions and Conditions
Chapter 4:
We can design a formWe can calculate
To make our programs morepowerful, we need to be able to make choices within our programs.
(e.g. if the number of items purchased is greater than 10, apply a discount rateif not apply the usual rate)
We will learn:The If…Then, Else, End If statementHow to write a logical expressionusing logical operators (And, Or)
and more...
Check the user’s input
110 F-2
If statement (1)A conditional statement:
In plain English (also called pseudocode):
If the age is less than 18,display the message “can’t vote”Elsedisplay the message “can vote”
As a flowchart
displaycan vote
displayCan’t vote
?age < 18
False True
110 F-3
As a VB statement
If intAge < 18 Then lblMessage.Text = "Can’t Vote"Else lblMessage.Text = "Can Vote"End If
If statement (2)
110 F-4
VB If statement
General Form:
If (condition) Then
statement(s)
Elsestatement(s)
End If
optional
To write the condition, we can use the following relational operators:
> for greater than
< for less than
>= for greater than or equal to
<= for less than or equal to
<> for not equal to
= for equal to
compare numbers… and strings
parentheses are optionalbut clearer
110 F-5
Conditions examples
If (CInt(txtNumber.txt)>intMAX) Then
If ( dblB^2 - 4*dblA*dblC > 0 ) Then
If ( intAge <> 25) Then
With numbers:
With strings:
Use of lexicographic order ( dictionary)All characters are ordered according to theirUnicode encoding (see table p 150)
the computer can compare strings
"&123#" > "abc""monkey"<"month"
False because & < aTrue because k < t
…< & < … < 5 < … < A < B < … < a < … < z < … < } < …
38 53 65 66 97 122 125
110 F-6
Logical operators
How to write in VB a condition like:
If 0 < intNumber < 20 ?
use the logical operator And
If ( intNumber>0 And intNumber<20) Then
The other logical operators are Not and Or
Recall the logical table:
FFTT
TFFF
TTTF
P, Q are conditionsT and F stand for True and False
P Q ! P P && Q P || QT TT FF TF F
Not P P And Q P Or Q
110 F-7
Using And, Or, Not
If (dblFinal > 90) And (dblMidterm > 90) Then lblGrade.Text = “A”Endif
( ) are optional,but clearer
Be careful with the precedence order:Not Or And
lowerprecedence
higher precedence
intA = 2intB = 3intC = 4
If Not intA>3 And intB<>3 Or Not intC = 6 Then lblDisplay.Text="the condition is true"Else lblDisplay.Text= "the condition is false"Endif
What is displayed?the condition is true
Do not hesitate to use parentheses!
110 F-8
Condition Truth Value(1)_When we write
If (condition) Then
VB evaluates the condition as a Boolean( = True or False)
e.g.
If (dblTemperature>212) Then
lblDisplay.Text = "The water is boiling"
End If
True or False
_You may use shortcuts if you are testing a variable which is a Boolean:
e.g. the Checked property of a radio button is either True or False
If radRed.Checked = True Then
same as
If radRed.Checked Then
110 F-9
Condition Truth Value (2)
_ If your condition is just a number
Not a good programming practice!
Forbidden with Option Strict On!
If ( x ) Then
False if x=0, True if x0,
Always use
If (x=0) Then
If (x<>0) Then
110 F-10
The numeric value of a condition
What happens when VB converts a boolean to a number ?Warning: this example is illegal with OptionStrict On!
CInt(True) is -1
CInt(False)is 0
We can understand how VB reads the condition:
0 <= x <= 10
Evaluated firstTrue or False
To evaluate True (or False) <= 10,VB converts True to -1 and False to 0
Since -1 (or 0) is less than 10, the condition 0 <= x <= 10 is always True!
True(always)
Similarly, -10 <= x <= - 5 is always False
110 F-11
IsNumeric: a useful function
_ to check the user’s input of a number:
How do we check that an entry writtenin a text box is a number? e.g. 1.3 is OK but “one” is not!
Instead use the VB function IsNumeric
IsNumeric("1.3") is True
IsNumeric("one") is False
If IsNumeric(txtInput.Text) Then
110 F-12
An ExampleGoal Input the user’s age and check that it is avalid entry (e.g. 0 < age < 130)
How to validate?
if the age is not between 0 and 130 or is not a numeric entry, display a warning messageand clear the text box.
use a message box
Exit
Validate
Enter your age
textbox
110 F-13
Nested Ifs
Useful for cases with more than 2 choices:
How to code this in VB?
Example: print the percentage tax based on pay
if 30000 pay < 50000 22%
if 50000 pay < 100000 28%
if 100000 pay 31%
if 15000 pay < 30000 18%
if pay < 15000 0%
110 F-14
If (dblPay>=0 And dblPay<15000) Then dblRate = 0Endif
If (dblPay >=15000 And dblPay <30000) Then dblRate = 0.18Endif
But we can do better!
If (dblPay >=30000 And dblPay <50000) Then dblRate = 0.22Endif
If (dblPay >=50000 And dblPay <100000) Then dblRate = 0.28Endif
If (dblPay >=100000) Then dblRate = 0.31Endif
Simple Solution
110 F-15
If (dblPay < 15000) Then dblRate = 0.00Else
End If
BetterCascaded ifs
If (dblPay < 30000) Then dblRate = 0.18
Else
End If
If (dblPay < 50000) Then dblRate = 0.22Else
End If
If (dblPay < 100000) Then dblRate = 0.28
Else dblRate = 0.31End If
110 F-16
using ElseIf (clearer)
If (dblPay < 15000) Then dblRate = 0.00ElseIf (dblPay < 30000) Then dblRate = 0.18ElseIf (dblPay < 50000) Then dblRate = 0.22ElseIf (dblPay < 100000) Then dblRate = 0.28Else dblRate = 0.31End If
order of the conditionsis important.Conditions are successively inclusive
Best
Only one End If
110 F-17
using Select Case
Select Case intAge Case Is < 13 lblAgeGroup.Text = “Child” Case 13 To 19 lblAgeGroup.Text = “Teenager” Case 20 To 30 lblAgeGroup.Text = “Young adult” Case 31 To 60 lblAgeGroup.Text = “Middle aged” Case Else lblAgeGroup.Text = “Senior”End Select
Another approach
Other possibilities: Testing for a set of values Case 1, 2, 5, 10 Works with strings as well Case “UW”, “Seattle Central”
110 F-18
More on Strings
Function for strings (can be useful in conditionals)
ToUpper
converts all of the letters to uppercase
ToLower
converts all of the letters to lowercasee.g.
txtString1.Text.ToUpper()
Dim strName As StringstrName=strName.ToLower()
A string is immutable (= can't change) strName.ToLower() doesn't change strName You must write: strName = strName.ToLower()
& to concatenate strings: "Hello, " & "world!"Special characters (in the ControlChars class)e.g. ControlChars.NewLine