You are on page 1of 21

Using Variables

and If-Then
Selection Structure

ENSC 26 Laboratory
VARIABLES AND DATA TYPES
Data Type
 A property of a variable which defines the values that the variable can take, the
programming language used, or the operations that can be performed on it.
 The following data types are used in Visual BASIC:

1.Integer: whole only numbers (decimals are automatically rounded)


2.Decimal: whole numbers and decimal numbers
3.Text: letters, words or combination of symbols
4.Date: date with specific format
5.Boolean: logic values (true or false)
6.Object: any object such textboxes, forms and other controls
VARIABLES AND DATA TYPES
Integer Data Type
 The range of values for an integer data (default value = 0) type depends directly on
the size of the binary digits that can be managed by that type; the more digits, the
bigger the range.
VARIABLES AND DATA TYPES
Decimal Data Type
 The Decimal data type (default value = 0) provides the greatest number of
significant digits for a number (up to 29 significant digits).
 It is particularly suitable for calculations that require a large number of digits but
cannot tolerate rounding errors.
VARIABLES AND DATA TYPES
Scope
 Scope determines which code can access the variable depending on its locationin
the program.
 A variable can have scope at one of the following levels:
OPERATORS
Shortcut Operators
 An operator is symbol or keyword which is used to perform a logical process.
 Together with keyword, variables, and values, it is used fo construct a formula.
 Visual BASIC uses the following general types of operators:

1. Arithmetic Operators
2. Shortcut Operators
3. Comparison Operators
4. Logical Operators
5. Concatenation Operator
OPERATORS
Arithmetic Operators
 The priority of operation is as follows: ^, *, /, \, mod, +, -.
 Example: 8 \10 / 5 mod 3 = 1
OPERATORS
Operators
 The assignment operator (=) can be preceded by an arithmetic operator (except
Mod) to create shortcut expressions

 Example: If Value = 8
OPERATORS
Comparison Operators
 Note that all comparison operators have equal precedence.
 Other comparison operators include Is, IsNot, and Likewhich shall be discussed on later
chapters.
OPERATORS
Logical Operators
 Logical operators have the following order of precedence: negation (Not), conjunction (And,
AndAlso), inclusive disjunction (Or, OrElse), and exclusive disjunction (Xor).
EXPRESSING PROGRAM ALGORITHMS
Flowcharts
 Flowchart is a graphical
representation of an
algorithm to define its
logical flow.
 It illustrates the steps of an
algorithm by using different
kinds of boxes which are
ordered by connecting
them with arrows (shown
on the right).
EXPRESSING PROGRAM ALGORITHMS
Flowcharts
 The algorithm for a Celsius-
To-Kelvin program can be
expressed as:
DECISION/SELECTION STRUCTURES
If-Then Statements
 A conditional statement used in directing program flow based on multiple decision
criteria.
 It has an equivalent to the English form:
 “If such-and-such is true, then do so-and-so.”
 General forms:
A. Single Selection
1. Single line If statement
2. If…Then
B. Multiple Selection
3. If…Then…Else
4. Nested If…Then.. Else
5. If…Then…ElseIf
DECISION/SELECTION STRUCTURES
If-Then Statements
 Single-Line Syntax:

 If…Then… Syntax:

Else
 If...Then...Else Syntax:
If

Then
DECISION/SELECTION STRUCTURES
If-Then Statements
 Nested If...Then...Else Syntax
 Nesting is the process of embedding program statements in other statements
 Below is the syntax for Nested If-Then Statements:
DECISION/SELECTION STRUCTURES
Assigning Condition in If-Then Statements
 Below are condition statements that include conditional operators and results to a
Boolean value (True or False):

 If several conditions are to be used, logical operators are used to combine conditions
into one larger condition:
SAMPLE PROBLEM
SAMPLE PROBLEM
SAMPLE PROBLEM
SAMPLE PROBLEM
SAMPLE PROBLEM

You might also like