if

Syntax if expression

statements1

else

statements2

Action Evaluates the if expression. If the result is true, executes statements1.

The else clause of the if statement is optional. If it is provided, and expression evaluates to false, statements2 are executed.

Examples if not file.exists (path)
{file.new (path)}

file.setType (path, 'TEXT')}
In this simple form of the if statement, we only want to execute the statements that create a new text file if the file doesn't already exist. If the call to file.exists returns true, the expression not file.exists (path) will evaluate to false, and the
indented statements will not be executed.

if file.isFolder (path)
{size = file.bytesInFolder (path)}

else
{size = file.size (path)}

This example demonstrates the use of the else clause, which enables us to take one of two possible courses of action depending on the result of a test - in this case whether we are dealing with a file or a folder.

See Also case

not

Discuss