Saturday 8 September 2012

Conditional Control Structures in VB 6.0

If Statement
It is very common to test a condition and take either of two possible actions based on the result of the test. If statement is used to test condition and act according to the result of the condition.


If condition Then
     Statements
Else
     Statements
End if

If condition is satisfied then statements given after Then will be executed otherwise statements given after Else are executed.

As you can understand from the syntax, Else is optional. If you do not give else then no statements will be executed when condition is not true.

Single Line vs. Multiple Line
You can give entire if statement in a single line. The following is an example of single line if statements.

If amount > 10000 then discount  = amount * 0.10

Note that when you use single line if statement End if is NOT required.

If .. then .. elseif… else statement
When you have to test for multiple conditions and take action based on the result of conditions then multiple if statement is best suited.

If condition then
    Statements
Elseif condition then
    Statements
Else
    Statements
End if

In the if..elseif ladder, if  condition is ever true the statements after that conditions will be executed and control comes out of entire if statement.

The following is an example where we determine the percentage of discount based on the value of amount variable.

If  amount > 20000 then
       Dis = 10
Elseif  amount > 15000 then
       Dis = 8
Elseif  amount >  10000 then
     Dis = 5
Else
     Dis = 0   ‘ no discount
End if


Select Case Statement
This statement is used when a single variable value is to be compared with different set of values.

Select Case expression
  Case value-1
      Statements
  [Case value-2
      Statements]…
  [Case else
      Statements]
End Select

Expression is evaluated in the beginning of the statement and the result is compared with the given values from top. If at any point the value is equivalent to the result of condition then the corresponding statements are executed and control comes out of Select Case statement.

Case Else is executed when none of the given values match the result of the expression.
The value may be either a single value or a range of values or a collection of values. It may also be a combination of all. The following example will explain the point.

Select Case code
 Case   1
         Dis  = 10
 Case   2 to 4
         Dis =  12
 Case   5, 7
         Dis  =  14
 Case   6, 7 to 10
         Dis = 11
 Case else
         Dis = 0
End select

In the above Select Case statement, first the value of variable code is compared with 1 (first case). If they are same then the corresponding action takes place (setting dis variable to value 10). Otherwise, code is compared with second case, in which case, condition will be true if code is in the range 2 to 4. In third case code is compared with 5 and 7. In fourth case, code is compared with value 6 and also with the range from 7 to 10.

In none of the given conditions are true, then statements given after Case Else are executed, where dis is set to 0. The following are the possible values that you can give after each case:

¨       A single constant. Ex: 10
¨       A single range. Ex: 1 to 5
¨       A collection of values each separated by comma. Ex: 1,4,6
¨       Any combination of the above three. Ex: 5,7, 10 to 15

No comments:

Post a Comment