180 likes | 295 Views
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
E N D
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 greater than 10, apply a discount rate if not apply the usual rate) How to write a logical expression using logical operators (And, Or) Check the user’s input and more... 110 F-1
If statement (1) True False age < 18 ? 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 110 F-2
If statement (2) As a VB statement If intAge < 18 Then lblMessage.Text = "Can’t Vote" Else lblMessage.Text = "Can Vote" End If 110 F-3
VB If statement If (condition) Then parentheses are optional but clearer statement(s) Else optional statement(s) End If To write the condition, we can use the following relational operators: compare numbers… and strings General Form: > 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 110 F-4
Conditions examples the computer can compare strings 38 53 65 66 97 122 125 With numbers: If ( intAge <> 25) Then If (CInt(txtNumber.txt)>intMAX) Then If ( dblB^2 - 4*dblA*dblC > 0 ) Then With strings: Use of lexicographic order ( dictionary) All characters are ordered according to their Unicode encoding (see table p 150) …<&<…<5<…<A<B<…<a<…<z<… <}<… False because & < a "&123#" > "abc" True because k < t "monkey"<"month" 110 F-5
Logical operators use the logical operator And P, Q are conditions T and F stand for True and False Not P P And Q P Or Q F T T F F T T F T T F F How to write in VB a condition like: If 0 < intNumber < 20 ? If ( intNumber>0 And intNumber<20) Then The other logical operators are Not and Or Recall the logical table: 110 F-6
Using And, Or, Not ( ) are optional, but clearer Not And Or higher precedence lower precedence What is displayed? If (dblFinal > 90) And (dblMidterm > 90) Then lblGrade.Text = “A” Endif Be careful with the precedence order: intA = 2 intB = 3 intC = 4 IfNot intA>3 And intB<>3 OrNot intC = 6 Then lblDisplay.Text="the condition is true" Else lblDisplay.Text= "the condition is false" Endif the condition is true Do not hesitate to use parentheses! 110 F-7
Condition Truth Value(1) True or False _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 _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-8
Condition Truth Value (2) False if x=0, True if x0, _ If your condition is just a number If ( x ) Then Not a good programming practice! Forbidden with Option Strict On! Always use If (x=0) Then If (x<>0) Then 110 F-9
The numeric value of a condition CInt(True) is -1 CInt(False)is 0 Evaluated first True or False True (always) What happens when VB converts a boolean to a number ? Warning: this example is illegal with Option Strict On! We can understand how VB reads the condition: 0 <= x <= 10 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! Similarly, -10 <= x <= - 5 is always False 110 F-10
IsNumeric: a useful function If IsNumeric(txtInput.Text) Then _ 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 functionIsNumeric IsNumeric("1.3") is True IsNumeric("one") is False 110 F-11
An Example text box Enter your age Validate Exit use a message box 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. 110 F-12
Nested Ifs if pay < 15000 0% if 15000 pay < 30000 18% if 30000 pay < 50000 22% if 50000 pay < 100000 28% if 100000 pay 31% How to code this in VB? Useful for cases with more than 2 choices: Example: print the percentage tax based on pay 110 F-13
Simple Solution But we can do better! If (dblPay>=0 And dblPay<15000) Then dblRate = 0 Endif If (dblPay >=15000 And dblPay <30000) Then dblRate = 0.18 Endif 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 110 F-14
Better Cascaded ifs If (dblPay < 15000) Then dblRate = 0.00 Else If (dblPay < 30000) Then dblRate = 0.18 Else If (dblPay < 50000) Then dblRate = 0.22 Else If (dblPay < 100000) Then dblRate = 0.28 Else dblRate = 0.31 End If End If End If EndIf 110 F-15
Best using ElseIf (clearer) order of the conditions is important. Conditions are successively inclusive OnlyoneEnd If 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 110 F-16
Another approach 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 Other possibilities: Testing for a set of values Case 1, 2, 5, 10 Works with strings as well Case “UW”, “Seattle Central” 110 F-17
More on Strings 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() & to concatenate strings: "Hello, " & "world!" Special characters (in the ControlChars class) e.g. ControlChars.NewLine Function for strings (can be useful in conditionals) A string is immutable (= can't change) strName.ToLower() doesn't change strName You must write: strName = strName.ToLower() 110 F-18