The conditional execution
The conditional execution instruction enables you to tell the program under which conditions to act one way and under which other conditions to act a different way.
The simpliest form of a conditional execution instruction looks like this:
IF logical expression THEN order END
The logical expression is evalueted on runtime. If the result is TRUE the commands following THEN will be executed. Otherwise the program flow continues after the END
Example:
PROCEDURE Main
VAR name : STRING
cgiwriteln('content-type: text/html')
cgiwriteln('')
name:=getquerystring('name')
IF name<>'' THEN
cgiwriteln('Good morning '+name)
END
ENDPROC
This little script checks whether there is a parameter 'name' sent in the referring URL.
If so (name<>'') it displays the greeting message.
Now we want to improve the script that way, that even if no name was passed a default greeting message is written: "Hello, unknown world".
Using the tools we already know this can be achieved like this: PROCEDURE Main
VAR name : STRING
cgiwriteln('content-type: text/html')
cgiwriteln('')
name:=getquerystring('name')
IF name<>'' THEN
cgiwriteln('Good Morning '+name)
END
IF NOT name<>''THEN
cgiwriteln('Hello, unknown world')
END
ENDPROC
This works well but does not look very elegant. In addition both logical expressions will be calculated in any case:
name<>'' and NOT name<>''
For this purpose it exists an extension to the conditional statement:
IF logical expression THEN
Statements_1
ELSE
Statements_2
END
Here the logical expression is calculated, too. If its result is TRUE the Statements_1 block is processed. For the case it returns FALSE the Statements_2 block will be executed.
This enables us to simplify our greeting script:
PROCEDURE Main
VAR name : STRING
cgiwriteln('content-type: text/html')
cgiwriteln('')
name:=getquerystring('name')
IF name<>'' THEN
cgiwriteln('Good Morning '+name)
ELSE
cgiwriteln('Hello, unknown world')
END
ENDPROC
Now our demands raise quickly. It would be possible to get the idea to greet the user "Hans" extraordinary cordially whereas others get the standard phrases from the example above.
We can solve this problem by cascading the IF statements.
To make it more clearly we will use text indention every time we cascade an IF clause within another one:
PROCEDURE Main
VAR name : STRING
cgiwriteln('content-type: text/html')
cgiwriteln('')
IF name='Hans' THEN
cgiwriteln('A very special Good Morning, Dear Hans')
ELSE
IF name<>'' THEN
cgiwriteln('Good Morning '+name)
ELSE
cgiwriteln('Hello, unknown world')
END
END
ENDPROC
Think of, you now would want to greet a lot more than only one user by their name. The cascade would grow very fast, very deep which would make it almost impossible to keep an overview of it.
But of course, EASY gives you a way to prevent this.
For the case, the ELSE branch of an conditional clause contains exactly one other conditional clause you can use an ELSIF branch (without any further nesting):
IF logical expression 1 THEN
Statements_1
ELSIF logical expression 2 THEN
Statements_2
ELSIF logical expression 3 THEN
Statements_3
...
ELSE
Statements
END
The program first checks the logical expression 1. If that leads to FALSE as result it continues by testing the logical expression 2. If this expression also is FALSE, it tries logical expression 3 next, and so on.
If there was no expression TRUE when the program arrives at the ELSE branch it will execute the statements there.
As soon as any of the logical expressions is TRUE the Statements in the corresponding branch will be executed.
Our program now looks like this:
PROCEDURE Main
VAR name : STRING
cgiwriteln('content-type: text/html')
cgiwriteln('')
name:=getquerystring('name')
IF name='Hans' THEN
cgiwriteln('A very special Good Morning, Dear Hans')
ELSIF name<>'' THEN
cgiwriteln('Good Morning '+name)
ELSE
cgiwriteln('Hello, unknown world')
END
ENDPROC
|