The "if" Statement

You can use the if statement to conditionally execute part of your program, based on the truth value of a given expression. Here is the general form of an if statement:

if (test) then
  then-statement
else
  else-statement
end if;

If test evaluates to true, then then-statement is executed and else-statement is not. If test evaluates to false, then else-statement is executed and then-statement is not. The else clause is optional. Here is an actual example:

if (x == 10) then
  message("x is 10");
end if;

If x == 10 evaluates to true, then the statement message("x is 10"); is executed. If x == 10 evaluates to false, then the statement message("x is 10"); is not executed.

Here is an example using else:

if (x == 10) then
  message("x is 10");
else
  message("x is not 10");
end if;

You can use a series of if statements to test for multiple conditions:

if (x == 1) then
  message("x is 1");
else if (x == 2) then
  message("x is 2");
else if (x == 3) then
  message("x is 3");
else
  message("x is something else");
end if;

This function calculates and displays the date of Easter for the given year y:

Last updated

Was this helpful?