Regardless of the simplicity or complexity of a method, 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.