4D v16.3Case of...Else...End case |
||
|
4D v16.3
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 Note that the Else part is optional; you can write: Case of 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. This example tests a numeric variable and displays an alert box with a word in it: Case of For comparison, here is the If...Else...End if version of the same method: If(vResult=1) ` Test if the number is 1 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
Case of 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 or: Case of or: Case of
See also
Control Flow
|
PROPERTIES
Product: 4D
HISTORY
ARTICLE USAGE
4D Language Reference ( 4D v16) |