Visual Programming

Statements in Visual Basic

Objectives 
Boolean Variables and Expressions Input box What Are Conditional Statements? Using AND and OR in Boolean Expressions Using the NOT operator

Boolean Variables 
Boolean variable stores one of the following constant values: "True", or "False". For example: Dim Kuku As Boolean Kuku = True Dim YoYo As Boolean YoYo = False What are the True and False stand for? They are the result of every "Boolean expression´

Examples - Boolean Expressions 
Boolean expression is like a question that the answer to it is "True"

or "False" 
For example: ‡Is 4 Plus 6 is 10? True ‡Is 2 bigger than 4? False But 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

Examples - Boolean Expressions
The following code: Dim ABC As Boolean ABC = (3 > 4) Print ABC Will print "False" on the form. The value of the Boolean expression 3 > 4 4. is "False", because 3 is not greater than 4

Examples - Boolean Expressions
‡ The following code:

Dim ABC As Boolean ABC = ("abf" = "abf") Print ABC

Will print "True" on the form.
The value of the boolean expression "abf" = "abf³ is "True", because "abf" is equal to "abf"

Examples - Boolean Expressions
‡ The following code: Dim ABC As Boolean Dim A As Integer Dim B As Integer A=1 B=2 ABC = (A + 1 = B) Print ABC
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

INPUT BOX
‡ ‡ ‡ InputBox 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³ as in Figure 1

‡ ‡ ‡ ‡

‡ ‡

Conditional Statements
‡ The syntax of the conditional statement:

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

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: Dim Password As String Password = InputBox("Please enter the password") If (Password = "let me in") Or (Password = "sam sent me") Then Lets see what effect has the "Or" operator on the boolean expression we used: MsgBox "The Password is correct!" (Password = "let me in") Or (Password = "sam sent me") First, the left and right boolean expressions Else are being evaluated if the Password is MsgBox "Incorrect Password!" For example,"let me in") is True "let me in" then (Password = and (Password = "sam sent me") is False. False. End Then the boolean expression is look like this: (True) Or (False) End If
True Or False = True So the final result of the expression is True .

Run the program.

Using "And" and "Or" In Boolean Expressions
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.

‡

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
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

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 the Password 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

Sign up to vote on this title
UsefulNot useful