Conditional statements and Loop structure |
|
Select Case Statement |
|
It is just as the alternative of If…Then…Else statement.It executes one statement in several groups of statements. |
|
A Select Case provides the same functionality as the If…then…Else statement but it makes the code more efficient and readable |
|
A Select Case works with a single test expression. |
Value of the statements will be dependent on test expression. |
|
Syntax: |
|
Select Case testexpression |
[Case expression1 |
[statements-n]] . . . |
[Case expression2 |
[statements-n]] |
. |
. |
Case Else |
Statement..n |
End Select |
<Testexpression> |
Any numeric or string expression. |
<statement> |
Statement executes if Testexpression matches any part of expressionlist. |
|
Example: |
|
The following example describes the use of the Select Case statement- |
|
Dim num as integer |
Dim a(10) as integer |
'Calculations containing num |
Note "'" Indicates the Comment Line |
Select Case num |
|
Case 1 a(1)=num |
Case 2 a(2)=num |
Case 3 a(3)=num |
Case else a(1)=num |
End Select |
|
Num is declared as integer variable here and a(10) is declared as an array of ten elements. |
Select Case checks values of num, according to that it executes the corresponding case statement. |
|
Note that: |
|
1.Value of the statement will be dependent on expression. |
|
2.The Case Else clause is used to indicate the elsestatements to be executed if no match is found between the testexpression and an expressionlist |
|
3.Select Case statements can be nested. |
|
4.Each nested Select Case statement must have a matching End Select statement. |
|
|
|
|
Conditional statements and Loop structure |
|
Do...Loop Structure |
|
Do…loop is used to execute a block of statements for indefinite number of times. There are several variations of Do...Loop statement. |
Each variation evaluates a numeric condition to determine whether to continue execution or not. |
|
Syntax: |
|
Do While <condition> |
statements… |
Loop |
|
Note that: |
|
1.When Visual Basic executes Do While loop, it first tests condition. |
|
2.If condition is False (zero), it skips past all the statements. |
|
3.If it's True (nonzero), Visual Basic executes the statements and then goes back to the Do While statement and tests the condition again. |
|
Example: |
|
Do While count<=5 |
count = count + 1 |
Loop |
|
Here loop increments the value of count until it becomes equal or less than 5. |
Another variation of Do...Loop statement executes the statement first and then tests condition after each execution. This variation guarantees at least one execution of statements. |
|
Syntax: |
|
Do |
<statement> |
Loop While <condition> |
|
|
|
|
Conditional statements and Loop structure |
|
For...Next Structure |
|
For...Next loop is used when you know how many times you want to execute loop.But in case of Do While it is not the case. |
Do While is used when you do not know how many times you want to execute that particular loop/Condition. |
|
Syntax: |
|
For counter = start To end [Step increment] |
statements |
Next [counter] |
The arguments counter, start, end, and increment are all numeric. |
|
In executing the For loop, Visual Basic: |
|
1. Sets counter equal to start. |
|
2. Tests to see if counter is greater than end. If so, Visual Basic exits the loop. |
|
3. Executes the statements. |
|
4. Increments counter by 1 or as it's specified. |
|
5. Repeats steps 2 through 4. |
|
Example: |
|
Dim A(40) as Integer |
For i = 0 To 40 step 1 |
A(i)=i |
Next |
Print A(9) |
|
Here index values are stored in array a(40).Then it prints the value of a(9). |
|
|
|
|
|
|
|
No comments:
Post a Comment