2.2.2. Program Control. Great Migrations LLC PROMULA Application Development System
Reklam
Reklam
Promula Application Development System User's Manual
A COLUMN relation between a set and a variable tells PROMULA to use the variable's values to label columns classified by the set in displays of array variables.
After defining, initializing, and relating labels to the program sets, a display of any variable dimensioned by the sets is much more meaningful. For example, the display of variable expns is shown in the dialog below as an example:
WRITE expns
JAN
FEB
MAR
APR
MAY
JUN
JUL
AUG
SEP
OCT
NOV
DEC
RENT
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
Monthly Expenses by Category ($)
FOOD CAR SERVICE UTILITIES
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
CAR INS
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
PHONE
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
For more information about relations between sets and variables, refer to Chapter 3 of this manual, especially the sections covering the PROMULA noun Relation and the READ set, DEFINE RELATION, and SELECT RELATION statements.
2.2.2. Program Control
KEY TOPICS:
1.
Procedures
2.
Linear Flow
3.
Conditional Branches
4.
Looping
One of the most important tasks facing an application programmer is setting up structures to control the sequence of events that take place during program execution. The efficiency of a program and the accuracy of its results are highly dependent on the correct implementation of these structures. Fortunately, only three types of control structures are needed to handle the control requirements of any application program. These are Linear Flow structures, Conditional Branch structures, and Looping structures. In addition, a fourth type of control structure, the Procedure is often used to help modularize the activities of a program into subunits that work together.
2.2.2.1. Procedures
A large computer program is like a complex machine; it can have many parts. The most fundamental of these parts are the program's statements. Each statement performs a specific predefined task. In addition to the statements that are available in the PROMULA language, it is possible to create your own. In PROMULA, a programmer-defined statement is called a
Procedure. A procedure is a set of statements that are executed as a group when the procedure's name is used as a program statement. This is referred to as "calling" or "invoking" the procedure.
Defining procedures gives programmers the ability to break the programming process into simpler steps. Each procedure creates a functional program unit that can later be integrated with the other procedures to create the full program.
28
Promula Application Development System User's Manual
When PROMULA encounters a procedure name in a program, it executes the statements of the procedure. When execution of a procedure finishes, processing continues with the statement following the procedure call. Procedures can call other procedures including themselves.
For more information on procedures, see the DEFINE PROCEDURE statement in Chapter 3 of this manual.
2.2.2.2. Linear Flow
PROMULA applications use linear control as the primary means of directing their course of action. This means that executable operations are performed in the order in which they are defined. Thus, execution of a program begins with the first statement of the first procedure of the program and proceeds to follow the program instructions one-by-one toward the last statement of the program. After execution of the last statement, the program ends. A schematic of linear program flow is shown in the diagram below:
Start (Step 1)
Step 2
Step 3
Other Steps...
End (Last Step)
Figure 2-10: Linear Program Flow
Any one of these steps may be a simple statement, a procedure call, or another control structure.
2.2.2.3. Conditional Branches
A linear flow of action through a program in which every statement from the first to the last is executed in sequence is often inflexible and inefficient. Even worse, it may lead to errors if the data put into the program does not fit the linear logic defined by the code. In order to efficiently manage complex problems and data, your programs will require flexibility. The basic element of all complex control structures is the Conditional Branch. Conditional branches are program statements that can redirect linear flow and create more flexible and responsive execution paths. A schematic of a conditional branch is shown in the figure below:
FALSE (0)
Process
B
Condition
TRUE (1)
Process
A
29
Promula Application Development System User's Manual
Figure 2-11: A Simple Conditional Branch
Conditional branch statements are often referred to as "IF-THEN-ELSE" statements. For example, the diagram above illustrates a simple conditional branch that can be read as "If the Condition is true, then do Process A; else (the Condition is false), do Process B". Process A and/or Process B may also contain Conditional branches.
An IF-THEN-ELSE statement that might be used in the budget program would check the value of the taxes variable to make sure it "makes sense." Recall that variable taxes is used by the budget program to make a simple adjustment on the worker's earnings to reflect the fraction of pay lost to taxes. The program expects this value to be between 0.5 and one. In
PROMULA, the DO IF statement is used to create logical branches.
An example of the DO IF statement that could be used to check the value of taxes and write an error message if the value does not fit the expectations of the program is shown below:
DO IF taxes GT 1
WRITE ("The value of taxes should be less than one.")
ELSE taxes LE 0
WRITE ("The value of taxes should be greater than or equal to zero.")
ELSE taxes GT 0.50
WRITE ("The value of taxes should be less than 0.5)
END IF
For more information on Branching refer to the sections covering the DO IF statement and the nouns Boolean Expression and Relational Expression in Chapter 3 of this manual.
2.2.2.4. Looping
Looping refers to the process of repeatedly performing a set of operations. The basic components of a loop are the body of the loop, and the DO condition for the loop. The body of the loop is simply the set of instructions that are executed on each pass through the loop. The DO condition for the loop is a true-false expression that is evaluated before each iteration of the loop to determine if the instructions in the body of the loop should be performed. PROMULA supports several types of looping control structures; these include the DO WHILE, DO UNTIL, DO file, and DO set statements, and recursion.
DO WHILE loops execute the instructions in the body of the loop while the DO condition is true.
DO UNTIL loops execute the instructions in the body of the loop until the DO condition is false.
DO file loops are used with text or random files. They execute the instructions in the body of the loop once for each record of the file and exit when the end of file is reached.
The DO set loop is unique to PROMULA; it is a set-controlled looping structure that executes the instructions in the body of the loop once for each element of the set's selection vector. A set's selection vector contains the currently active elements of the set. Thus, the order and range of a DO set loop can be controlled by sorting and/or selecting the elements of the set.
DO set loops are an extremely powerful tool for manipulating PROMULA's set-based array variables.
Recursion is a looping control structure that does not use one of PROMULA's DO loop statements explicitly. It is used to execute a procedure repeatedly until some "exit" condition occurs. Recall that a procedure is a group of statements that are
30
İndir
Reklam