Decisions and Conditions

18
110 F-1 Decisions and Conditions Chapter 4: We can design a form We can calculate To make our programs more powerful, we need to be able to make choices within our program e.g. if the number of items purchased i reater than 10, apply a discount rate f not apply the usual rate) We will learn: The IfThen, Else, End If statemen How to write a logical expression using logical operators (And, Or) and more... Check the user’s input

description

Decisions and Conditions. We can design a form We can calculate. To make our programs more powerful, we need to be able to make choices within our programs. We will learn: The If … Then , Else , End If statement. Chapter 4:. (e.g. if the number of items purchased is - PowerPoint PPT Presentation

Transcript of Decisions and Conditions

Page 1: 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

Page 2: Decisions and Conditions

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

Page 3: Decisions and Conditions

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)

Page 4: Decisions and Conditions

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

Page 5: Decisions and Conditions

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

Page 6: Decisions and Conditions

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

Page 7: Decisions and Conditions

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!

Page 8: Decisions and Conditions

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

Page 9: Decisions and Conditions

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

Page 10: Decisions and Conditions

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

Page 11: Decisions and Conditions

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

Page 12: Decisions and Conditions

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

Page 13: Decisions and Conditions

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%

Page 14: Decisions and Conditions

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

Page 15: Decisions and Conditions

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

Page 16: Decisions and Conditions

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

Page 17: Decisions and Conditions

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”

Page 18: Decisions and Conditions

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