Wednesday, February 25, 2009

Select Case Statement

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
Any numeric or string expression.
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.

No comments: