Wednesday, 10 August 2011

Using "Select Case"

0 comments
 
The "Select Case" conditional statement
is very useful when you have in your conditional
statement many conditions.

For example, in the last page we had the following code:


Dim Name As String
Name = InputBox("Please enter your name")
If (Name = "elvis") Then
   MsgBox "your name is elvis"
ElseIf (Name = "tim") Then
   MsgBox "your name is tim"
ElseIf (Name = "john") Then
   MsgBox "your name is john"
ElseIf (Name = "steve") Then
   MsgBox "your name is steve"
Else
   MsgBox "I don't know you"
End If


When you have many conditions, the "If Statement"
become too bulky.
In this case, you can use instead of it the "Select Case Statement".
The following "Select Case Statement" is do EXACTLY the same
thing as the code above:

Dim Name As String
Name = InputBox("Please enter your name")
Select Case Name
    Case "elvis": MsgBox "your name is elvis"
    Case "tim": MsgBox "your name is tim"
    Case "john": MsgBox "your name is john"
    Case "steve": MsgBox "your name is steve"
    Case Else: MsgBox "I don't know you"
End Select



The "Select Case" syntax is very simple:

It Begins with:
Select Case VariableName
Then comes the conditions.
Every condition has "Case" before it, and ":" after it.
The "Select Case" conditions are little different from the "If" conditions:


The Select Case Condition   The equivalent "If" Condition
Case "elvis" VariableName = "elvis"
Case Is <> "elvis" VariableName <> "elvis"
Case 5 VariableName = 5
Case Is <> 5 VariableName <> 5
Case Is > 5 VariableName > 5
Case Is >= 5 VariableName >= 5
Case Is < 5 VariableName < 5
Case Is <= 5 VariableName <= 5


After every condition, comes the line that will be executed
if the condition will be "True".

For example, the line:

Case "elvis": MsgBox "your name is elvis"

Is the same as:

If (VariableName = "elvis") Then  
    MsgBox "your name is elvis"
End If



After all conditions, comes the Else condition (It's
optional condition, and you can omit it if you want):
Case Else:

This condition will be executed ONLY if none
of the conditions above was executed.

After all of the conditions, the statement closed with   End Select


Note that you can have more than 1 line to executed
in each condition. For example:

Dim Name As String
Name = InputBox("Please enter your name")
Select Case Name
    Case "elvis": MsgBox "your name is elvis"
                  MsgBox "you are the king!"
                  MsgBox "I didn't know you are alive!"
    Case "tim": MsgBox "your name is tim"
    Case "john": MsgBox "your name is john"
    Case Else: MsgBox "I don't know you"
End Select
Readmore...

Nested Conditional Statements

0 comments
 
Untill now, we could program simple conditional statements:
If the password is xxx - do something,
and if not - do other thing.

But what if we want to write more complicated conditional statements:
If the password is xxx - do something,
If the password is yyy - do something else,
If the password is zzz - do something else,
and if the password is none of the above - do something else.


To do that we'll use "ElseIf":

Dim Number As Integer
Number = InputBox("Please enter your age")
If (Number > 50) Then
   MsgBox "you are older than 50"
ElseIf (Number < 20) Then
   MsgBox "you are younger than 20"
Else
   MsgBox "your age is between 20 and 50"
End If



How does that code above work:

First, the first boolean expression is being evaluated:
(Number > 50)
If it's equal to True then the line that following it
(MsgBox "you are older than 50") will be executed, and
the "If statement" will be ended here, so the next
boolean expression (Number < 20) will not be checked.
If it's equal to False, the next boolean expression will be checked:
(Number < 20)
If it's equal to True, the line that following it
(MsgBox "you are younger than 20") will be executed, and
the "If statement" will be ended here.
If it's equal To False, the "Else Statement" will be executed:
MsgBox "your age is between 20 and 50"

Only 1 of the lines will be executed!


You can use more than one "ElseIf" in your "If Statement",
For example:

Dim Name As String
Name = InputBox("Please enter your name")
If (Name = "elvis") Then
   MsgBox "your name is elvis"
ElseIf (Name = "tim") Then
   MsgBox "your name is tim"
ElseIf (Name = "john") Then
   MsgBox "your name is john"
ElseIf (Name = "steve") Then
   MsgBox "your name is steve"
Else
   MsgBox "I don't know you"
End If
Readmore...

More Advanced Boolean Expressions

0 comments
 
ou can combine the operators "Not", "And" and "Or" in
the same boolean expression, using the parentheses "(" and ")"

For example:

(True And False) Or (Not False) = True

The priority of the "Not" operator is higher than
the priority of "And" and "Or" operators.

For example:

Not True And False = False, because
The first operater to be executed is the "Not" (Figure 6):
Not True = False.
Then we get the following boolean expression:
False And False = False.
If we will executed the "And" operator before
the "Not" operator, we would get WRONG answer

A Teaser:
What will be printed on the form after
executing the following code?

Dim Elvis As Boolean
Elvis = (Not False And True) And Not ((True And Not False) Or False)
Print Elvis


Answer:
 







Readmore...

Using The "Not" Operator

0 comments
 
The "Not" operator has very simple function:

Not True = False
Not False = True


Before we continue,
I'll mention that you don't have to include
"Else" in the "If" statement.

For example:

Dim Password As String
Password = InputBox("Please enter the password")
If (Password = "let me in") Then
   MsgBox "The Password is correct!"
End If



If the password is correct, The code above
will display a message box with the text
"The Password is correct!".
Otherwise, the code will do nothing,
because there is no "Else".


Lets see an example of using the operator "Not".
Copy the following code to your Form_Load event:

Dim Password As String
Password = InputBox("Please enter the password")
If Not (Password = "let me in") Then
   MsgBox "Incorrect Password!"
   End
End If



The boolean expression: Not (Password = "let me in")
is True only if the Password is not "let me in". Why?
If the Password is not "let me in",
(Password = "let me in") is False (Figure 3) ,
and we get the boolean expression: Not (False).
Not False = True, so eventually, the
expression value is True
In conclusion,
The code above will display a message box
with the text "Incorrect Password!" ONLY if
the password is different from "let me in"
Readmore...

Using "And" and "Or" In Boolean Expressions

0 comments
 
We can use "And" and "Or" operators to make more   complicated boolean expression.

In the last example we used the boolean expression Password = "let me in".

This boolean expression is "True" if the Password is "let me in", and "False" If the Password is not.

How can we make a boolean expression
that will be "True" if the Password is
"let me in" or "sam sent me", and if the
Password is other than the two above, the
boolean expression will be "False"?

To create this boolean expression we will use the "Or" operator:

(Password = "let me in") Or (Password = "sam sent me")

Put the following code in your Form_Load event:

Dim Password As String
Password = InputBox("Please enter the password")
If (Password = "let me in") Or (Password = "sam sent me") Then
   MsgBox "The Password is correct!"
Else
   MsgBox "Incorrect Password!"
   End
End If



Run the program.
If you'll type "let me in" or if you'll type "sam sent me"
the password will be correct.
If you'll type any other text the program will shut down.


The "Or" is operator, the same as "+" is operator.
How the "+" operator works we already know:
5 + 6 = 11
But how the "Or" Operator works?

The "+" operator is being executed on numbers:
Number + Other Number = The sum of both numbers.

The "Or" operator is being executed on "True" or "False":
False Or True = True
True Or False = True
True Or True = True
False Or False = False

The 4 examples above are the only options
of using the "Or" operator.

Lets see what effect has the "Or" operator on
the boolean expression we used:

(Password = "let me in") Or (Password = "sam sent me")

First, the left and right boolean expressions
are being evaluated (Figure 2).

For example, if the Password is "let me in" then
(Password = "let me in") is True
and (Password = "sam sent me") is False.

Then the boolean expression is look like this:
(True) Or (False)

As we saw in the 4 examples above, True Or False = True
So the final result of the expression is True

This boolean expression will be True if
the password is "let me in" because True Or False = True,
the expression will be True if the password is "sam sent me"
because False Or True = True,
the expression will be False if the password is NOT "let me in"
and NOT "sam sent me" because False Or False = False.

The "And" operator is similar to the "Or"
operator, except it has different Effects on boolean expressions:

True And True = True
True And False = False
False And True = False
False And False = False

When using the "And" operator, the
expression will be True only if both
boolean expressions are True.

For example, copy the following code
to your Form_Load event:


Dim UserName As String
Dim Password As String

UserName = InputBox("Please enter the user name")
Password = InputBox("Please enter the password")
If (Password = "let me in") And (UserName = "elvis") Then
   MsgBox "The login is correct!"
Else
   MsgBox "Incorrect Login!"
   End
End If



This code will pop up two InputBoxes.
The first will ask you to enter the user name,
and the second will ask you to enter the password.
The login will be correct only if both user name
and password are correct.

The boolean expression (Password = "let me in") And (UserName = "elvis")
is True only if (Password = "let me in") is True, and
(UserName = "elvis") is True, because
True And True = True, and any other
combination is equal to False.
Readmore...

The syntax of the conditional statement:

0 comments
 
If (boolean expression) Then
    The code to execute if the boolean expression is equal to True
Else
    The code to execute if the boolean expression is equal to False
End If


Lets make a password protected program.
We want to prompt the user to enter the password
at the very start of the program, so put the following
code in the Form_Load event:


Dim Password As String
Password = InputBox("Please enter the password")
If (Password = "let me in") Then
   MsgBox "The Password is correct!"
Else
   MsgBox "Incorrect Password!"
End If



Run the program.
An InputBox is appearing.
Type "let me in" (without the quotes) in the InputBox.
Note that the text is case sensitive, and if you will
type "LET ME IN" you will get a message box with the
text "Incorrect Password!".


Lets understand how this code works:

The boolean expression (Password = "let me in") value is True
ONLY if the Password variable value is "let me in".

If this boolean expression's value is True, the code
that will be executed is the code that located between
the    Then    and the   Else   (MsgBox "The Password is correct!")

If this boolean expression's value is False, and this
happens when the Password variable value is NOT "let me in",
the code that will be executed is the code that located between
the    Else    and the   End If   (MsgBox "Incorrect Password!")


If you enter a wrong password, a message box is appearing,
but the program is continue running.
To end the program, use the command   End  .
This command is simply shut down the program.

Put the End command in the "Else Section":


Dim Password As String
Password = InputBox("Please enter the password")
If (Password = "let me in") Then
   MsgBox "The Password is correct!"
Else
   MsgBox "Incorrect Password!"
   End
End If
Readmore...

InputBox

0 comments
 
In the next examples we want to receive a value
from the user when the program is running.
To do so we will use the InputBox command.

The InputBox command syntax is:

VariableName = InputBox ("Text to Display")

After executing this command, the variable
will get the value that the user has entered.

Example:
Put 1 Command button on your form and
enter the following code to its click event:

Dim Elvis As String
Elvis = InputBox("Please Enter your name")
Print Elvis



Run the program and click the button.

A Message Box is appearing with the text "Please Enter your name"
 Type your name in the text box, and press OK. Your name will be printed on the form.
Readmore...