Regardless of the simplicity or complexity of a method or function, you will always use one or more of three types of programming structures. Programming structures control the flow of execution, whether and in what order statements are executed within a method. There are three types of structures:
- Sequential: a sequential structure is a simple, linear structure. A sequence is a series of statements that 4D executes one after the other, from first to last. A one-line routine, frequently used for object methods, is the simplest case of a sequential structure. For example:
- Branching: A branching structure allows methods to test a condition and take alternative paths, depending on the result. The condition is a Boolean expression, an expression that evaluates TRUE or FALSE. One branching structure is the
If...Else...End ifstructure, which directs program flow along one of two paths. The other branching structure is the
Case of...Else...End casestructure, which directs program flow to one of many paths.
- Looping: When writing methods, it is very common to find that you need a sequence of statements to repeat a number of times. To deal with this need, the 4D language provides the following looping structures:
For each...End for each
The loops are controlled in two ways: either they loop until a condition is met, or they loop a specified number of times. Each looping structure can be used in either way, but
Repeatloops are more appropriate for repeating until a condition is met, and
Forloops are more appropriate for looping a specified number of times.
For each...End for eachallows mixing both ways and is designed to loop within objects and collections.
Note: 4D allows you to embed programming structures up to a "depth" of 512 levels.
return statement can be called from anywhere. When a
return statement is used in a function or method, the execution of the function or method is stopped. The remaining code is not executed and the control is returned to the caller.
return statement can be used to return a value to the caller.
var $message : Text var $i : Integer While (True) //infinite loop $i:=$i+1 $message+=String($i)+"A\r" // until 5 logConsole($message) If ($i=5) return //stops the loop End if $message+=String($i)+"B\r" // until 4 logConsole($message) End while $message+=String($i)+"C\r" //never executed logConsole($message) // 1A // 1B // 2A // 2B // 3A // 3B // 4A // 4B // 5A