Presentation is loading. Please wait.

Presentation is loading. Please wait.

VB Decisions & Conditions

Similar presentations


Presentation on theme: "VB Decisions & Conditions"— Presentation transcript:

1 VB Decisions & Conditions
2/21/2019 Prepared By: Deborah Becker

2 Prepared By: Deborah Becker
Covered Tipics Conditions defined Relational operators Logical Operators If Blocks Read & Create Flowcharts (Logic) 2/21/2019 Prepared By: Deborah Becker

3 Prepared By: Deborah Becker
A Condition Definition Condition is an expression involving relational operators (such as > or =) or logical operators (and or) that is either true or false. Example: “Frog” = “Frog” 12 > 8 = 2/21/2019 Prepared By: Deborah Becker

4 Relational Conditions
> greater than < less than = equal to <> not equal to >= greater than or equal to <= less than or equal to Expressions containing comparison operators always result in an answer that is either true or false. Example expressions using Visual Basic controls: txtName.text <> "Frockmeister" Val(txtAge.Text) > 25 optPrintForm.Value = True chkDiscardStyles.Value = False txtInterestRate.Text / 12 <= 0.05 2/21/2019 Prepared By: Deborah Becker

5 Examples of Conditions
txtName.text <> "Frockmeister" Val(txtAge.Text) > 25 optPrintForm.Value = True chkDiscardStyles.Value = False (Val(txtInterestRate.Text) / 12) <= 0.05 2/21/2019 Prepared By: Deborah Becker

6 Mathematical Relational Operators
2/21/2019 Prepared By: Deborah Becker

7 Prepared By: Deborah Becker
Is the Condition True 1 <= 1 1 < 1 “car” < “cat” “Dog” > “dog” Remember the ASCII Table 2. False < means less than. No number can be less than itself True Characters of a string are compared one at a time, working from left to right. The first two characters match and the third decides if the condition is true. R precedes T in the alphabet. True Uppercase letters precede lowercase letters in the ANSI table. 2/21/2019 Prepared By: Deborah Becker

8 Are These Conditions True?
Let a = 4 and b = 3 Let c = “hello” and d = “bye” (a + b) < (2 * a) (Len(c) – b) = (a / 2) C < (“good” & d) * The function Len() returns a numeric value that indicated the number of characters in the string, inside the function. 1. True = 7 and 2 * 4 = 8 2. True Len(c) = 5 – 3 = 2 and (4 / 2) = 2 False H follows G in the ANSI table 2/21/2019 Prepared By: Deborah Becker

9 Prepared By: Deborah Becker
Logical Operators The three main logical operators are And Or Not Some programming conditions require more complicated comparison then just true or false 2/21/2019 Prepared By: Deborah Becker

10 Logical Operators allow us to..
Evaluate condition1 and condition2 based on the following condition1 and condition2 condition1 or condition2 condition1 not condition2 2/21/2019 Prepared By: Deborah Becker

11 With Relational and Logical Conditions We Can
Comparing Numeric Variables & Constants Comparing Strings Comparing text property of text boxes Uppercase and lowercase character comparisons Compound conditions When comparing values from text boxes with numeric values, be sure to first convert the text box data to a value. For example: If txtPrice.Text = curRetailPrice Then ... can result in an inaccurate comparison of a string and a currency value. Better to do this: If Val(txtPrice.Text) = curRetailPrice Then ... Also, if the text box is empty, then Val(textbox.text) is equal to zero. That can be very handy. It eliminates one possible error when you use a value in calculations When comparing strings, make sure you “negate” any potential problems with case not matching. Generally, you simply want the spelling to match. Compare these two slightly different conditions: If txtLastName.Text = “Smith” or If Lcase(txtLastName.Text) = “smith” You can use the logical operators of And, Or, and Not to form compound expressions such as: If (Ucase(txtLastName) = “SMITH” And Val(txtAge) > 25) Then... If (Val(txtAge.Text) = 0 Or txtLastName.Text = “”) Then ... Using parentheses around the conditional statement is optional but desirable 2/21/2019 Prepared By: Deborah Becker

12 Comparing Numbers & Strings
If txtPrice.Text = curRetailPrice Then ... If Val(txtPrice.Text) = curRetailPrice Then ... Compare these two slightly different conditions: If txtLastName.Text = “Smith” If Lcase(txtLastName.Text) = “smith” 2/21/2019 Prepared By: Deborah Becker

13 Are these conditions T/F?
Let the string variable answ = “Y” (answ = “Y”) or (answ = “y”) (answ = “Y”) and (answ = “y”) Not (answ = “y”) True the condition become “Y” = “Y” False the condition would be false for every condition fo answ. You can not set a variable = to two separate values simultaneously True because “Y” = “y” is false 2/21/2019 Prepared By: Deborah Becker

14 Prepared By: Deborah Becker
Using Parentheses Using parentheses can improve readability. Place parentheses in this equation to help evaluate it. a < b + c Or d < e And Not f = g 2/21/2019 Prepared By: Deborah Becker

15 Prepared By: Deborah Becker
Answer a < (b + c) Or (d < c) And (Not f = g) A condition involving numeric variables is different from an algebraic truth. The condition (a + b) < 2 * a is not a valid algebraic truth because it is not true for all values of a and b VB will evaluate it as true if it is correct for the current values of the variables 2/21/2019 Prepared By: Deborah Becker

16 Prepared By: Deborah Becker
Compound Conditions If a1 <= a2 or a3 > a4 and a5 < a6 Then And has precedence over Or All comparison operators have precedence over all logical operators Use parentheses () to alter the order of evaluation Parentheses alter the order of evaluation and are good for explicitly documenting groupings or are good if you are unsure of precedence. If (curValue > 100) and (curValue < 200) Then lblMessage.Caption = "Value between 101 and 199" End If The following is a project called SimpleSample.vbp to illustrate the example on pages 2/21/2019 Prepared By: Deborah Becker

17 Prepared By: Deborah Becker
Parentheses Parentheses alter the order of evaluation, are good for explicitly documenting groupings, and are good if you are unsure of precedence. If (curValue > 100) and (curValue < 200) Then lblMessage.Caption = "Value between 101 and 199" End If 2/21/2019 Prepared By: Deborah Becker

18 IF Statement Structure (1)
Sleepy? Go to bed True False Form: If condition Then statement(s) End If 2/21/2019 Prepared By: Deborah Becker

19 IF Statements Structure (2)
If School Day False True Stay In Bed Go to Class Action Action The if block allows a program to decide on a course of action. In the If statement You are always testing the condition to determine if it is True or False If no Else statement is included then the statement is only executed if the condition is true. 2/21/2019 Prepared By: Deborah Becker

20 Prepared By: Deborah Becker
If Examples in VB If … Then (all on one line) If strName = "Jamie" Then lblMessage.Caption _ = "Hello Jamie" Block If … Then … End If If strName = "Sam" Then lblMessage.Caption = "Hello Sam" [another statement] End If In the If Statement, when the condition is true, the Then clause is executed; when the condition is false, only the Else clause, if present, is executed. The if block must contain an End If. It is always two words Time 2/21/2019 Prepared By: Deborah Becker

21 Block If…Then…Else…End If
If strName = "Connie" Then lblMessage.Caption = "Hello Connie" [another statement] Else lblMessage.Caption = "Hello Stranger" [another statement] End If The if then else clause can contain an else condition If today is cloudy then go to class Else go to beach End if 2/21/2019 Prepared By: Deborah Becker

22 If…then…ElseIf…then…end if
If School Day True False If Sunny Stay In Bed False True Play Tennis Go to Class 2/21/2019 Prepared By: Deborah Becker

23 Prepared By: Deborah Becker
Example of Nested IF If costs = revenue Then picResult.Print = “Break even” Else If costs < revenue Then profit = revenue – costs picResult.Print “Profit is “; _ FormatCurrency(profit) Else loss = costs – revenue picResult.Print “Loss is “; _ FormatCurrency(loss) End if Elseif is written as one word End if is always two words. 2/21/2019 Prepared By: Deborah Becker

24 If Block and Logic Operators
The If Block can be combined with logic operators to compare more than one condition If (answer >= .5) and (answer <= 1) Then picSolution.Print “Good”; Else picSolution.Print “No” End if 2/21/2019 Prepared By: Deborah Becker

25 Additional Information
It is not necessary to indent If Blocks We indent to improve readability Some times the use of relational operators make the If Block structure easier to understand If condition1 Then If condition2 Then action End if If condition1 and condition2 then 2/21/2019 Prepared By: Deborah Becker

26 Prepared By: Deborah Becker
The Select Case Block Executes a statement based on the value of a variable Select Case bytMonth Case 1, 3, 5, 7, 8, 10, 12 number_of_days = 31 Case 2 number_of_days = 28 Case 4, 6, 9, 11 number_of_days = 30 End Select If more branches are required then a ‘Select Case’ command can be used. Notice that in the following example, several values can trigger the same case statement. For example, the months 1, 3, 5, 7, 8, 10 and 12 all are 31 days long. Instead of writing 12 separate case statements, all these values can be included in one case by using commas. The ‘End Select’ statement defines the end of the Case block. To ‘trap’ any values not picked up by earlier cases used the ‘Case Else’ construct. Case Else MsgBox "ERROR: Month must be between 1 and 12!“ End Select 2/21/2019 Prepared By: Deborah Becker

27 Prepared By: Deborah Becker
Looking for a Number Select Case Score Case is = 0 ' Score is still 0 lblMsgbox ("Keep trying!") Case 1To 4 ' Score is between 1 and 4. lblMsgbox ("Making a good start!") Case 5, 6,7 ' Score is either 5, 6, or 7. lblMsgbox ("Nearly there!") Case Is > 7 And Number < 11 ' Score is 8 ,9, or 10 lblMsgbox ("Getting close! ") Case Else ' Score is one of the other values. lblMsgbox (“Number is to large! ") End Select 2/21/2019 Prepared By: Deborah Becker

28 Prepared By: Deborah Becker
The Problem In New Jersey the state income tax is based on a persons base income at three different interval levels The tax on Income <= $20,000 is taxed at 2% of income The tax on Income > $20,000 but <= $50,000 is $400 plus 2.5% of (income -$20,000) The tax on income > $50,000 is $1150 plus 3.5% of (income - $50,000) 2/21/2019 Prepared By: Deborah Becker

29 Prepared By: Deborah Becker
2/21/2019 Prepared By: Deborah Becker

30 Prepared By: Deborah Becker
For Next Block For index = lower limit to upper limit Coding statements This could be code to print the records in a file. Print one record from a file May contain condition blocks Next index 2/21/2019 Prepared By: Deborah Becker

31 Action of For...Next structure
Counter<=ending value Counter>ending value For counter = start To end statement1 statement2 . . . For counter = start To end statement1 statement2 . . . Increment counter Next counter Next counter statement The Index can also be called the counter. 2/21/2019 Prepared By: Deborah Becker

32 Iterations of a For...Next loop
Private Sub cmdForNextTest_Click() Dim X As Integer ‘ define the index For X = 1 To ‘ 1 = lower limit picDisplay.Print X, ‘ 3 = upper limit Next X End Sub What will be the value of X when the loop terminates? What will be inside the picture box? 2/21/2019 Prepared By: Deborah Becker

33 Any Questions about the homework problem?
Problem Check Any Questions about the homework problem? 2/21/2019 Prepared By: Deborah Becker


Download ppt "VB Decisions & Conditions"

Similar presentations


Ads by Google