Professional Documents
Culture Documents
4
Procedural Logic
Objectives
At the end of this module, you will be able to:
Use Informix 4GL syntax for decision logic, including IF statements and
CASE statements
Follow Informix 4GL syntax for logic loops, including WHILE loops
and FOR loops
IF
CASE
WHILE
FOR
Informix has a set of decision and looping statements (listed in the slide above),
which can be used to determine the path a program follows in its execution.
Decision statements and loops are staples in most programming languages. If you
have prior programming experience, the purpose and functionality of the statements
will be familiar to you, although the exact syntax of these statements in Informix
4GL may be slightly different than what you have used.
In subsequent modules, decision statements and loops will be used as needed in code
examples demonstrating the use of other Informix 4GL statements. They are
described here so that you will be familiar with them when they are encountered in
following modules.
Test 2
Test 3
otherwise
It is a rare program that does the same thing every time it runs. Informix 4GL, like
other programming languages, provides methods for making executable statements
contingent upon various conditions.
With an IF... THEN... statement, you can make a single action conditional upon a
certain situation. For example, you can display a particular error message if the user
attempts to enter invalid information.
Adding an ELSE clause to an IF...THEN... statement allows you to specify an
alternative action to take if the test is not met.
While it is possible to use more than one IF statement to achieve a multiple logical
split, you often want to group several conditions and related actions together in one
statement. The CASE statement allows you to do this.
Example:
IF prompt_answer MATCHES "[Yy]" THEN
ERROR "Customer will be deleted."
ELSE
ERROR "Delete cancelled."
END IF
The IF... THEN... [ELSE...] statement is used to test an expression which can be
evaluated as TRUE or FALSE.
An IF statement is an instruction to execute a statement conditionally. It involves a
two-way logical choice. During program execution, when the test expression
evaluates to TRUE, Informix 4GL executes the statements following the keyword
THEN and terminating with the keyword END IF or ELSE.
With an ELSE clause, whenever the test expression does not evaluate to TRUE,
Informix 4GL executes the ELSE clause statements.
Example:
IF state = "CA" THEN
IF zipcode MATCHES "940*" THEN
MESSAGE "City is Redwood City."
END IF
END IF
Statement(s)
Count
Statement(s)
Syntax:
WHILE Boolean expression
statement(s)...
[EXIT WHILE]...
[CONTINUE WHILE]...
END WHILE
Example:
WHILE boss_in_office = TRUE
CALL act_busy() RETURNING boss_in_office
END WHILE
CALL sneak_out()
Syntax:
FOR integer variable = integer expression to integer expression
[STEP integer expression]
statement...
[CONTINUE FOR] ...
[EXIT FOR] ...
END FOR
Example:
MAIN
DEFINE i SMALLINT
FOR i = 1 TO 10
DISPLAY "i = ", i AT i,1
END FOR
END MAIN
Use the FOR loop to specify a number of times an iteration should occur. If you
use the keyword STEP with an integer expression, the loop is incremented in steps
of the value you state.
Since integer expression can be a variable, you can set the count dynamically.
Default STEPS
The default STEP is +1 for integer ranges that start with a lower number and end
with a higher number. The default STEP is -1 for integer ranges that start with a
higher number and end with a lower number.
Example:
FOR i = 1 TO 10 (step defaults to +1)
FOR i = 10 TO 1 (step defaults to -1)
A comment is text in 4GL source code to assist human readers but which 4GL
ignores. You can indicate comments in several ways:
Each comment line can begin with a pair of hyphens or minus signs. This
conforms to the ANSI standard for SQL.
--this is comment line one.
--this is comment line two.
Each comment line can begin with the pound (or sharp) symbol.
# this is comment line one.
# this is comment line two.
A comment can begin with the left-brace and end with the right-brace symbol.
These can be on the same line or on different lines.
{ this is comment line one.
this is comment line two. }
Conditional comment indicators enable the same source file to support
different features, depending on whether you compile with Dynamic 4GL or
with Informix 4GL.
--# OPTIONS HELP FILE "d4gl_help" line ignored by I-4GL
--@ OPTIONS HELP FILE "i4gl_help" line ignored by D4GL
Informix 4GL procedural logic statements are similar to those found in other
programming languages. They are used to guide the path of a program during its
execution.
The statements introduced in this module will be used in code examples that appear
in later modules.