Lab 5.1 IF Statements

Lab Objectives

After this Lab, you will be able to:

Use IF-THEN Statements

Use IF-THEN-ELSE Statements

An IF statement has two forms: IF-THEN and IF-THEN-ELSE. An IF-THEN statement allows you to specify only one group of actions to take. In other words, this group of actions is taken only when a condition evaluates to TRUE. An IF-THEN-ELSE statement allows you to specify two groups of actions, and the second group of actions is taken when a condition evaluates to FALSE or NULL.

IF-THEN Statements

An IF-THEN statement is the most basic kind of a conditional control and has the following structure:

```IF

CONDITION

THEN
STATEMENT 1;
...
STATEMENT N;
END IF;
```

The reserved word IF marks the beginning of the IF statement. Statements 1 through N are a sequence of executable statements that consist of one or more of the standard programming structures. The word CONDITION between keywords IF and THEN determines whether these statements are executed. END IF is a reserved phrase that indicates the end of the IF-THEN construct.

This flow of the logic from the preceding structure of the IF-THEN statement is illustrated in the Figure 5.1.

Figure 5.1. IF-THEN Statement

When an IF-THEN statement is executed, a condition is evaluated to either TRUE or FALSE. If the condition evaluates to TRUE, control is passed to the first executable statement of the IF-THEN construct. If the condition evaluates to FALSE, control is passed to the first executable statement after the END IF statement.

Consider the following example. You have two numeric values stored in the variables , v_num1 and v_num2 . You need to arrange your values so that the smaller value is always stored in v_num1 , and the larger value is always stored in the v_num2 .

FOR EXAMPLE

```DECLARE
v_num1 NUMBER := 5;
v_num2 NUMBER := 3;
v_temp NUMBER;
BEGIN
-- if v_num1 is greater than v_num2 rearrange their values
IF v_num1 > v_num2 THEN
v_temp := v_num1;
v_num1 := v_num2;
v_num2 := v_temp;
END IF;

-- display the values of v_num1 and v_num2
DBMS_OUTPUT.PUT_LINE ('v_num1 = 'v_num1);
DBMS_OUTPUT.PUT_LINE ('v_num2 = 'v_num2);
END;
```

In this example, condition v_num1 > v_num2 evaluates to TRUE because 5 is greater that 3. Next, the values are rearranged so that 3 is assigned to v_num1 , and 5 is assigned to v_num2 . It is done with the help of the third variable, v_temp , which is used for temporary storage.

This example produces the following output:

```
v_num1 = 3

v_num2 = 5

PL/SQL procedure successfully completed.

```

IF-THEN-ELSE Statement

An IF-THEN statement specifies the sequence of statements to execute only if the condition evaluates to TRUE. When this condition evaluates to FALSE, there is no special action to take except to proceed with execution of the program.

An IF-THEN-ELSE statement enables you to specify two groups of statements. One group of statements is executed when the condition evaluates to TRUE. Another group of statements is executed when the condition evaluates to FALSE. This is indicated as follows :

```IF

CONDITION

THEN
STATEMENT 1;
ELSE
STATEMENT 2;
END IF;
STATEMENT 3;
```

When CONDITION evaluates to TRUE, control is passed to STATEMENT 1; when CONDITION evaluates to FALSE, control is passed to STATEMENT 2. After the IF-THEN-ELSE construct has completed, STATEMENT 3 is executed. This flow of the logic is illustrated in the Figure 5.2.

Figure 5.2. IF-THEN-ELSE Statement

 The IF-THEN-ELSE construct should be used when trying to choose between two mutually exclusive actions. Consider the following example: ```DECLARE v_num NUMBER := &sv_user_num; BEGIN -- test if the number provided by the user is even IF MOD(v_num,2) = 0 THEN DBMS_OUTPUT.PUT_LINE (v_num' is even number'); ELSE DBMS_OUTPUT.PUT_LINE (v_num' is odd number'); END IF; DBMS_OUTPUT.PUT_LINE ('Done'); END; ``` It is important to realize that for any given number only one of the DBMS_ OUTPUT.PUT_LINE statements is executed. Hence, the IF-THEN-ELSE construct enables you to specify two and only two mutually exclusive actions. When run, this example produces the following output: ``` Enter value for v_user_num: 24 old 2: v_num NUMBER := &v_user_num; new 2: v_num NUMBER := 24; 24 is even number Done PL/SQL procedure successfully completed. ```

NULL Condition

In some cases, a condition used in an IF statement can be evaluated to NULL instead of TRUE or FALSE. For the IF-THEN construct, the statements will not be executed if an associated condition evaluates to NULL. Next, control will be passed to the first executable statement after END IF. For the IF-THEN-ELSE construct, the statements specified after the keyword ELSE will be executed if an associated condition evaluates to NULL.

FOR EXAMPLE

```DECLARE
v_num1 NUMBER := 0;
v_num2 NUMBER;
BEGIN
IF v_num1 = v_num2 THEN
DBMS_OUTPUT.PUT_LINE ('v_num1 = v_num2');
ELSE
DBMS_OUTPUT.PUT_LINE ('v_num1 != v_num2');
END IF;
END;
```

This example produces the following output:

```
v_num1 != v_num2

PL/SQL procedure successfully completed.

```

The condition

```
v_num1 = v_num2

```

is evaluated to NULL because a value is not assigned to the variable v_num2 . Therefore, variable v_num2 is NULL. Notice that the IF-THEN-ELSE construct is behaving as if the condition evaluated to FALSE, and the second DBMS_OUTPUT.PUT_LINE statement is executed.