Page 113 - Open Soource Technologies 304.indd
P. 113
Unit 5: VB String and Operators
This example points out the need to understand how Visual Basic evaluates expressions involving
operators. In general, such expressions are evaluated left to right, and when it comes to a contest
between two operators (such as + and / in the last term of our original program), the operator
with the higher precedence is used first.
Visual Basics operator precedence, arranged by category.
Operators and
Operato
Precedence Comparison Logical
Arithmetic
Exponentiation
(^) Equality (=) Not
Negation (–) Inequality And
(<>)
Multiplication
and division Less than Or
(*,/) (<)
When expressions contain operators from more than one category, arithmetic operators are
evaluated first, comparison operators are evaluated next, and logical operators are evaluated last.
Also, comparison operators actually all have equal precedence, which means they are evaluated
in the left-to-right order in which they appear.
If in doubt, use parentheses operations within parentheses are always performed before those
outside. Within parentheses, however, operator precedence is maintained.
5.2.6. Using If & Else Statements
The If statement is the bread and butter of Visual Basic conditionals, but you can forget the syntax
every now and then (that is, is it ElseIf or Else If?), so here_s the If statement:
If condition Then
[statements]
[ElseIf condition-n Then
[elseifstatements]]...
[else
[elsestatements]]
end If
And here_s an example showing how to use the various parts of this
popular statement:
Dim intInput
intInput = -1
While intInput < 0
LOVELY PROFESSIONAL UNIVERSITY 107