0% found this document useful (0 votes)
5 views

Lesson 4 - Control Structures - Practical

Uploaded by

Uapi Muukua
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lesson 4 - Control Structures - Practical

Uploaded by

Uapi Muukua
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Faculty of Computing and Informatics

Lesson 4 – Practical
Control Structures
Learning Objectives

After studying this lesson, you should be able to:

• Identify the uses and types of control structures


• Construct an IF statement
• Use CASE statements and CASE expressions
• Construct and identify different loop statements
NOTE

Practicals are depended on the theory session. Lecturer must execute


the practical after the theory session. The practical Lecturers will be notified to
start with a practical. This will maintain consistency and quality amongst the
various practical groups. Students need to understand the theoretical
background before they can apply the theory into practical.
IF-THEN

IFboolean-expression THEN
statements
END IF
;

IF v_user_id <> 0 THEN


UPDATE users SET email = v_email WHERE user_id = v_user_id;
END IF;
IF-THEN-ELSIF-ELSE

IFboolean-expression THEN
statements [ ELSIF boolean-expression THEN
statements [ ELSIF boolean-expression THEN
statements ...]]
[ ELSE statements ]
END IF;

IF number = 0 THEN
result := ''zero''; ELSIF number > 0 THEN result := ''positive'';
ELSIF number < 0 THEN result := ''negative'';
ELSE -- hmm, the only other possibility is that number IS NULL
result := ''NULL'';
END IF;
LOOP
-- some computations Loops
IF count > 0 THEN EXIT;
-- exit loop
END IF;
END LOOP; [<<label>>]
LOOP LOOP statements
-- some computations END LOOP;
EXIT WHEN count > 0;
END LOOP;
BEGIN
-- some computations IF stocks > 100000
THEN EXIT;
-- illegal. Can't use EXIT outside of a LOOP
END IF;
END;
WHILE
[<<label>>]
WHILE expression LOOP
statements
END LOOP;

WHILE amount_owed > 0 AND gift_certificate_balance > 0 LOOP


-- some computations here
END LOOP;
WHILE NOT boolean_expression LOOP
-- some computations here
END LOOP;
FOR (integer for-loop)
[<<label>>]
FOR name IN [ REVERSE ] expression .. expression LOOP
statements
END LOOP;

FOR i IN 1..10 LOOP


-- some expressions here RAISE NOTICE ''i is %'',i;
END LOOP; FOR i IN REVERSE 10..1 LOOP
-- some expressions here
END LOOP;

You might also like