Download presentation
Presentation is loading. Please wait.
1
VB Decisions, Conditions & If
Chapter 5.1 & 5.2 12/30/2018 Prepared By: Deborah Becker
2
Prepared By: Deborah Becker
Covered Tipics Relational & Logical Operators Using And, Or, and Not If Blocks Read & Create Flowcharts (Logic) 12/30/2018 Prepared By: Deborah Becker
3
Prepared By: Deborah Becker
A Condition Definition Condition is an expression involving relational operators (such as > or =) that is either true or false. Example: “Frog” = “Frog” 12 > 8 = 12/30/2018 Prepared By: Deborah Becker
4
Prepared By: Deborah Becker
Conditions > greater than < less than = equal to <> not equal to >= greater than or equal to <= less than or equal to 12/30/2018 Prepared By: Deborah Becker
5
Mathematical Relational Operators
12/30/2018 Prepared By: Deborah Becker
6
Prepared By: Deborah Becker
Is the Condition True? 1 <= 1 1 < 1 “car” < “cat” “Dog” < “dog” 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. 12/30/2018 Prepared By: Deborah Becker
7
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) 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 12/30/2018 Prepared By: Deborah Becker
8
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 12/30/2018 Prepared By: Deborah Becker
9
Logical Operators allow us to..
Evaluate condition1 and condition2 based on the following condition1 and condition2 condition1 or condition2 condition1 not condition2 12/30/2018 Prepared By: Deborah Becker
10
Are these conditions T/F?
Dim answ as String = “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 12/30/2018 Prepared By: Deborah Becker
11
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 f = g 12/30/2018 Prepared By: Deborah Becker
12
Is this easier to understand?
((a < (b + c)) Or ((d < c) And (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 12/30/2018 Prepared By: Deborah Becker
13
Prepared By: Deborah Becker
The IF Statements 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. 12/30/2018 Prepared By: Deborah Becker
14
If Statement Examples in VB
If … Then (all on one line) If strName = "Jamie" Then lblMessage.text = "Hello Jamie" Block If … Then … End If If strName = "Sam" Then lblMessage.text = "Hello Sam" login(strName) 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 12/30/2018 Prepared By: Deborah Becker
15
Block If…Then…Else…End If
If strName = "Connie" Then lblMessage.text = "Hello Connie" Login(strName) Else lblMessage.text = "Hello” msgbox(“Not a valid User”) 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 12/30/2018 Prepared By: Deborah Becker
16
If…then…ElseIf…then…end if
If School Day True False If Sunny Stay In Bed False True Play Tennis Go to Class 12/30/2018 Prepared By: Deborah Becker
17
Prepared By: Deborah Becker
Example If costs = revenue Then lblResult.text = “Break even” ElseIf costs < revenue Then profit = revenue – costs lblResult.text = “Profit is “; _ FormatCurrency(profit) Else loss = costs – revenue lblResult.text = “Loss is “; _ FormatCurrency(loss) End if Elseif is written as one word End if is always two words. 12/30/2018 Prepared By: Deborah Becker
18
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 lblSolution.text = “true”; Else lblSolution.text = “false” End if 12/30/2018 Prepared By: Deborah Becker
19
Nesting Ifs—MultipleValidations
If you needed to check several input fields using nested ifs can eliminate the need to display multiple message statements Error messages are only displayed for the conditions that are false (Else) 12/30/2018 Prepared By: Deborah Becker
20
Additional Information
It is not necessary to indent If Blocks We indent to improve readability When constructs contain If Blocks inside other If Blocks, they are referred to as Nested Ifs Some times the use of relational operators can simplify the If Block structure making it easier to understand If condition1 Then If condition2 Then action End if If condition1 and condition2 then 12/30/2018 Prepared By: Deborah Becker
21
Prepared By: Deborah Becker
How can we simplify If condition1 Then If condition2 Then action End if If condition1 and condition2 then action End if 12/30/2018 Prepared By: Deborah Becker
22
Prepared By: Deborah Becker
Flowcharting the Ifs Flowcharts are a good tool to evaluate If Blocks We will use some example problems to see how flowcharting can add clarification to analyzing a problem 12/30/2018 Prepared By: Deborah Becker
23
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) 12/30/2018 Prepared By: Deborah Becker
24
Prepared By: Deborah Becker
12/30/2018 Prepared By: Deborah Becker
25
Prepared By: Deborah Becker
The End That’s All Folks 12/30/2018 Prepared By: Deborah Becker
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.