Professional Documents
Culture Documents
Decision Structures,
VB PPT: 02
Loop Structures and Arrays
Single-line form
Convenient for simple conditional tests
Syntax
4
RELATIONAL OPERATORS
Used to compare two operands & then to return a
Boolean result
If the comparison evaluates true, the operator returns true
Operation Operator
Equal =
Not equal <>
Less than <
Greater than >
6
LOGICAL OPERATORS
Compare Boolean expressions & return a Boolean
result
And operator
Performs logical conjunction on two Boolean expressions
If both expressions evaluate true, then the and operator
returns true, otherwise False
Or operator
Performs logical disjunction on two Boolean expressions
If at least expressions evaluate true, then the or operator
returns true, otherwise False
7
LOGICAL OPERATOR
Not operator
Performs logical negation on a Boolean expression
Yields the opposite of the expression in evaluates
Xor operator
Perform logical exclusion on two expressions
If at least one expression evaluates true, but not both,
then the Xor operator returns true otherwise false
8
SHORT-CIRCUITING LOGICAL
OPERATORS
Result in faster execution code
andAlso operator
Similar to the And operator
If the first expression evaluates false, then the second
expression is never evaluated & the AndAlso operator returns
false
orElse operator
Similar to the or operator
If the first expression evaluates true, then the second
expression is never evaluated & the OrElse operator returns
true
9
OPERATOR PRECEDENCE
Logical operators
Precedence in descending order
Negation (Not)
Conjunction (And, AndAlso)
Disjunction (Or, OrElse, Xor)
11
SELECT…CASE STATEMENT TO BE CONT.
If no match found, the statements under the case else clause are
executed
14
WHILE…END WHILE STATEMENT
Executes a block of statements an indefinite
number of times depending on the Boolean result
of a condition
Syntax
While Condition
[Statements]
End while
Loop continues while the condition remains true
Checks the condition before begin each repetition
Exit While statement can be used to break the loop
immediately
15
DO…LOOP STATEMENTS
16
DO WHILE…LOOP STATEMENT
Used for pre-condition repetition
Similar to While…End while loop
Syntax
Do while Condition
[Statements]
Loop
Loop continues while the condition remains true
Checks the condition before begin each repetition
Exit do statement can be used to break the loop
immediately
17
DO…LOOP WHILE STATEMENT
Used for post-condition repetition
Syntax
Do
[Statements]
Loop while Condition
Loop continues while the condition remains true
Checks the condition after each repetition
Statements in the loop will always be executed at least
once
Exit Do statement can be used to break the loop
immediately
18
DO UNTIL…LOOP STATEMENT
Used for pre-condition repetition
Syntax
Do until Condition
[Statements]
Loop
Loop continues until the condition becomes true
Checks the condition before begin each repetition
Exit Do statement can be used to break the loop
immediately
19
DO…LOOP UNTIL STATEMENT
Used for post-condition repetition
Syntax
Do
[Statements]
Loop until Condition
Loop continues until the condition becomes true
Check the condition after each repetition
Statements in the loop will always be executed at least
once
Exit Do statement can be used to break the loop
immediately
20
FOR…NEXT STATEMENT
Execute a block of statements a specific number of
time
Syntax
For counter [As DataType] = Start To end [Step Step]
[Statements]
Next [Counter]
Counter should be a variable of numeric data type
Can be declared outside or as a part of the loop
Incremented or decremented after each repetition
Step specifies the amount by which the counter is incremented
or decremented
If omitted, 1 is assumed default
21
FOR…NEXT STATEMENT C ONT.
Syntax
Start & End specify the initial & the final values of the
counter respectively
Loop continues until the counter exceeds End
Exit For statement can be used to break the loop
immediately
22
FOR EACH…NEXT STATEMENT
Executes a block of statements for each object in
an objects collection
Syntax
For Each ObjectVariable [As DataType] In collection
[Statements]
Next [ObjectVariable]
objectVariable should be of the collection’s data type
Can be declares outside or as a part of the loop
Set to an object in the collection for each repetition
Loop continues for all the objects in the collection
Exit For statement can be used to break the loop
immediately
23
ARRAYS
Array
Single variable with many compartments to store the
values of same data type
Enables to refer the values stored in these
compartments by using the same identifier and a
number called index or subscript
Declaration Syntax
Dim Identifier (UpperBound) [As DataType]
UpperBound specifies upper-bound of the array
Lower-bound is 0 by default and can not be changed
Declares an array of upperBound + 1 elements, with subscripts
form 0 through upperBound
If data type is omitted, it will be object by default
24
ARRAYS C ONT.
25
DEFAULT INITIALIZATION VALUES
Array element that are not initialized at the
declaration are automatically initialized to a default
value
Data Type Default Value
Boolean False
char 0
String Nothing
26
INITIALIZING ARRAYS
Arrays can also be initialized explicitly at their
declaration
Syntax
27
RESIZING ARRAYS
Arrays can be resized at any time by using the
ReDim statement
Helps to manage memory efficiently
Syntax
ReDim [Preserve] Identifier (NewUpperBound)
newUpperBound should be a non-negative integer
If preserve is omitted, the exiting values are lost and the
array will be initialized to a default value
Preserve keyword preserves all the possible exiting
values
Can only be used at method level
28
MULTIDIMENSIONAL ARRAYS
An array can have more than one dimensions
29
MULTIDIMENSIONAL ARRAYS C ONT.
Syntax
Identifier (RowSubscript, Columnsubscript)
30
INITIALIZING MULTIMENSIONAL
ARRAYS
Elements that are not initialized at the declaration
are implicitly initialized to a default value
Explicit initialization
Syntax for a two dimensional array
Manipulating
Sorting and
Searching arrays
32
PROPERTIES OF ARRAY CLASS
Length
Gets an integer that represents the total number
of element in all the dimensions
Syntax
Identifier.Length
Rank
Gets the number of dimensions
Syntax
Identifier. Rank
33
PROPERTIES OF ARRAY CLASS CONT.
Clear
Initializes a range of element to default value depending on
the data type
Syntax
Array.Clear (Identifier, Index, Length)
Identifier is the array identifier
Index specifies the starting index of the range of
element to clear
Length specifies the number of element to clear
34
METHODS OF ARRAY CLASS
Get length
Gets the number of element in the specified dimension
Syntax
Identifier.GetLength (Dimension)
Dimension should be a non-negative integer
GetUpperBound
Gets the upper bound of the specified dimension
Syntax
Identifier.GetUpperBound (Dimension)
Dimension should be a non-negative integer
35
METHODS OF ARRAY CLASS CONT.
Sort
Sorts the elements in an one-dimensional array
Syntax
Array.sort (identifier)
Identifiers is the array identifier
binarySearch
Searches an one-dimensional sorted array
Syntax
Array .BinarySearch (Identifier,Value)
Identifier is the array identifier
Value specifies the value to search for
If found, returns index of the element that contains
the value, otherwise a negative integer 36
END OF THE LECTURE
Any
REFERENCE
https://youtu.be/mts_K96vpAE
https://youtu.be/g4ngY2VBEUg