Deutsch English
Alphabetical index
Array functions
CGI functions
Data types
Date functions
Debugging
Fulltext functions
Language elements
  • 'ANWEISUNGEN'
  • 'AUSDRÜCKE'
  • 'BEZEICHNER'
  • DEF
  • IF
  • 'INDEXBESCHREIBUNG'
  • 'KONSTANTEN'
  • MODULE
  • PROCEDURE
  • REPEAT
  • RETURN
  • USES
  • VAR
  • WHILE
    Numerical functions
    Operators
    Other functions
    Ramtext functions
    Selecting
    Socket functions
    Statistical functions
    Stream functions
    String functions
    System functions
    Table functions
    Text functions
    Home    Overview    Search    Impressum    Contact    Members
    Topic complex Language elements
    Function IF
    Short Conditional execution
    Syntax IF <log. Ausdruck> THEN <Anweisungen> {ELSIF <log. Ausdruck> THEN <Anweisungen>} [ELSE <Anweisungen>] END
    Parameter log. expression::= log. term {OR log. term}.
    log. term::=log. faktor {AND log. faktor}.
    log. faktor::=[NOT] comparison | (log. expression).
    Return
    See also:
    Description 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

    User comments:
    Write a comment:
    Name
    EMail
    Subject
    Text (Switch mode)

    Choose between
    the normal and the
    advanced editor.
    The advanced editor
    runs in IE 5+ and
    Mozilla 1.4+ only
    SPAM-Protection Please enter tdbengine in this field.

    tdbengine chat
    irc.tdbengine.org
    #tdbengine

       Copyright © 2003-2004 tdb Software Service GmbH
       Alle rechte vorbehalten. / All rights reserved
       Last changed: 21.10.2004
    {Fehler für :execmacro{execmacro="sessionspy"}