4D v16.3

If...Else...End if

Home

 
4D v16.3
If...Else...End if

If...Else...End if  


 

 

The formal syntax of the If...Else...End if control flow structure is:

 If(Boolean_Expression)
    statement(s)
 Else
    statement(s)
 End if

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

 If(Boolean_Expression)
    statement(s)
 End if

The If...Else...End if structure lets your method choose between two actions, depending on whether a test (a Boolean expression) is TRUE or FALSE.

When the Boolean expression is TRUE, the statements immediately following the test are executed. If the Boolean expression is FALSE, the statements following the Else statement are executed. The Else statement is optional; if you omit Else, execution continues with the first statement (if any) following the End if.

Note that the Boolean expression is always fully evaluated. Consider in particular the following test:

 If(MethodA & MethodB)
    ...
 End if

The expression is TRUE only if both methods are TRUE. However, even if MethodA returns FALSE, 4D will still evaluate MethodB, which is a useless waste of time. In this case, it is more interesting to use a structure like:

 If(MethodA)
    If(MethodB)
       ...
    End if
 End if

The result is similar and MethodB is evaluated only if necessary.

Example  

  ` Ask the user to enter a name
 $Find:=Request(Type a name)
 If(OK=1)
    QUERY([People];[People]LastName=$Find)
 Else
    ALERT("You did not enter a name.")
 End if

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

 If(Boolean_Expression)
 Else
    statement(s)
 End if

or:

 If(Boolean_Expression)
    statement(s)
 Else
 End if



See also 

Case of...Else...End case
Control Flow
For...End for
Repeat...Until
While...End while

 
PROPERTIES 

Product: 4D
Theme: Language definition

 
HISTORY 

 
ARTICLE USAGE

4D Language Reference ( 4D v16)
4D Language Reference ( 4D v16.1)
4D Language Reference ( 4D v16.2)
4D Language Reference ( 4D v16.3)