ControlFlow

Control Flow Statements

Like all procedural programming languages, Euphoria has a set of statements that control the execution flow of the program.



IF

This would probably be the most common form of flow control statement. It works by testing if some expression is true or false, then depending on the result runs one set of statements or another set of statements.

Syntax

The general format is ...

if BOOLEXPR then  
    TRUESTMT 
else  
    FALSESTMT  
end if 

Where BOOLEXPR is any expression that evaluates to a single number; zero is deemed to be false and non-zero is deemed to be true. If the expression is non-zero (true) then all the statements in TRUESTMT are run then control goes to the first statement after the end if clause. However, if the expression evaluates to zero (false) then all the statements in FALSESTMT are run then control goes to the first statement after the end if clause.

Notes:

  • you can nest if constructs to any depth.
  • The else clause can be omitted if there are no statements to execute when the expression is false.
  • The TRUESTMT can be omitted if there are no statements to execute if the expression is true.
if AA then  
    if BB then 
       STMT1 
    end if 
    STMT2 
else  
    FALSESTMT  
    if CC then 
    else 
        STMT3 
    end if 
end if 

There are a couple of variations to the general syntax. The first one is the elsif clause.

if BOOLEXPR1 then  
    TRUESTMT1  
elsif BOOLEXPR2 then  
    TRUESTMT2 
 
elsif BOOLEXPR3 then  
    TRUESTMT3 
. . . 
else 
    FALSESTMT  
end if 

This is almost the same as before however, if BOOLEXPR1 is false then BOOLEXPR2 is tested, and so on in order. As soon as one expression is true, the statements in the corresponding if or elsif clause are run, then control proceeds to after the end if. If all expressions are false, then the statements in the else clause are run.

The second variation is the label clause. This clause allows the program to name the if statement, which then enables the break statement to prematurely exit an enclosing if construct.

if AA label "IFTOP" then  
    if BB then 
       STMT1 
       break "IFTOP" -- Go to end of the IFTOP construct. 
    end if 
    STMT2 
else  
    FALSESTMT  
    if CC then 
    else 
        STMT3 
    end if 
end if 

SWITCH

This is like a if construct in that it is used to run a specific set of statements depending on the value of an expression, but it is a more specialized construct.

Rather than:

if matrix[this_file][that_file] = GLOBAL then 
... 
elsif matrix[this_file][that_file] = PUBLIC then 
... 
elsif matrix[this_file][that_file] = LOCAL then 
... 
else 
 
end if 

you can write the expression,matrix[this_file][that_file], to be compared just once:

switch matrix[this_file][that_file] do 
    case GLOBAL then 
        ... 
    case PUBLIC then 
        ... 
    case LOCAL then 
        ... 
    case else 
        ... 
end switch 

The case values must be constants. The switch statements jump to the case that matches. And by default they will jump to the first statement after the corresponding end switch statement when they encounter the next case. However, this can be modified for any number of cases by adding 'fallthru'.

GOTO

FOR

WHILE

LOOP

Routine Calls

Boolean Expressions

Search



Quick Links

User menu

Not signed in.

Misc Menu