4D v13.4

Case of...Else...End case

Home

 
4D v13.4
Case of...Else...End case

Case of...Else...End case  


 

 

The formal syntax of the Case of...Else...End case control flow structure is:

 Case of
    :(Boolean_Expression)
       statement(s)
    :(Boolean_Expression)
       statement(s)
       .
       .
       .
 
    :(Boolean_Expression)
       statement(s)
    Else
       statement(s)
 End case

Note that the Else part is optional; you can write:

 Case of
    :(Boolean_Expression)
       statement(s)
    :(Boolean_Expression)
       statement(s)
       .
       .
       .
 
    :(Boolean_Expression)
       statement(s)
 End case

As with the If...Else...End if structure, the Case of...Else...End case structure also lets your method choose between alternative actions. Unlike the If...Else...End if structure, the Case of...Else...End case structure can test a reasonable unlimited number of Boolean expressions and take action depending on which one is TRUE.

Each Boolean expression is prefaced by a colon (:). This combination of the colon and the Boolean expression is called a case. For example, the following line is a case:

:(bValidate=1)

Only the statements following the first TRUE case (and up to the next case) will be executed. If none of the cases are TRUE, none of the statements will be executed (if no Else part is included).

You can include an Else statement after the last case. If all of the cases are FALSE, the statements following the Else will be executed.

Example  

This example tests a numeric variable and displays an alert box with a word in it:

 Case of
    :(vResult=1) ` Test if the number is 1
       ALERT("One.") ` If it is 1, display an alert
    :(vResult=2) ` Test if the number is 2
       ALERT("Two.") ` If it is 2, display an alert
    :(vResult=3) ` Test if the number is 3
       ALERT("Three.") ` If it is 3, display an alert
    Else ` If it is not 1, 2, or 3, display an alert
       ALERT("It was not one, two, or three.")
 End case

For comparison, here is the If...Else...End if version of the same method:

 If(vResult=1) ` Test if the number is 1
    ALERT("One.") ` If it is 1, display an alert
 Else
    If(vResult=2) ` Test if the number is 2
       ALERT("Two.") ` If it is 2, display an alert
    Else
       If(vResult=3) ` Test if the number is 3
          ALERT("Three.") ` If it is 3, display an alert
       Else ` If it is not 1, 2, or 3, display an alert
          ALERT("It was not one, two, or three.")
       End if
    End if
 End if

Remember that with a Case of...Else...End case structure, only the first TRUE case is executed. Even if two or more cases are TRUE, only the statements following the first TRUE case will be executed.

Consequently, when you want to implement hierarchical tests, you should make sure the condition statements that are lower in the hierarchical scheme appear first in the test sequence. For example, the test for the presence of condition1 covers the test for the presence of condition1&condition2 and should therefore be located last in the test sequence. For example, the following code will never see its last condition detected:

 Case of
    :(vResult=1)
       ... `statement(s)
    :((vResult=1)&(vCondition#2)) `this case will never be detected
       ... `statement(s)
 End case
 .


In the code above, the presence of the second condition is not detected since the test "vResult=1" branches off the code before any further testing. For the code to operate properly, you can write it as follows:

 Case of
    :((vResult=1)&(vCondition#2)) `this case will be detected first
       ... `statement(s)
    :(vResult=1)
       ... `statement(s)
 End case
 .

Also, if you want to implement hierarchical testing, you may consider using hierarchical code.

Tip: Branching can be performed without statements to be executed in one case or another. When developing an algorithm or a specialized application, nothing prevents you from writing:

 Case of
    :(Boolean_Expression)
    :(Boolean_Expression)
 
       .
       .
       .
 
    :(Boolean_Expression)
       statement(s)
    Else
       statement(s)
 End case

or:

 Case of
    :(Boolean_Expression)
    :(Boolean_Expression)
       statement(s)
       .
       .
       .
 
    :(Boolean_Expression)
       statement(s)
    Else
 End case

or:

 Case of
    Else
       statement(s)
 End case

 
PROPERTIES 

Product: 4D
Theme: Language definition

 
SEE ALSO 

Control Flow
For...End for
If...Else...End if
Repeat...Until
While...End while