You are on page 1of 14

Module

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

Procedural Logic 1772 07-99 4-1


 2000 Informix Software, Inc.
Decision and Looping Statements

„ IF
„ CASE
„ WHILE
„ FOR

Procedural Logic 1772 07-99 2

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.

Procedural Logic 4-2


Decision Logic: Branching

Execution Conditional: IF...THEN...


Test determines whether to
execute an action.

Binary logical fork: IF...THEN...ELSE


Test determines which of two
actions to take.

Multiple logical fork: CASE


Test 1

Test 2

Test 3

otherwise

Procedural Logic 1772 07-99 3

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.

Procedural Logic 4-3


Decisions: The IF Statement

Use the IF statement for making actions dependent on a test.

Example:
IF prompt_answer MATCHES "[Yy]" THEN
ERROR "Customer will be deleted."
ELSE
ERROR "Delete cancelled."
END IF

Procedural Logic 1772 07-99 4

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.

Procedural Logic 4-4


IF Within Other Statements

Keep track of IF ... THEN


hierarchy! Use proper IF ... THEN
indentation. . . .
END IF
END IF

Example:
IF state = "CA" THEN
IF zipcode MATCHES "940*" THEN
MESSAGE "City is Redwood City."
END IF
END IF

Procedural Logic 1772 07-99 5

IF statements can be nested within other IF statements. A frequent mistake is to


forget the END IF keywords or, in the case of nested IF statements, to fail to match
each IF with an END IF. The error message from the compiler in such cases is
usually just the generic syntax error.
Note Here is a good example of a case where tidy indenting is helpful. One of the first
things to check when you get a non-specific syntax error is that each IF is paired with
an appropriately placed END IF. The END IF clause is required for each IF
statement.

Procedural Logic 4-5


Decisions: The CASE Statement
Syntax:
CASE
WHEN {expr|Boolean-expr}
statement ...
[EXIT CASE]
...
Example:
[OTHERWISE ]
statement ... CASE
[ EXIT CASE ] WHEN answervar = "Y"
END CASE MESSAGE "Answered Yes.
WHEN answervar = "N"
MESSAGE "Answered No."
OTHERWISE
MESSAGE "Bad Answer."
END CASE

Procedural Logic 1772 07-99 6

The CASE statement can be reduced to multiple IF statements. However, it is often


more coherent to express a series of related choices in one statement.
One method of the CASE statement repeats the variable or expression once for each
WHEN clause, as illustrated in the slide.
The OTHERWISE clause is triggered when all the preceding WHEN clause
conditions test FALSE. Statements made in the OTHERWISE clause execute if all
the listed conditions fail. For clarity, always use an OTHERWISE clause in your
CASE statement.
There is an implied EXIT CASE statement at the end of each sequence of statements
following a WHEN clause. Thus, when the conditions associated with a WHEN
statement are met, the related statements execute and the CASE statement
terminates. Program control passes to the sequence of statements following the END
CASE statement. You do not need an explicit EXIT CASE.
Since the CASE statement evaluates conditions in the order in which they are listed,
place the most likely conditions early in the list for improved performance.
A second method uses the variable or expression only once. The WHEN statement
then checks the possible values for the given expression. An example of this method
is on the following page.

Procedural Logic 4-6


Alternate Syntax:
CASE answervar
WHEN "Y"
MESSAGE "Answered Yes."
WHEN "N"
MESSAGE "Answered No."
OTHERWISE
MESSAGE "BAD Answer."
END CASE

Procedural Logic 4-7


Logical Loops

Generalized looping: WHILE


Test Boolean
Expression

Statement(s)

Counting loops: FOR

Count

Statement(s)

Procedural Logic 1772 07-99 8

Loops provide a way to reiterate executable statements.


A WHILE loop continues as long as the stated Boolean test expression holds TRUE
or until an EXIT WHILE statement is encountered. If possible, it is best to have only
one way out of a loop.
A FOR loop executes a specified number of times. The specification can be in the
form of a variable whose value is set dynamically. A STEP option allows you to
specify count increments.
4GL also has a special FOREACH loop, which causes a statement or statement
sequence to be executed for each row returned from a query. FOREACH will be
discussed in detail in later modules.

Procedural Logic 4-8


WHILE Loop

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()

Procedural Logic 1772 07-99 9

Use a WHILE loop to execute a statement or group of statements for as many


times as the stated Boolean condition tests TRUE.
The optional statement EXIT WHILE moves program control to the first statement
following END WHILE.
The optional statement CONTINUE WHILE interrupts the sequence and causes
program control to return to the top of the sequence and to test the Boolean
expression.
If the Boolean expression is FALSE, then the program control skips to the first
statement after the END WHILE.

Procedural Logic 4-9


FOR Loop

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

Procedural Logic 1772 07-99 10

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)

Procedural Logic 4-10


The LET Statement
„ LET assigns values to program variables.

LET var1 = "Memphis"

LET var2 = 1000

LET var3 == "TESTER" (version 7.3)

Procedural Logic 1772 07-99 11

The LET statement is used to assign values to program variables. To execute a


LET statement, 4GL evaluates the expression on the right of the equal sign and
assigns the resulting value to the variable on the left.
When a variable is declared in a program, the value assigned depends on the version
of 4GL that you are using. For RDS, the value is initialized to NULL. When using
the C Compiler version, however, the content of the variable is whatever happens to
occupy that memory location. For this reason, it is important to assign a value to a
program variable before referencing it in programs.
You can use most of the 4GL built-in functions and character operators within the
LET statement.
Beginning with version 7.3, == can be used as a synonym for =.

Procedural Logic 4-11


Comment Indicators
„ A pair of hyphens or minus signs (ANSI standard)
--this is a comment
„ The pound (or sharp) symbol
# this is a comment also
„ Left and right braces
{this comment
is multi-line}
„ Conditional comments (I-4GL version 7.3)
--# OPTIONS HELP FILE "d4gl_help"
--@ OPTIONS HELP FILE "i4gl_help"

Procedural Logic 1772 07-99 12

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

Procedural Logic 4-12


Summary

„ Informix 4GL procedural logic statements are similar to


those found in other programming languages.
„ IF, CASE, WHILE, FOR, and LET statements will appear
in other modules that focus on other Informix 4GL
statements.

Procedural Logic 1772 07-99 13

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.

Procedural Logic 4-13

You might also like