The Hyper Programming Language |
home | language reference | compiler | hyper on launchpad |
Most of you will already know what an 'if' statement is. The 'if' statement in Hyper looks like its equivalent in Pascal and BASIC, and languages derived from them. There is nothing new to this statement; if you have seen it in other languages then you should know what's it all about.
The 'if' statement can have many forms. The simplest form is:
if condition then statements end if
As you see, the condition does not have to be put between brackets like in C++ and Java. Also note that you are allowed to write just end instead of the full end if if you like.
You can extend the 'if' with an 'else' clause:
if condition then statements else statements end if
Or you can use 'else if' branches:
if condition then statements else if condition then statements (...) end if
And of course you can combine them; just make sure the 'else' clause is below the 'else if' branches:
if condition then statements else if condition then statements else if condition then statements else if condition then statements else statements end if
A simple example showing a function that returns the largest of two numbers:
(the example is only for educational purposes; normally you would want to write this using a conditional expression instead of an 'if' statement)
procedure max(a & b : nat) : nat if a > b then return a else return b end end
Another example that compares two numbers, and returns a negative number if the first argument is the largest, that returns a positive number if the second argument is the largest, and returns zero if the numbers are equal:
procedure compare(a & b : nat) : int if a > b then return -1 else if a < b then return 1 else return 0 end end