next up previous contents
Next: Defaults Up: Conditionals Previous: Conditionals   Contents

Simple conditionals

The basic conditional structure is

        if(<condition>)
          <stmt1>
        else
          <stmt2>

A ``statement'' can be either an assignment, or a group of statements delimited by curly brackets.

The effect of the statement

        if(c)
          x := foo;
        else
          x := bar;
is exactly equivalent to

        x := c ? foo : bar;

If x is assigned in the ``if'' part, but not assigned in the ``else'' part, then x is undefined when the condition is false. This means that x can take any value in its type. Similarly, if x is assigned in the ``else'' part, but not in the ``if'' part, then x is undefined when the condition is true. For example,

        if(c)
          x := foo;
        else
          y := bar;
is equivalent to

        x := c ? foo : undefined;
        y := c ? undefined : bar;

Mathematically, undefined is the set of all possible values.

If next(x) is assigned in one part of the conditional, but not the other, then

        next(x) = x;
is the default. For example,

        if(c)
          next(x) := foo;
        else
          next(y) := bar;
is equivalent to

        next(x) := c ? foo : x;
        next(y) := c ? y : bar;

Conditionals are statements, and therefore can be nested inside conditionals. Groups of statements can also be nested inside conditionals. For example:

        if(c)
        {
          x := foo;
          if(d)
            next(y) := bar;
          else
            next(z) := bar;
        }
        else
          x := bar;

The ``else'' part may be omitted, although this is hazardous. It can result in an ambiguity as to which ``if'' a given ``else'' corresponds to, if there are nested conditionals. Care should be take to use curly braces to disambiguate. Thus, instead of:

  if(c)
    if(d)
      <stmt>
    else
      <stmt>
the preferred usage is:

  if(c){
    if(d)
      <stmt>
    else
      <stmt>
  }
The effect of:

        if(c)
          <stmt>
is equivalent to:

        if(c)
          <stmt>
        else {}


next up previous contents
Next: Defaults Up: Conditionals Previous: Conditionals   Contents
2002-10-28