# if

> 1. Conditional statement with one alternative

> 2. Conditional statement with two alternatives

> 3. Conditional statement with several alternatives

The conditional statement is used to branch in the program execution in function of Boolean expression. It is possible to chain more than one such branching in one program statement.

Related keywords:

- else

#### 1. Conditional statement with one alternative

### if( condition ){ statement_block };

- The if keyword introduces the conditional statement.

- condition is a Boolean expression.

- statement_block contains one or more statements. The block will only be executed if the value of the preceding logical expression (condition) is true.

Example

#### 2. Conditional statement with two alternatives

### if( condition ){ statement_block1 } else { statement_block2 };

- The if keyword introduces the conditional statement .

- condition is a Boolean expression.

- The else keyword precedes the statement block to be executed when the condition is false.

- statement_block1 contains one or more statements. The block will only be executed if the value of the preceding logical expression (condition) is true.

- statement_block2 contains one or more statements. The block will only be executed if the value of the preceding logical expression (condition) is false.

Example

#### 3. Conditional statement with several alternatives

### if( condition1 ){ statement_block1 } else if( conditionn ){ statement_blockn } else { statement_blockx };

- The if keyword introduces the conditional statement.

- condition1 is a Boolean expression.

- statement_block1 contains one or more statements. The block will only be executed if the value of condition1 is true.

- The else if keyword pair precedes the next condition (conditionn) to be checked. The keyword pair and the following conditionn and statement_blockn may be repeated as many times as needed.

- conditionn is a Boolean expression.

- statement_blockn contains one or more statements. The block will only be executed if the value of conditionn is true AND all preceding conditions (conditioni, where i = 1 .. n-1) are false.

- The else keyword introduces the conditional statement.

- statement_blockx contains one or more statements. The block will only be executed if the value of all preceding conditions (conditioni, where i = 1 .. n) is false.

Example

Example 1:

if (v_date == "1.1.2000") { log ( "apage" ) };

When the variable v_date equals the character string 1.1.2000, the word apage will be written to the log.

Example 2:

if (v_datum == "1.1.2000") { log ( "apage" ) } else { log ( "satanas" ) };

When the variable v_datum equals the character string 1.1.2000, the word apage, else the word satanas will be written to the log.

Example 3:

if (v_data == "red") { log ( "rot" ) } else if (v_data == "blue"){ log ( "blau" ) } else { log ( "duester" ) };

When the variable v_data equals the character string red, the word rot will be written to the log. .When the variable v_data equals the character string blue, the word blau will be written to the log. When the variable v_data contains something else, the word duester will be written to the log.

BNF definition of if