Seed7 - The extensible programming language
Seed7 FAQ Manual Screenshots Examples Libraries Algorithms Download Links
Examples echo args file in string simple clock count words subtype map function generic func declare stmt template operator 3-way if
Examples
3-way if statement
 previous   up   next 

This example introduces a new statement: The 3-way if-statement. In a 3-way if-statement two values are compared and depending on the outcome one of 3 code blocks, corresponding to less than, equal and greater than, is executed. An example using the 3-way if statement is:

if yourAge cmp myAge is
  lt: writeln("You are yonger than me");
  eq: writeln("We have the same age");
  gt: writeln("You are older than me");
end if;

The syntax of this statement can be defined with the Seed7 Structured Syntax Description (S7SSD):

$ syntax expr: .if.().cmp.().is.
                  lt. : .().
                  eq. : .().
                  gt. : .().
                end.if is -> 25;

The semantic of this statement can be defined with:

const proc: if (in integer: a) cmp (in integer: b) is
              lt: (in proc: ltPart)
              eq: (in proc: eqPart)
              gt: (in proc: gtPart)
            end if is func
  begin
    if a < b then
      ltPart;
    elsif a = b then
      eqPart;
    else
      gtPart;
    end if;
  end func;

The type proc determines that 'ltPart', 'eqPart' and 'gtPart' are call-by-name parameters. Contrary to normal parameters a call-by-name parameter is not evaluated, when the function is called. Instead the function decides, when a call-by-name parameter is evaluated. So the lt, eq and gt parts of a 3-way if-statement are not evaluated, when it is called. Instead the 3-way if-statement decides, when 'ltPart', 'eqPart' and 'gtPart' are evaluated.


 previous   up   next