Chapter 3 Control structures
Chapter 3 Control structures
1. Introduction
2. Conditional control structures
3. Iterative control structures
2
4.1 Introduction
Control structures are the most important contribution to extending SQL to
PL/SQL.
Not only does PL/SQL allow us to manipulate Oracle data, it allows us to
manipulate data using:
Conditional structures:
PL/SQL offers two structures for programming a conditional action: the IF structure and the CASE
structure
Repetitive structures:
In PL/SQL, repetitive structures (in which there are three; FOR, WHILE, LOOP) all use the LOOP
statement.
3
4.2 Conditional control structures
IF Structure
The IF instruction is used to execute a sequence of conditional instructions.
Depending on the tests to be programmed, we can distinguish three forms of IF
structure:
IF-THEN (si-alors)
IF-THEN-ELSE,
IF-THEN-ELSIF.
NB:
The IF statement ends with «END IF» and not «ENDIF».
4
4.2 Conditional control structures
IF Structure
IF-THEN
IF-THEN-ELSE
5
4.2 Conditional control structures
IF structure
IF-THEN-ELSIF
6
4.2 Conditional control structures
Exercise
Write a program that allows you to find out if a telephone number (declared
and initialized in the DECLARE section) is a landline number or a mobile
number.
NB1: Use the function SUBSTR
SUBSTR (string, start, [lenght]): Extracts a string from the start index having the length
lenght. If start is negative then we start from the end.
7
4.2 Conditional control structures
Solution
8
4.2 Conditional control structures
Solution
9
4.2 Conditional control structures
CASE structure
To choose between several values or several actions, you can use the CASE
instruction,
We have two possible forms to use the CASE statement:
The CASE structure evaluates a condition and returns a value for each case,
The CASE structure evaluates a condition and performs an action (which can be a PL/SQL block) for
each case.
10
4.2 Conditional control structures
CASE structure
First form:
11
4.2 Conditional control structures
CASE structure
Second form:
NB: If the ELSE clause does not exist, and in the event that no condition is valid, the
system returns an error.
12
4.2 Conditional control structures
Exercise
Write a PL/SQL code that allows you to find the mention of a student from
his grade (declared and initialized in the DECLARE part or entered at the
console).
NB:
Use the PROMPT command to display a message to the console
Use the ACCEPT command to retrieve what was entered at the console.
13
4.2 Conditional control structures
Solution
14
4.3 Iterative control structures
In PL/SQL, repetitive structures (which are three) all use the LOOP
statement:
LOOP,
WHILE,
FOR.
15
4.3 Iterative control structures
LOOP
In general, the LOOP instruction executes a sequence of instructions more than one
time.
The LOOP keyword is placed before the first instruction in the sequence and the
END LOOP keyword is placed after the last instruction in the sequence.
The syntax is as follows:
LOOP
--instructions to execute
END LOOP;
16
4.3 Iterative control structures
LOOP
To exit the LOOP, use the EXIT instruction as follows:
LOOP
--instructions to execute
EXIT [WHEN condition;]
END LOOP;
The particularity of this structure is that the first iteration is carried out
whatever the initial conditions.
The condition is only evaluated at the end of the loop.
If no condition is specified (no condition), the loop is exited immediately.
If the condition is false, the sequence of instructions is executed again. This process continues until the
condition is true to go into sequence after the END LOOP.
17
4.3 Iterative control structures
Exercise
Write a PL/SQL code that calculates the sum of the first n integers :
sum=n+n-1+n-2+…+2+1.
Write a PL/SQL code that calculates the factorial of 10.
18
4.3 Iterative control structures
Solution
DECLARE
v_somme NUMBER(4) := 0;
v_entier NUMBER(3) := 1;
BEGIN
LOOP
v_somme:= v_somme+v_entier;
v_entier:= v_entier+ 1;
EXIT WHEN v_entier=10;
END LOOP;
DBMS_OUTPUT.PUT_LINE ('Somme = ' || v_somme);
END;
19
4.3 Iterative control structures
Solution
DECLARE
v_fac NUMBER := 1;
v_entier NUMBER(3) := 10;
BEGIN
LOOP
v_fac:= v_fac* v_entier;
v_entier:= v_entier-1;
EXIT WHEN v_entier=0;
END LOOP;
DBMS_OUTPUT.PUT_LINE (‘10!= ' || v_fac);
END;
20
4.3 Iterative control structures
WHILE-LOOP
The WHILE-LOOP instruction associates a condition of a sequence of instructions.
Before each iteration of the loop, the condition is evaluated. If the condition is true,
the sequence of instructions is executed, then control resumes at the start of the
loop.
If the condition is false or null, the loop is skipped and control proceeds to the next
statement (after the END LOOP).
Syntax:
WHILE condition LOOP
--instructions;
END LOOP;
21
4.3 Iterative control structures
WHILE-LOOP
DECLARE
v_somme NUMBER(4) := 0;
v_entier NUMBER(3) := 1;
BEGIN
WHILE (v_entier<=5) LOOP
v_somme:= v_somme+v_entier;
v_entier:= v_entier+ 1;
END LOOP;
DBMS_OUTPUT.PUT_LINE ('Somme = ‘ | | v_somme);
END;
22
4.3 Iterative control structures
FOR-LOOP
The FOR-LOOP loop, called counter, makes it possible to carry out an iterative loop
associated with an integer variable which will be incremented at each iteration.
The general syntax of the FOR-LOOP structure is as follows:
The number of iterations is known in advance even before the start of the loop.
We use a colon (..) as an interval operator.
The step of the FOR loop is always 1.
23
4.3 Iterative control structures
Exercise
Calculate the sum of the first 50 integers.
Calculate the factorial of 9.
24
4.3 Iterative control structures
Solution
DECLARE
v_somme NUMBER(4) := 0;
BEGIN
FOR v_compteur IN 1..50 LOOP
v_somme:= v_somme+ v_compteur;
END LOOP;
DBMS_OUTPUT.PUT_LINE ('La somme= ' || v_somme);
END;
25
4.3 Iterative control structures
Solution
DECLARE
v_fact NUMBER := 1;
BEGIN
FOR v_compteur IN 1..9 LOOP
v_fact:= v_fact* v_compteur;
END LOOP;
DBMS_OUTPUT.PUT_LINE ('le factoriel= ‘ | | v_fact);
END;
26
4.3 Iterative control structures
NB1:
Inside the FOR loop, the counter cannot be changed
FOR v_compteur IN 1..9 LOOP
…
v_compteur:=8; /*expression 'V_COMPTEUR' cannot be used as an assignment target
*/
END LOOP;
NB2:
the counter variable of the FOR is only accessible inside of the loop.
FOR v_compteur IN 1..9 LOOP
…
END LOOP;
DBMS_OUTPUT.PUT_LINE (v_compteur); --'V_COMPTEUR’ must be declared
27