You are on page 1of 38

VISUAL PROGRAMMING

WITH VISUAL BASIC.NET

Decision Structures,
VB PPT: 02
Loop Structures and Arrays

Ishara Wimaladasa (BSc,Pg dip in IT, NDTVE)


DECISION STRUCTURES
 Used to test a condition & then to perform
different operations depending on the result of
that test
 Enable to branch the execution to different parts
of code conditionally
 Supported by VB.NET
 If…Then…End If
 Multiple-line form of If … Then … Else … End If
statement
 Single-line form of If … then … Else … End If
statement
 Select … Case statement
2
IF … THEN … ELSE STATEMENT
 Multiple-line form
 Conditionally executes a block of statements, depending on the
result of a comparison
 Syntax
If Condition Then
[Statements]
[Else
[Statements]]
End if

 If the condition evaluates True, the statements under Then clause


are executed, otherwise the statements under Else clause are
executed
3
IF … THEN … ELSE STATEMENT C ONT.

 Single-line form
 Convenient for simple conditional tests

 But not as readable as multiple-line form

 Syntax

If Conditional Then [Statements] [Else Statements]

 If multiple statements are specified, they should be


separated with colons (:)

4
RELATIONAL OPERATORS
 Used to compare two operands & then to return a
Boolean result
 If the comparison evaluates true, the operator returns true

 If the comparison evaluates false, the operator returns false

Operation Operator
Equal =
Not equal <>
Less than <
Greater than >

Less than or equal <=


5
Greater than or equal >=
OPERATOR PRECEDENCE
 Relational operators
 Have an equal level of precedence

 Precedence is less than of any math operator

 Operator are evaluated form left to right

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)

 Precedence is less than that or any math or relational operator


 If more operators with equal precedence exist in a comparison,
their operations are evaluated from left to right
 Parentheses can be used to override the default order of
precedence
10
NESTED IF STATEMENT
 Enable to evaluate several alternative conditions
& then to execute different blocks of statements,
depending on the result of such comparisons
 Syntax
If Condition Then
[Statements]
[ElseIf Condition then]
[Statements]
[Else
[statements]]
End If
 Can have multiple ElseIf clauses

11
SELECT…CASE STATEMENT TO BE CONT.

 Useful when a single expression is to be evaluated for


several different values
 An alternative to a set of nested If statements
 Evaluates the expression only once & uses it for every
comparison
 Syntax
select case Expression
[Case MathValues
[Statements]]
Case Else
[Statements]]
End Select
 Can have multiple ElseIf clauses 12
SELECT…CASE STATEMENT
 Execution
 The Value Of The Expression Is Compared With The
Match Values Of The Case Clauses In The Order They
Appear In Select… End select block
 If a match found, the corresponding statement block is executed

 If no match found, the statements under the case else clause are
executed

 Use to keyword to specify the boundaries of a range of


values to be matched

 Use is keyword with a relational operator to specify a


condition on the match values 13
LOOP STRUCTURES
 Used to execute one or more lines of code
repeatedly
 Can be two types
 Condition controlled
 Counter controlled

 A set of statements can be repeated


 Until a condition becomes true
 Until a condition becomes false
 A specified number of times, or
 For each object in an objects collection

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

 Execute a block of statements an indefinite


number of times depending on the boolean result
of a condition
 Statement can be repeatedly executed
 While the condition remains true or

 Until the condition becomes true

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.

 To store a value in an element of an array


 Syntax
Identifier (Subscript) = Value
 To retrieve the value stored in an element of an
array
 Syntax
Identifier (Subscript)

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

date 0:00:00 on 1/1/0001


object Nothing
Numeric data types 0

String Nothing
26
INITIALIZING ARRAYS
 Arrays can also be initialized explicitly at their
declaration
 Syntax

Dim Identifier ( ) As [DataType] = {Value 1, Value2, Value3}

 Valuen is the initialization value for element n – 1

 Should not specify the upper bound, if initialized explicitly

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

 Dimensionality or rank means the number of


subscripts used to identify and individual element

 VB.NET supports arrays up to 32 dimensions


 Declaration syntax for a two dimentional array

Dim Identifier (Row, column) [As DataType]

 Every dimension should have a non-zero length

 If Data Type is omitted, it will be object by default

29
MULTIDIMENSIONAL ARRAYS C ONT.

 To store a value in an element of a two


dimentional array
 Syntax
Identifier (RowSubscript, Columnsubscript) = Value

 To retrieve the value stored in an element of a two


dimensional array

 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

Dim Identifier (,) As [DataType] = _

{{ValueList1}, {ValueList2}, … , {ValueListn}}

 If there are m columns and n rows, value list is a comma


separated list of m values for each row.
 Should not specify the upper bounds, if initialized explicitly
31
ARRAY CLASS

 In VB.NET, any array declared inherits from the


system.Array base class

 Provides properties and methods for


 Creating

 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

You might also like