Download presentation
Presentation is loading. Please wait.
1
Chapter 5 Decisions
2
Outline and Objectives
Relational and Logical Operators If Blocks Select Case Blocks This chapter introduces the control structure, powerful programming tool that will be used in virtually all programs from this point on.’ Control structures allow the programmer to determine whether or not a specific statements are executed. Two types of control structures: decision structure loop structure Decision structures are used to make comparisons 2
3
Condition A condition is a Boolean expression that can be either true or false Conditions can be formed by using the six Relational operators and the three Logical operators
4
Relational Operators
5
Print 1<=1 True Print 1 < 1 False Print 2 < 5 True
Examples Print 1<=1 True Print 1 < 1 False Print 2 < 5 True Print 3 <> 3 False Print 0 < True Print 5 <= 3 False
6
Examples Determine whether each of the following conditions is true or false assume that: a = 4, b = 3, c = “hello” , d = “bye” Print (a+b) < 2 * a True Print (Len(c) – b) = (a / 2) True Conditions also can involve variables, numeric operators, and functions . To determine whether a condition is true or false, first compute the numeric or string values and if the resulting is true or false
7
Using Relational Operators on Strings
Computers use a special coding system to compare character strings called ANSI With ANSI each character has a number associated with it, this number is called ANSI value of the character. The ANSI values of characters are given in following table. (Appendix A) You do not need to memorize the table Just know the numbers, small and capital letters A= a= sp= =48
8
Punctuation < Capitals letters < Small letters
8
9
Using Relational Operators on Strings
The string str1 is said to be less than the string str2 if str1 precedes str2 alphabetically when using the ANSI (or ASCII) table Two strings are compared working from left to right, character by character, to determine which one should precede the other.
10
Example of Comparing Character Strings
Print "Chase" > "Chaz" False Print " Cat" < "Cat" True Print "Pay" < "Pay " True Print "Jones" < > "James" True Print "Hope" > "Hopeful" False 10
11
Print c < (“good” & d) False
Example… Determine whether the following condition is true or false (assume that c = “hello” and d = “bye”) Print c < (“good” & d) False
12
Logical Operators The result of a logical operator is also True or False The three Logical Operators are: Not And Or
13
Not Not: Negates a single expression Example: Suppose answ = “B”
Print Not (answ = “b”) True X Not X F T
14
And Example: Suppose answ = “B”
Takes two expressions, returns True only if both expressions are evaluated as being true Example: Suppose answ = “B” Print (answ = “B”) And (answ = “b”) False X Y X And Y F T
15
Or Example: Suppose answ = “B”
Takes two expressions, returns true if either one of the expressions is evaluated as being true. Example: Suppose answ = “B” Print (answ = “B”) Or (answ = “b”) True X Y X Or Y F T
16
Chapter 5 - Visual Basic Schneider
Note: Not ( X And Y ) = Not X OR Not Y Not ( X Or Y ) = Not X And Not Y Chapter 5 - Visual Basic Schneider 16
17
Precedence First, all Arithmetic operations are carried out
Then all expressions involving >, <, and = are evaluated to true or false The logical operations are next applied in the order Not, then And, and Finally Or.
18
Examples…
20
Evaluate the following to True or False
Assume a=4, b=3 Print a*3-2=5-b False Print a=b+3<=a+b True Print not (a<2) or b<4 = a+b True Print Not(a<b)=(a>=b+2) False Be careful about the priorities
21
Notes A Condition such as 2<n<5 (not a syntax error) but should never be used, because visual basic will not evaluate it as intended. The correct condition is (2<n) And (n<5) The following conditions are equivalent: a<>b Not (a=b) a>b Not (a<=b) Not (n<m) (n>=m)
22
Are these conditions equivalent?
(wrong) correct n>=m Not(n<m); n>m (a=b) and (a<b); a<>b try a=4 b=5 (wrong ) Not ((a=b) or (a=c)) ; a<>b and a<>c (correct ) Not (a>=b); (a<b) and not (a=b) (correct ) a<b is enough Chapter 5 - Visual Basic Schneider 22
23
Write an Exp equal to the negation of
a>b a<b Or a=b , a<=b (a<b) and (c<>d) (a>=b) Or (c=d) (a=b) or (a=d) (a<>b) and (a<>d) Not ( (a=b) or (a>b) ) (a=b) or (a>b) (a<> “”) and (a<b) and (len(a) < 5) (a =“”) Or (a>=b) Or (len(a)>=5) Chapter 5 - Visual Basic Schneider 23
24
Types of Decision Structures
If Block Statement Single alternative: If...Then Compound alternative: If Then...Else Select Case Statement
25
Single Alternative Decision
An action is taken if the condition is true, otherwise the control goes to the next statement. Syntax: If condition Then action End If If condition is true, action is executed. Otherwise, action is skipped
26
Example
27
Example
28
Compound Alternative Decision
Syntax If condition Then action1 Else action 2 End If
29
Example
30
Example
31
Example
32
This is easier to understand
Example If cond1 Then If cond2 Then action(s) End If If cond1 And cond2 Then action(s) End If This is easier to understand A confusing If Block 32
33
Compound Alternative Decision
Syntax If condition1 Then action1 ElseIf condition2 Then action 2 ElseIf condittion3 Then action3 Else action4 End If This block statement searches for the first True condition, carries out its action, and then skips to the statement following end if. If none of the conditions is true, then else’s action is carried out. In general, an IF block can contain any number of ElseIf clauses. 33
34
Example
35
Single Line If Statement
Syntax If condition Then action There is no End If If the condition is true, the action will be executed If the condition is false, the (else) action will be taken 35
36
Example
37
Example
38
Examples If n <> 0 then If n Then action(s) action(s) End if
If 5 Then action(s) End If If n <> 0 then action(s) End if If n Then action(s) End If If Not(n < m) Then action(s) End If Equivalent to If (n >= m) Then action If 2 < n < 5 Then action(s) End If Wrong (not a compiler error) (n > 2) And (n < 5) 38
39
Similar to If statement Used instead of compound If statements
The Select Case Block Similar to If statement Used instead of compound If statements Action is selected from a list of alternatives Avoids confusion of deeply nested If blocks 39
40
Select Case Block (Syntax)
Select Case selector Case value-list-1 action1 Case value-list-2 action2 ….. Case Else action of last resort End Select Select case choices are determined by the value of an expression called a SELECTOR. . Each of the possible actions is preceded by statement of the form CASE valulist., where valuelist itemizes the values of the selector for which the action should be taken 40
41
a constant a variable an expression
Select Case Block Each value-list contains one or more of the following types of items separated by a comma a constant a variable an expression an inequality sign preceded by Is and followed by a constant, variable, or expression a range expressed in the form a To b, where a and b are constants, variables, or expressions. 41
42
Example
43
Example
44
Example
45
Example
46
Example
47
Example
48
Example
49
what is the output of the following code
b = 4 Select Case a = b Case 3 Print "A" Case 4 Print "B" Case Not 5 > a < 2 Print "C" Case 0 Print "D" End Select Answer: C
50
what is the output of the following code
a = "hello" Select Case a Case "Hello" Print "B“ Case "apple" To “orange" Print “C“ Case a < “orange” Print “S” Case Is <= "z" Print “A" Case "hello" Print "D" End Select Answer: C
51
what is the output of the following code
Select Case a Case 3.5 Print "A" Case a > ‘Note A>5 false 0 Print "B" Case a - 1 To a + 1 Print "C" Case 3 Print "D" End Select Answer: B
52
Chapter 5 - Visual Basic Schneider
References Chapter 5 - Visual Basic Schneider
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.