Multiway branching

Sometimes it is necessary to examine the condition so that, depending on the result value, program offers multiple alternative actions. In this case, the Select-Case structure for multiway branching is used. It works by sending a parameter to it and, depending on its value, different parts of the program are executed. If the parameter value differs from all the alternatives offered the program code behind Else command is executed.

Syntax of Select-Case Structure is:

Select [ Case ] <test expression>
{Case <result value>
[PROGRAMME CODE]}
[Case Else
[ALTERNATIVE CODE]]
End Select   

Imagine that we should analyse the test results. We should write a program in which we first enter the test score and further, by using the Select-Case structure, we compare the entered score with the number of points and execute a code that returns appropriate message:

Dim score As Integer, message As String
score = InputBox (“Enter the score”)
Select Case score
    Case Is> = 90
        message = “Excellent”
    Case Is> = 80
        message = “Very good”
    Case Is> = 70
        message = “Good”
    Case Is> = 60
        message = “Sufficient”
    Case Else
        message = “Insufficient”
End Select
MsgBox message

As you can see, the Select-Case structure is much more convenient than an nested If-Then structure in case we have multiple of possible outcomes.