Relational Operators > greater than < less than >= greater than or equal to <= less than or equal to <> not equal to = equal to
Conditions Conditions are expressions that involve relational operators Example: A < B, A <> B, etc. where A, B are either numbers, or strings
More complex conditions More complex conditions can be formed using expressions involving simple conditions and logical operators. cond1 And cond2 cond1 Or cond2 Not cond1
What does A < B mean for two strings A, B? A precedes B lexicographically in the ANSI character set (as in a dictionary)
Controlling Program Flow Conditional Execution If condition Then do action1 Else action2 End If
Pseudo code If condition is true Then execute code 1 Else execute code 2 End If
Example Given two numbers A and B, print A if A < B, and print B if B <= A If A < B Then Print A Else Print B End If
The Else part can be absent Example: If A < B Then Print A End If
Pseudo code for another example: Input two numbers A, B If A < B then print the difference A-B Else print the difference B-A Lets consider the code below:
Private Sub Command1_Click() Dim A, B, D As Integer A = Text1.Text B = Text2.Text If A < B Then D = B - A Print D Else D = A - B Print D End If End Sub
Private Sub Command1_Click() Dim A, B, D As Integer A = Val(Text1.Text) B = Val(Text2.Text) If A < B Then D = B - A Print D Else D = A - B Print D End If End Sub
Another If statement can be in the Then or Else part of the If statement Pseudo code: Input two numbers A, B If A < B Then Print A-B Else If A > B Then Print B-A Else Print A and B are equal
Private Sub Command1_Click() Dim A, B As Integer A = Val(Text1.Text) B = Val(Text2.Text) If A < B Then Print B - A Else If A > B Then Print A - B Else Print "A and B are equal" End If End If End Sub
The ElseIf clause If there are more than two alternatives, the ElseIf clause is a cleaner, more readable way to write the code It is equivalent logically to the previous code
If cond1 Then action1 ElseIf cond2 Then action2 ElseIf cond3 Then action3 Else action4 End If
Private Sub Command1_Click() Dim A, B As Integer A = Val(Text1.Text) B = Val(Text2.Text) If A < B Then Print B - A ElseIf A > B Then Print A - B Else Print "A and B are equal" End If End Sub
What if we had more alternatives? If cond1 Then action1 ElseIf cond2 Then action2 ElseIf cond3 Then action3 Else action4 End If
Select Case Statement If we have to choose among a set of alternatives, a Select Case statement is cleaner, more readable Select Case statement is equivalent to If statement with ElseIf
Select Case Statement Select Case selector Case valuelist1 action1 Case valuelist2 action2.. Case Else action for fall through End Select
Select Case choices are determined by the value of an expression called selector Case valuelist1 action1 action1 is carried out if the value of selector lies in the range specified in valuelist1
valuelist1, valuelist2, etc. can be one or more of the following types of items seperated by commas: A constant A variable An expression An inequality sign preceded by Is and followed by a constant, variable, or expression A range expressed as a To b, where a,b are constants, variables or expressions
Example Select Case Grade Case Is > 90 yourgrade = A Case Is >= 80 yourgrade = B End Select If Grade is 95, it lies in both ranges > 90, and >= 80. The first valuelist item containing the selector is chosen, and corresponding action carried out
Example Select Case Grade Case Is > 90 yourgrade = A Case Is > 80 yourgrade = B Case Is > 70 yourgrade = C Case Else Please retake the Final End Select
Private Sub Command1_Click() Select Case Val(Text1.Text) Case Is > 90 Print "your grade is A" Case Is > 80 Print "your grade is B" Case Is > 70 Print "your grade is C" Case Is > 60 Print "your grade is D" Case Else Print Please retake Final" End Select End Sub
Case Else (and corresponding action) is optional Can we replace above Select Case by the following If Then statements? If Val(Text1.Text) > 90 Then Print your grade is A If Val(Text1.Text) > 80 Then Print your grade is B...