Control flow

One of the characteristics of Water is that anything traditionally regarded as a statement (e.g. if statements, loops, return statements, etc.) can be used as an expression. This can be very useful for writing short, fluid control flow.

If statement

The following example uses an if statement to select a different string based on whether the number you enter is even or odd.

Note
Water currently does not have support for switch statements, but you can get similar runtime semantics with if statements.


Loops

Water also supports for, while, and do-while loops, which work exactly how you would expect them to in other similar programming languages.

For loop

This example shows a for loop which counts the number of vowels in the sentence you enter.

Note
Did you know that "onomatopoeia" has the most vowels out of any word of its length?


While loop

This example runs a while loop until a random number smaller than 0.1 is generated. Try running it multiple times to see the number of attempts change.

The maximum I got after a couple of tries was 61. Let's see how rare that was with the next example!


Do-while loop

This example uses a do-while loop to repeatedly ask for input until you enter a valid number. A valid number consists only of digits, and the function that implements this below will check that. If you enter something which isn't a number, then the program will prompt you again for a number.