Visual Programming Visual Programming Statements in Visual Basic
8/7/2019 Visual Programming 3
http://slidepdf.com/reader/full/visual-programming-3 1/12
Visual ProgrammingVisual ProgrammingStatements in Visual Basic
8/7/2019 Visual Programming 3
http://slidepdf.com/reader/full/visual-programming-3 2/12
Boolean Variables and Expressions
Input boxInput box
What Are Conditional Statements?Using AND and OR in Boolean ExpressionsUsing AND and OR in Boolean Expressions
Using the NOT operator Using the NOT operator
Objectives
8/7/2019 Visual Programming 3
http://slidepdf.com/reader/full/visual-programming-3 3/12
Boolean Variables
Boolean variable stores one of the following
constant values: "True", or "False"."True", or "False".
For example:Dim Kuku As BooleanDim Kuku As Boolean
Kuku = TrueKuku = True
Dim YoYo As BooleanDim YoYo As Boolean
YoYo = FalseYoYo = False
What are the True and False stand for?
They are the result of every "Boolean expression´"Boolean expression´
8/7/2019 Visual Programming 3
http://slidepdf.com/reader/full/visual-programming-3 4/12
Examples - Boolean Expressions
Boolean expression is like a question that the answer to it is "True" True"
or "False"or "False"For example:
�Is 4 Plus 6 is 10? TrueTrue
�Is 2 bigger than 4? FalseFalseBut the question
How much is 4 Plus 6?
�Is not a boolean expression, because its answer
�is 10 (and not True or False)Examples of Boolean expressions in the next slide
8/7/2019 Visual Programming 3
http://slidepdf.com/reader/full/visual-programming-3 5/12
Examples - Boolean Expressions
The following code:
Dim ABC As BooleanDim ABC As BooleanABC = (3 > 4)ABC = (3 > 4)
Print ABCPrint ABC
Will print "False" on the form.Will print "False" on the form.
The value of the Boolean expression 3 > 4The value of the Boolean expression 3 > 4isis "False","False", becausebecause 3 is not greater than 43 is not greater than 4.
8/7/2019 Visual Programming 3
http://slidepdf.com/reader/full/visual-programming-3 6/12
� The following code:
Dim ABC As Boolean
ABC = ("abf" = "abf")Print ABC
Examples - Boolean Expressions
Will print "True" on the form.The value of the boolean expression "abf " = "abf³ is "True", because"abf " = "abf³ is "True", because
"abf " is equal to "abf ""abf " is equal to "abf "
8/7/2019 Visual Programming 3
http://slidepdf.com/reader/full/visual-programming-3 7/12
� The following code:
Dim ABC As BooleanDim ABC As Boolean
Dim A As Integer Dim A As Integer
Dim B As Integer Dim B As Integer
A = 1A = 1
B = 2B = 2
ABC = (A + 1 = B)ABC = (A + 1 = B)
Print ABCPrint ABC
Examples - Boolean Expressions
Will print "True" on the form,
because the value of the Boolean expression
(A + 1 = B) is "True".
In Boolean expressions you can use the
following signs:
= Equal to<> Not Equal to
> Greater than
< Smaller than
>= Greater than or Equal to
<= Smaller than or Equal to
8/7/2019 Visual Programming 3
http://slidepdf.com/reader/full/visual-programming-3 8/12
INPUT BOX
� InputBox
� In the next examples we want to receive a value from the user when theprogram is running.
� To do so we will use the InputBoxInputBox command.The InputBox command syntax is:
VariableName = InputBox ("Text to Display")
� After executing this command, the variable will get the value that the user hasentered.
� 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³ as in Figure 1
8/7/2019 Visual Programming 3
http://slidepdf.com/reader/full/visual-programming-3 9/12
Conditional Statements
� The syntax of the conditional statement:
If (boolean expression) ThenThe code to execute if the boolean expression is equal to True
ElseThe code to execute if the boolean expression is equal to False
End If
� Lets make a password protected program. We want to promptthe 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") ThenIf (Password = "let me in") Then
MsgBox "The Password is correct!"MsgBox "The Password is correct!"
ElseElse
MsgBox "Incorrect Password!"MsgBox "Incorrect Password!"
End If End If
8/7/2019 Visual Programming 3
http://slidepdf.com/reader/full/visual-programming-3 10/12
Using "And" and "Or " In Boolean
Expressions
� How can we make a boolean expression that will be "True"
if the Password is "let me in" or "sam sent me³ ?
Put the following code in your Form_Load event:Put the following code in your Form_Load event:
Dim Password As StringDim Password As String
Password = InputBox("Please enter the password")Password = InputBox("Please enter the password")
If (Password = "let me in") Or (Password = "sam sentIf (Password = "let me in") Or (Password = "sam sent
me") Thenme") Then
MsgBox"The Password is correct!
"MsgBox
"The Password is correct!
"
ElseElse
MsgBox "Incorrect Password!"MsgBox "Incorrect Password!"
EndEnd
End If End If
Run the program.
Lets see what effect has the "Or " operator on the boolean expression we used:Lets see what effect has the "Or " operator on the boolean expression we used:(Password = "let me in") Or (Password = "sam sent me")(Password = "let me in") Or (Password = "sam sent me")
First, the left and right boolean expressionsFirst, the left and right boolean expressionsare being evaluatedare being evaluated
For example, For example, if the Password is "let me in"if the Password is "let me in" thenthen(Password = "let me in") is True(Password = "let me in") is True
andand (Password = "sam sent me") is False(Password = "sam sent me") is False..
Then the boolean expression is look like this:Then the boolean expression is look like this:
(True) Or (False)(True) Or (False)True Or False = TrueTrue Or False = True
So the final result of the expression is True .So the final result of the expression is True .
8/7/2019 Visual Programming 3
http://slidepdf.com/reader/full/visual-programming-3 11/12
Using "And" and "Or " In Boolean Expressions
� The "And" operator is similar to the "Or³ operator , except it hasdifferent Effects on boolean expressions:
True And True = TrueTrue And False = False
False And True = False
False And False = FalseFor example, copy the following code to your Form_Load event:
Dim UserName As String
Dim Password As StringUserName = 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!"
EndEnd 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.
8/7/2019 Visual Programming 3
http://slidepdf.com/reader/full/visual-programming-3 12/12
Using The "Not" Operator
� 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 thePassword is not "let me in". Why?
If the Password is not "let me in",(Password = "let me in") is False ,
and we get the boolean expression: Not (False).Not False = True, so eventually,
the expression value is True