Introduction to Water Programming

This Site is intended to provide helps to Water language programmers around the world through technical examples, informational articles, and personal assistance.

The following examples are meant to be a quick Getting Started sort of tutorial using the Steam Free Version or the IDE. (If you are using the Free Version, be sure to set the output to "Rendered HTML".)

Example 9. If-Then-Elseif-Else

Conditional branching is a fundamental computing process that is implemented all the way down to the machine code level. Many high-level programming languages have introduced various models for implementing conditional branching in program logic. Water provides a construction similar to the Lisp cond construct, allowing multiple heterogenous conditions to be tested, and providing an optional default action.

Water:
<set x=10 />
<set y=20 />

<if>
   x.<equal 10 />  "x equal 10"
else
   "x not equal 10"
</if>

The above code will return "x equal 10".

Water:
<set x=10 />
<set y=20 />

<if>
   y.<equal 10 />  "y equal 10"
else
   "y not equal 10"
</if>

The above code will return "y not equal 10".

Water:
<set x=10 />
<set y=20 />

<if>
   y.<more x />  "y is greater than x"
else
   "y is not greater than x"
</if>

The above code will return "y is greater than x".

Water: multiple conditional expressions
<set x=10 />
<set y=20 />

<if>
   y.<less x />  "y is less than x"
   x.<more y />  "x is more than y"
else
   "none of the above conditions was satisfied"
</if>

The above code will return "none of the above conditions was satisfied". Notice that the multiple conditional expressions do not require an "elseif" keyword--they simply have a condition followed by an action/return value.

If the action part following a conditional expression has more that one line of instruction code, a Water <do>...</do> block needs to wrap the set of instructions.

Water: with compound conditions
<set x=10 />
<set y=20 />

<if>
   y.<less 30 />.<and x.<more 5 /> /> "x and y are both valid"
else
   "x and y are not both valid"
</if>

The above code will return "x and y are both valid".

Water: with negated condition (alternate syntax)
<set x=10 />
<set y=20 />

<if>
   <not <and y.<less 30 /> x.<more 5 /> /> />
   "x and y are both valid"
else
   "x and y are not both valid"
</if>

The above code will return "x and y are not both valid".

As every seasoned programmer will know, conditional expressions can mean the difference between correct and incorrect results in your program. It is best to experiment often with many different types of conditional expressions so that you can become comfortable using the conditional syntax of any new language you learn.

Go back to go to the next example.

©Copyright 2004, Mr. Merrick J. Stemen. All rights reserved.