Search Tools Links Login

VB6 Tutorial 19: Select Case


This lesson takes you through the Select Case block, a very useful decision making structure in Visual Basic.

The Select Case block provides an efficient way for decision making which is used to execute one block of statement(s) among a list of statement blocks according to the determined value of an expression. The expression is preceded by the "Select Case" phrase. The choices are made based on the value of this expression.

Syntax

Select Case expression
    Case value0
        statements
    Case value1
        statements
    Case value2
        statements
        ...........
        ...............
    Case else
        statements
End select

Example

Dim num As Integer
num = Val(Text1.Text)
Select Case num
Case 0
    Print "You have entered zero"
Case 1
    Print "You have entered one"
Case 2
    Print "You have entered two"
Case Else
    Print "The number you entered is greater than 2"
End Select

One block of statement(s) will be executed depending upon the value of num. If num=0 then, Case 0 will be evaluated and if num =1 then Case 1 will be evaluated.

See the other forms of Select Case Structure in the following programs.

Example

Dim score As Integer
score = Val(Text1.Text)
Select Case score
Case 900 To 1000
    Print "First position"
Case 800 To 899
    Print "Second position"
Case 700 To 799
    Print "Third position"
Case Else
    Print "No position, try again"
End Select

Example

Dim num As Integer
num = Val(Text1.Text)
Select Case num
Case Is > 0
    Print "Positive number"
Case Is < 0
    Print "Negative number"
Case 0
    Print "Zero"
End Select

Example

Select Case value
    Case 0, 1, 2, 3, 4, 5
        Print "The values are 0,1,2,3,4,5"
    Case Else
        Print "Other values"
End Select

Use the attached sample programs to see basic examples of Select ... Case in action.

One of the programs (Numbers) uses a function called Val(). We haven't learned about that one yet, but it is coming in a future lesson. In a nutshell, it converts the numbers in a string variable to a numeric value.

About this post

Posted: 2018-04-06
By: vb6boy
Viewed: 1,031 times

Categories

Visual Basic 6

VB6 Tutorial

Attachments

GradeProgram.zip
Posted: 4/7/2018 5:17:31 AM
Size: 1,275 bytes

NumberLength.zip
Posted: 4/7/2018 5:17:15 AM
Size: 1,201 bytes

WordLength.zip
Posted: 4/7/2018 5:17:00 AM
Size: 1,156 bytes


Loading Comments ...

Comments

CrazyDude posted this comment on 2018-04-07:

I'd also like to point out that using Select Case has a short-circuited quality when using Comma Separated (or) for testing inputs.  For example for that "Case 0, 1, 2, 3, 4, 5", if the input value is 2, it will stop there and not try to evaluate the 3, 4 and 5.  But in a line like "If value = 0 Or value = 1 Or value = 2 Or value = 3 Or value = 4 Or value = 5 Then", VB will try every one of them even if it sees that 3 is the matching value.  So Select Case can be very useful as a more efficient replacement for an "all Or" If/Then line of code.

You must be logged in to make a comment.