Download presentation
Presentation is loading. Please wait.
Published byIra Jefferson Modified over 9 years ago
1
Tutorial 4: The Selection Structure 1 Tutorial 4 The Selection Structure
2
Tutorial 4: The Selection Structure 2 The If…Then…Else Statement Lesson A Objectives After completing this lesson, you will be able to: Write pseudocode for the selection structure Create a flowchart to help you plan an application’s code Write an If…Then…Else statement Write code that uses comparison operators and logical operators Use the UCase and LCase functions
3
Tutorial 4: The Selection Structure 3 The Selection Structure Use to make a decision or comparison and then, based on the result of that decision or comparison, to select one of two paths The condition must result in either a true (yes) or false (no) answer If the condition is true, the program performs one set of tasks. If the condition is false, there may or may not be a different set of tasks to perform.
4
Tutorial 4: The Selection Structure 4 Coding the If and If/Else Selection Structures If condition Then instructions when the condition is true End If If condition Then [instructions when the condition is true] [Else [instructions when the condition is false]] End If
5
Tutorial 4: The Selection Structure 5 Comparison Operators Comparison operators are evaluated from left to right, and are evaluated after any mathematical operators =Is equal to >Is Greater Than >=Is Greater Than or Equal to <Is Less Than <=Is Less Than or Equal to <>Is Not Equal to
6
Tutorial 4: The Selection Structure 6 Expressions Containing Relational Operators 10 + 3 < 5 * 2 5 * 2 is evaluated first, giving 10 10 + 3 is evaluated second, giving 13 13 < 10 is evaluated last, giving false 7 > 3 * 4 / 2 3 * 4 is evaluated first, giving 12 12 / 2 is evaluated second, giving 6 7 > 6 is evaluated last, giving true All expressions containing a relational operator will result in either a true or false answer only
7
Tutorial 4: The Selection Structure 7 Using a Comparison Operator Dim intFirst, intSecond As Integer If (intFirst > intSecond) Then Dim intTemp As Integer intTemp = intFirst intFirst = intSecond intFirst = intTemp End If Block-level Variable
8
Tutorial 4: The Selection Structure 8 Flowchart Symbols start/stop oval process rectangle input/output parallelogram selection/repetition diamond symbols are connected by flowlines
9
Tutorial 4: The Selection Structure 9 Selection Structure Flowcharts T F TF
10
Tutorial 4: The Selection Structure 10 Logical Operators NotReverses the truth value of condition; false becomes true and true becomes false. 1 AndAll conditions connected by the And operator must be true for the compound condition to be true. 2 AndAlsoAll conditions connected by the AndAlso operator must be true for the compound condition to be true. 2 OrOnly one of the conditions connected by the Or operator needs to be true for the compound condition to be true. 3 OrElseOnly one of the conditions connected by the OrElse operator needs to be true for the compound condition to be true. 3 XorOne of the conditions connected by Xor must be true for the compound condition to be true. 4
11
Tutorial 4: The Selection Structure 11 Expressions Containing the And Logical Operator 3 > 2 And 6 > 5 3 > 2 is evaluated first, giving true 6 > 5 is evaluated second, giving true true And true is evaluated last, giving true 10 5 + 1 5 + 1 is evaluated first, giving 6 10 < 25 is evaluated second, giving true 6 > 6 is evaluated third, giving false true And false is evaluated last, giving false
12
Tutorial 4: The Selection Structure 12 Expressions Containing the Or Logical Operator 3 5 3 < 2 is evaluated first, giving false 6 > 5 is evaluated second giving true false or true gives true 10 5 + 1 10 < 25 is evaluated first, giving true 5 + 1 is evaluated second, giving 6 6 > 6 is evaluated third, giving false true or false is evaluated last, giving true
13
Tutorial 4: The Selection Structure 13 Operator Order of Precedence 1. Arithmetic Operators (Exponentiation, Negation, Multiplication, Division, Integer division, Modulus, Addition, Subtraction) 2. Concatenation Operators & 3. Comparison Operators ( = <> =) 4. Logical Operators (Not, And, AndAlse, Or, OrElse, Xor)
14
Tutorial 4: The Selection Structure 14 Using Logical Operators in an If…Then…Else Statement ‘ Using the AndAlso If sngHours >= 0 AndAlso sngHours <= 40 Then sngPay = sngHours * 10.65 Else MessageBox.Show("Input Error") End If ‘Using the OrElse If sngHours 40 Then sngPay = sngHours * 10.65 Else MessageBox.Show("Input Error") End If
15
Tutorial 4: The Selection Structure 15 The UCase and LCase Functions In most programming languages, string comparisons are case sensitive--in other words, the letter “A” is not the same as the letter “a” UCase is a function that converts all characters to Upper Case strData = UCase(“New York”) strData now contains “NEW YORK” LCase is a function the converts all characters to Lower Case strData = LCase(“New York”) strData now contains “new york”
16
Tutorial 4: The Selection Structure 16 The UCase and LCase Functions Determine if the variable strState is California If strState =“CA” OrElse strState = “ca” OrElse strState = “cA” OrElse strState = “Ca” Then Or you can use the UCase function If UCase(strState) = “CA” Then
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.