Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 programs. (e.g. if the number of items purchased is greater than 10, apply a discount rate if not apply the usual rate) We will learn: The If…Then, Else, End If statement How to write a logical expression using logical operators (And, Or) and more... Check the user’s input

2 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” Else display the message “can vote” As a flowchart display can vote display Can’t vote ? age < 18 False True

3 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)

4 110 F-4 VB If statement General Form: If ( condition ) Then statement(s) Else statement(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 optional but clearer

5 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 their Unicode encoding (see table p 150) the computer can compare strings "&123#" > "abc" "monkey"<"month" False because & < a True because k < t … < & < … < 5 < … < A < B < … < a < … < z < … < } < … 3853656697122125

6 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: F F T T T F F F T T T F P, Q are conditions T and F stand for True and False Not PP And QP Or Q

7 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 lower precedence higher precedence intA = 2 intB = 3 intC = 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!

8 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

9 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 x  0, Always use If (x=0) Then If (x<>0) Then

10 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 Option Strict On! CInt(True) is -1 CInt(False)is 0 We can understand how VB reads the condition: 0 <= x <= 10 Evaluated first True 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

11 110 F-11 IsNumeric: a useful function _ to check the user’s input of a number: How do we check that an entry written in 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

12 110 F-12 An Example Goal Input the user’s age and check that it is a valid 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 message and clear the text box. use a message box Exit Validate Enter your age text box

13 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 < 150000%

14 110 F-14 If (dblPay>=0 And dblPay<15000) Then dblRate = 0 Endif If (dblPay >=15000 And dblPay <30000) Then dblRate = 0.18 Endif But we can do better! If (dblPay >=30000 And dblPay <50000) Then dblRate = 0.22 Endif If (dblPay >=50000 And dblPay <100000) Then dblRate = 0.28 Endif If (dblPay >=100000) Then dblRate = 0.31 Endif Simple Solution

15 110 F-15 If (dblPay < 15000) Then dblRate = 0.00 Else End If Better Cascaded ifs If (dblPay < 30000) Then dblRate = 0.18 Else End If If (dblPay < 50000) Then dblRate = 0.22 Else End If If (dblPay < 100000) Then dblRate = 0.28 Else dblRate = 0.31 End If

16 110 F-16 using ElseIf (clearer) If (dblPay < 15000) Then dblRate = 0.00 ElseIf (dblPay < 30000) Then dblRate = 0.18 ElseIf (dblPay < 50000) Then dblRate = 0.22 ElseIf (dblPay < 100000) Then dblRate = 0.28 Else dblRate = 0.31 End If order of the conditions is important. Conditions are successively inclusive Best Only one End If

17 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”

18 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 lowercase e.g. txtString1.Text.ToUpper() Dim strName As String strName=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


Download ppt "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."

Similar presentations


Ads by Google