IF Statement

When your program needs to make a decision, you need to use the IF statement.  It is common in most programming languages.

An if statement is a special command you can use to test if a ‘condition’ is true, and depending on the condition being met, execute specified statements.

This example above shows a basic if statement:

Else

With an if statement it is also possible to execute alternative statements if the condition is not met, in this case you will need to make use of the else statement:

As you can see above, two sets of statements are specified, one to be executed if the condition is met, another to be executed if the condition is not met.

Conditions

When specifying conditions for your if statement, you have a large number of options you can use to test whether your condition has been met:

Is equal to ( = )

Is not equal to ( <> )

Less than ( < )

Greater than ( > )

Less than or equal to ( <= )

Greater than or equal to ( >= )

Multiple conditions

In all of the examples above we have only compared one value, but you can make your conditions much more useful by making use of the following operators:

and

You can use the and operator to specify that both conditions must be met:

 or

You can use the or operator to specify that either condition must be met: