You are on page 1of 24

VISUAL BASIC PROGRAMMING

PART 2
Strings
 A string is any combination of letters, numbers or
symbols surrounded by double quotes " "
 Two strings are considered equal if and only if
each and every character is identical
– “Bob” is equal to “Bob”
– but not equal to “BOB”, “bob” or “Robert”
 Strings can be manipulated in assignment
statements
Review of Comparisons
 Comparisons can be made on both numeric and
string data types
 The comparison operators in Visual Basic are
– < Less Than
– > Greater Than
– = Equal
– <= Less Than or Equal
– >= Greater Than or Equal
– <> Not Equal
Review of Comparisons
 Sometimes, a simple test is not adequate for a
conditional branch or loop instruction. We need a
more complex test that combines the results of
multiple simple tests
 In this case, we can use the logical (Boolean)
operators to combine test results
 The logical operators, in order of precedence, are
• AND: returns a True only when both conditions are True
• OR: returns a True when either or both conditions are True
Review of Comparisons
 Let X = 5, Y = 6

(X > 0) OR (Y >7) (TRUE) OR (FALSE)


TRUE

(X < 5) AND (Y > 0) (FALSE) AND (TRUE)


FALSE

(10 > X) AND (X > 4) (TRUE) AND (TRUE)


TRUE
Conditional Branch Instructions
 IF statement
IF (condition) THEN
instruction block
END IF
 Condition is a variable or expression which must
evaluate to either True or False
 If the condition is True, then execute the
instruction block
 If the condition is False, do not execute the
instruction block
Write a VB program to enter the value of X. If X > 50, the
program will display " X is greater than 50 ".
Write a VB program that will order the values of X and Y so
that the smaller value will be in X and the larger value will be
in Y.
Conditional Branch Instructions
 Are the results identical when these two code fragments are executed?

IF (big > small) THEN IF (big > small) THEN


big = small big = small
small = 0 END IF
END IF small = 0

In the left code fragment, small becomes zero


only when big is greater than small.
In the right code fragment, small always
becomes zero
Conditional Branch Instructions
 IF statement with an ELSE clause

IF (x > 5) THEN IF (x > 5) THEN



do instruction block 1
What if we need multiple related tests?
do instruction block 1
END IF ELSE
IF(x <= 5) THEN do instruction block 2
do instruction block 2
END IF
END IF
Conditional Branch Instructions
 IF statement with ELSEIF clauses
IF (condition 1) THEN
Do instruction block 1
ELSEIF (condition 2) THEN
Do instruction block 2
ELSEIF (condition 3) THEN
Do instruction block 3
ELSE
Do instruction block 4
END IF
 Can include any number of ELSEIF clauses
 The ELSE clause may be omitted
Write a VB program to add 2 numbers and check if
the Total is greater than or equal to 50 or not.
Conditional Branch Instructions
Write a VB program to assign
the correct grade to an exam
mark
A if exam mark is 90 or
above
B if exam mark is 80 to 89
C if exam mark is 70 to 79
D if exam mark is 60 to 69
F if exam mark is below 60
Ticket Example
Write a VB program which calculates the amount of money
to charge for a ticket. The amount varies with the age of
the
individual. The charge for a person less than 16 is $7. The
charge for a person over age 65 is $5. The charge is $10
for everyone else.
VB code - Ticket Example
Medical Example
Given an employee’s eligible medical expenses for a
calendar year, write a VB program to compute the amount of
reimbursement from group medical insurance. The
insurance does not cover the first $100 of medical expenses.
It pays 90% of the remaining amount in the first $2000 of
expenses and 100% of any additional expenses.
VB code –Medical Example
FOR - NEXT
 The FOR - NEXT loop is used to loop a specific
number of times (there is no condition to test at
either the beginning or end of the loop)

FOR J = 1 TO 4
Instruction block
NEXT J

 We will execute the instruction block 4 times


FOR - NEXT
FOR counter = start TO end
Instruction block
NEXT counter
 Counter is a variable that represents an integer
 Start is the first integer value assigned to counter
 End is the last integer value assigned to the counter
 By default, each time through the loop, the counter is
incremented by 1
 NEVER CHANGE THE VALUE OF THE
COUNTER INSIDE THE LOOP
FOR - NEXT

 A variation is:

FOR counter = start TO end STEP increment


Instruction block
NEXT counter

 Increment can be a positive integer (count up)


 Increment can be a negative integer (count down)
FOR - NEXT
 How many times would this loop execute?
For J = 1 To 5 Step 5
Instruction block
Next J

 Will be executed one time only.


Write a VB program to enter five numbers and  
find their total.
Write a VB program to enter five numbers and  
find the largest number among five numbers entered
by the user
Write a VB program to enter 10 numbers and  
find their average

You might also like