|
Controling
program Flow
Making
decisions using the If keyword
At
some point in the code you may need to test some condition and the,
depending on the results of that test, perform different operations.
The decision structures in Visual Basic are:
|
1.
The If …Then statement
Syntax:
If
[condition] Then [statement]
Or
If
[condition] Then
[statements]
End
If
|
2. The If
… Then … Else statement
Syntax:
If
[condition1] Then
[statementblock-1]
ElseIf condition2 Then
[statementblock-2]] ...
Else
[statementblock-n]]
End If
|
Example: This example
demonstrates the use of variables, decision statements and operators
usage.
|
Dim
Number As Integer, UserNumber As String 'declare the variables
Randomize 'throw the dice
'we select a random number and then we make it an
integer
Number = Int(Rnd * 100) + 1 'select the number and
exculde 0
Do 'here starts the main program loop
DoEvents
UserNumber =
InputBox("Enter a number", "Guess the
number")
If UserNumber
<> "" Then 'we verify if the user has entered
a value
If
IsNumeric(UserNumber) Then
'we use the
IsNumeric function to verify if the
'user has
entered an valid numeric value
If UserNumber
= Number Then
'if the number
equals the number
'the computer
has selected
MsgBox
"You have guessed the number!"
Exit Sub
ElseIf
UserNumber > Number Then
'If the number
is too big prompt the
'user that he
has entered a wrong value
MsgBox
"Your number is too big."
ElseIf
UserNumber < Number Then
'If the number
is too small prompt the
'user that he
has entered a wrong value
MsgBox
"Your number is too small."
End If
End If
Else
Exit Sub 'we
have an invalid value so exit the program
End If
Loop
|
The Select Case Statement
Select case almost
alike If … Then … Else statement. This statement is used when you
have to evaluate one expression and depending on the result, perform a
certain action.
The syntax is very simple and easy to understand:
Select Case
[expression to test]
Case [expressionlis1]
[statement list1]
Case [expressionlis2]
[statement list2]
…
Case [expressionlisn]
[statement listn]
Case Else
[statement]
End Select
Each expressionlist
is a list of one or more values. If there is more than one value in a
single list, the values are separated by commas. Each statementblock
contains zero or more statements. If more than one Case matches the
test expression, only the statement block associated with the first
matching Case will execute. Visual Basic executes statements in the
Case Else clause (which is optional) if none of the values in the
expression lists matches the test expression.
Note: Select case
function evaluates an expression once at the beginning, while the
If…Then…Else evaluates an expression for each ElseIf statement. So
if you have only one expression is recommended that you use the Select
Case block instead of the If..Then..Else block, because it’s faster
and more easy to write.
In the next example
we will use:
Constants,
Enumeration of constants, Select case block
This ends the
programming basics chapter of out site press here to go further. |