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

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