Programming (OLD) - Visual Basic Control Structures

 CBSE Class 12 Informatics Practices

Revision Notes
Chapter - 5
Visual Basic Control Structures

CONTROL FLOW: Control structures allow you to regulate the flow of your program's execution. Using control structures, you can write Visual Basic code that makes decisions or that repeats actions.
1) SEQUENTIAL:
It means the statements are being executed sequentially. All statments is executed by one by one.  This represents default flow of statements.

2) SELECTION (Decision construct)
Selection construct select their course of action depending upon the result of a condition's or Boolean expression's. VB compiler firslty check condition if condition is true statment 1 is executed other statement 2 is executed.

3) ITERATION (Repetition)/ Looping:
Iteration means repetition of a set-of-statement depending upon a condition test. A loop is a set of statements that are repeated a certain number of times. The block of statements written in between the loop gets executed as many number of times as the user wants. 

Rules for using Iteration: The 3 steps involve in a loop are:
- Initialization - of counter variable
- Test condition - for testing the value of the counter variable
- Updation - Increase/decrease the value of counter variable

Two Forms of SELECTION CONSTRUCT - 
Two Way Branching
IF Statement
> If .... Then End If
> If ... Then ... Else .... End If
> Nested If
> If.. Then .. Else If ... End IF
> IIF (exp1, <true>, <false>)

Multi Way Branching
Select Case: It can be used with the following 3 forms:
> Exact Match
> A Relational Case Match
> A Range of Case Match

Various Forms of LOOPING CONSTRUCT -
¦ Entry controlled / Pre tested / Top tested Loops:

Visual Basic loop structures allow you to run one or more lines of code repetitively. You can repeat the statements in a loop structure until a condition is True, until a condition is False, a specified number of times, or once for each element in a collection.

NOTE: This type ofloops executed for zero or more times.
> While Wend
> Do while Loop
> Do Until Loop
> For Next

While Loops

The While....End While construction runs a set of statements as long as the condition specified in the Whilestatement is True.

Do Loops

The Do....Loop construction allows you to test a condition at either the beginning or the end of a loop structure. You can also specify whether to repeat the loop while the condition remains True or until it becomes True 

For Loops

The For...Next construction performs the loop a set number of times. It uses a loop control variable, also called a counter, to keep track of the repetitions.

For Each Loops

The For Each...Next construction runs a set of statements once for each element in a collection.

ARRAY: Arrays is a group of homogenous(Similar) elements.
• An array is a collection of variables of the same data type that are referenced by a common name.

Arrays are of Two Types:
1) Fixed / Static size Array: Those arrays in which the no. of elements is fixed and does not vary.
2) Dynamic size Array: Dynamic arrays are those arrays whose no. of elements can be varied. Declaration of Array:
Fixed size - Dim ArrayName(size) as DataType Eg: Dim arr(4) as Integer
Dynamic size - Dim ArrayName( ) as DataType Eg: Dim arr1( ) as Integer

Example :

'Declare a single-dimension array of 5 values  
Dim numbers(4) As Integer   

‘Declare a single-dimension array and set array element values  
Dim numbers = New Integer() {1, 2, 4, 8}  

 ‘Redefine the size of an existing array retaining the current values  
ReDim Preserve numbers(15)  

 ‘Redefine the size of an existing array, resetting the values  
ReDim numbers(15)  

‘Declare a multi-dimensional array  
Dim matrix(5, 5) As Double  

‘Declare a multi-dimensional array and set array element values  
Dim matrix = New Integer(4, 4) {{1, 2}, {3, 4}, {5, 6}, {7, 8}}  

 ‘Declare a jagged array  
Dim sales()() As Double = New Double(11)() {}

Zero - based and One - based Indexing:
If an array is declared as arr(4) it means we count these element from 0 to 4 ie. 5 elements, so this is known as Zero - based Indexing.
And if an array is declared as arr(1 to 5) it means we count these element from 1 to 5 ie. 5 elements, so this is known as One - based Indexing.