Wafl Home

Program Structure Tutorial

Conditional Expressions

Programming language Wafl has two kind of conditional expressions: if and switch.

Expression if consists of a condition and two conditional expressions:

if <condition> then <exp1> else <exp2> 

The condition has to be of Bool type, while conditional expression may be of any type, but must have the same type. The condition is evaluated first. If its value is true, expression exp1 in then branch is evaluated and the evaluated value is the result of the complete if expression. If the value of the condition is false, expression exp2 in else branch is evaluated and the evaluated value is the result of the complete if expression.

Expression switch consists of a conditional expression, at least one case clause and a default clause:

switch <cond-exp> {
    <case-clause>
    ...
    <default-clause>
}

where case clauses are defined as optionally multiple case literals, followed by an expression:

case <literal> ...
    <exp>;

and multiple literals in a same case clause may be separated either by keyword case or by comma.

Default clause is defined as:

default : <exp>

Expression cond-exp and all literals in case clauses have to be of a same primitive type. All other expressions have to be of any, but same type. Expression cond-exp is evaluated first. Its value is then tested on equality to literals specified in case clauses. If there is a literal equal to cond-exp value, the corresponding expression is evaluated and its value is the result of the complete switch expression. If a same literal is specified many times, only the first appearance is considered. If no specified literal is equal to cond-exp value, the expression from the default clause is evaluated and its value is the result of the complete switch expression.

The following example uses both kinds of conditional expressions to calculate how many days there is in a given month.

Source code:

{#
daysInMonth( 1, 2000 ),
daysInMonth( 2, 2000 ),
daysInMonth( 2, 2001 ),
daysInMonth( 2, 2004 ),
daysInMonth( 2, 2100 )
#}
where{
    daysInMonth( month, year ) =
        switch month {
            case 2
                if isLeapYear(year)
                    then 29
                    else 28;

            case 4 case 6, 9, 11
                30;
                
            default
                31
            };
    isLeapYear( year ) =
        year % 4 = 0
        and ( year % 100 != 0
              or year % 400 = 0 );
}

Result:

{# 31, 29, 28, 29, 28 #}

 

Table of Contents

Let's Start

Program Structure

Primitive Data Types

List

Tuple

Record

HTML

Command Line Interpreter

Using Web Servers

Syntax

Examples

Tips

The most of examples evaluates with both command line and Web server Wafl interpreters. If any example is based on specific features of an interpreter, it is explicitly annotated.