VB - Basic Operators, Arrays & Making Decision

Page 1

BASIC OPERATORS  Visual Basic also provides operators for

mathematical, comparison and logical calculation. In general, the program evaluates mathematical expressions in the following order:  From left to right  Follows the rules of precedence from basic math

 Here is the list, from highest precedence to lowest:     

Exponentiation (^) Multiplication and division (*, /) Integer Division (\) Modulus (Mod) Addition and subtraction (+, -)


BASIC OPERATORS  Example  Multiplication and division are performed first from left to right. Then addition and subtraction are performed. For example, 5 + 10 * 3 = 5 + 30 = 35.  You can change this order of precedence by using parentheses. For example, (5 + 10) * 3 = 15 * 3 = 45. If you are unsure of the order of precedence, it is a good idea to clarify your intentions with parentheses.


ARRAYS  By definition, an array is a list of variables, all with

the same data type and name.  When we work with a single item, we only need to use one variable.  However, if we have a list of items which are of similar type to deal with, we need to declare an array of variables instead of using a variable for each item.  For example, if we need to enter one hundred names, instead of declaring one hundred different variables, we need to declare only one array.  We differentiate each item in the array by using subscript, the index value of each item, for example name(1), name(2),name(3) .......etc.


ARRAYS  We could use Public or Dim statement to

declare an array just as the way we declare a single variable.  The Public statement declares an array that can be used throughout an application while the Dim statement declares an array that could be used only in a local procedure.  The general format to declare an array is as follow: Dim arrayName(subs) As dataType Where subs are indicates the last subscript in the array.


ARRAYS  Example

Dim CusName(10) As String  Statement above will declare an array that consists of 10 elements if the statement appear in the declaration area, starting from CusName(1) to CusName(10).  Otherwise, there will be 11 elements in the array starting from CusName(0) through to CusName(10)

 Example

Dim Count(100 to 500) As Integer  Statement above declares an array that consists of the first element starting from Count(100) and ends atCount(500)


MAKING DECISIONS  CONDITIONAL STATEMENTS  So far, all the programs we have worked on display menus, objects and dialog boxes on the screen in response to an action from the user, such as clicking a command button.  Now, we will learn to use decision statements to compare variables, properties and values, and to execute one or more statements based on the results.


MAKING DECISIONS  If/Then STATEMENT

 Visual Basic provides an If/Then statement that you can use to make decisions in program.  A decision “chooses” between different program paths depending on whether a specified condition is true or false.

Example If x = 7 Then result. text = “7 is a lucky number”  will produce an output with the message “7 is a lucky number” if the variable x equal to 7.  Otherwise there is no output.


MAKING DECISIONS  If/Then/Else STATEMENT From the example above, to also produce an output in the event that x is not equal to 7, add an Else statement: If x = 7 Then result. text = “7 is a lucky number” Else result. Text = “try again” will produce the message “try again” in an output box if variable x is not equal to 7.


MAKING DECISIONS  The syntax for a conditional statement is If condition Then doThis [Else doThat] the Else part is optional.

 In conditional statements, comparison operators are usually used to make comparisons.


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.