PL/SQL Loops. Building Logical Conditions All logical conditions must yield a boolean condition. You can build a simple Boolean condition by combining.

Slides:



Advertisements
Similar presentations
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
Objectives In this chapter, you will learn about:
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Loops – While, Do, For Repetition Statements Introduction to Arrays
Chapter 4 Loops and Character Manipulation Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
C++ for Engineers and Scientists Third Edition
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming Logic and Design Fifth Edition, Comprehensive
Programming Logic and Design Sixth Edition Chapter 5 Looping.
Program with PL/SQL Lesson 4. Writing Control Structure.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Bordoloi and Bock Control Structures: Iterative Control.
6.2 For…Next Loops General Form of a For…Next Loop
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
1 Writing Control Structures Part D. 2 PL/SQL Decision Control Structures Sequential processing Sequential processing Processes statements one after another.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
55:032 - Intro. to Digital DesignPage 1 VHDL and Processes Defining Sequential Circuit Behavior.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
Perl Chapter 3 Conditional statements. Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational,
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Fundamentals of PL/SQL part 2 (Basics)
Chapter 6: Loops.
Chapter 4 Repetition Statements (loops)
Program with PL/SQL Lesson 4.
Writing Control Structures
Chapter 3: Decisions and Loops
Programming Logic and Design Fourth Edition, Comprehensive
JavaScript: Control Statements I
Writing Control Structures
JavaScript: Control Statements.
Database Management Systems 2
Arrays, For loop While loop Do while loop
Outline Altering flow of control Boolean expressions
Chapter 8 JavaScript: Control Statements, Part 2
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 7 Additional Control Structures
Writing Control Structures
3 Control Statements:.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chapter 8: More on the Repetition Structure
Chapter 6 Control Statements: Part 2
Flow of Control.
CSC215 Lecture Control Flow.
Database Programming Using Oracle 11g
Chapter 8 JavaScript: Control Statements, Part 2
Presentation transcript:

PL/SQL Loops

Building Logical Conditions All logical conditions must yield a boolean condition. You can build a simple Boolean condition by combining number, character or date expressions with a comparison operator Examples: v_name1 = v_name2 v_name != ‘Ali’ v_cgpa between 2.00 and 4.00 v_cgpa > 2.50 v_name=‘Ali’ and v_deptno=35 and v_cgpa between 2.00 and 4.00

Using NULL values in Logical Conditions MUSTYou MUST handle null values with the IS NULL or IS NOT NULL operator. Any arithmetic expression containing a null value evaluates to NULL. Concatenated expressions with null values treat null values as an empty string.

Logic Tables NOT TRUE FALSE NULL OR TRUE FALSE NULL TRUEFALSENULL FALSE TRUE NULL AND TRUE FALSE NULL TRUEFALSENULL TRUE NULL FALSE TRUE FALSE NULL Boolean conditions with Logical operators

Boolean Conditions V_REORDER_FLAGV_AVAILABLE_FLAGV_FLAG TRUE TRUEFALSE NULLTRUE NULLFALSE v_flag := v_reorder_flag AND v_available_flag; TRUEFALSENULLFALSE What is the value of V_FLAG in each case?

Iterative Control: LOOP Statements Loops repeat a statement or sequence of statements multiple times. There are three loop types: –Basic loop –FOR loop –WHILE loop

Main uses of Loop Types Use Basic loop: to provide repetitive actions without overall conditions FOR loops: to provide iterative control of actions based on a count WHILE: loops to provide iterative control of actions based on a condition EXIT statement : to terminate loops depending on your programming logic

Basic Loop LOOP statement1;... EXIT [WHEN condition]; END LOOP; LOOP statement1;... EXIT [WHEN condition]; END LOOP; where:conditionis a Boolean variable or expression (TRUE, FALSE, or NULL); where:conditionis a Boolean variable or expression (TRUE, FALSE, or NULL); -- delimiter showing beginning of the loop -- statements -- EXIT statement -- delimiter showing end of the loop Syntax If the exit statement of a basic loop is missing, you have an infinite loop

The EXIT Statement You can terminate a loop using the EXIT statement. Control passes to the next statement after the END LOOP statement. You can issue EXIT either as an action within an IF statement or as a standalone statement within the loop. The EXIT statement must be placed inside a loop. –You can attach a WHEN clause to allow conditional termination of the loop. When the EXIT statement is encountered, the condition in the WHEN clause is evaluated. –If the condition yields TRUE, the loop ends and control passes to the next statement after the loop. A basic loop can contain multiple EXIT statements.

Basic Loop DECLARE v_counterNUMBER(2) := 0; BEGIN LOOP DBMS_OUTPUT.PUT_LINE(‘Current Value is ‘||v_counter); v_counter := v_counter + 1; EXIT WHEN v_counter > 10; END LOOP; END; DECLARE v_counterNUMBER(2) := 0; BEGIN LOOP DBMS_OUTPUT.PUT_LINE(‘Current Value is ‘||v_counter); v_counter := v_counter + 1; EXIT WHEN v_counter > 10; END LOOP; END; Example: Common mistake: failing to increment v_counter will result in an infinite loop!

FOR Loop FOR counter in [REVERSE] lower_bound..upper_bound LOOP statement1; statement2;... END LOOP; FOR counter in [REVERSE] lower_bound..upper_bound LOOP statement1; statement2;... END LOOP; Syntax Use a FOR loop to shortcut the test for the number of iterations. You do not have to declare the index; it is declared implicitly.

Counter : an implicitly declared integer whose value automatically increases or decreases by 1 on each iteration of the loop until the upper or lower bound is reached REVERSE : causes the index to decrement with each iteration from the upper bound to the lower bound –The lower bound is still referenced first. –If the lower bound of the loop range evaluates to a larger integer than the upper bound, the sequence of statements will not be executed. FOR Loop

Guidelines –Reference the counter within the loop only; it is undefined outside the loop. –Use an expression to reference the existing value of a counter. –Do not reference the counter as the target of an assignment.

FOR Loop BEGIN FOR i IN LOOP DBMS_OUTPUT.PUT_LINE(‘Current value is ‘|| i); END LOOP; END; BEGIN FOR i IN LOOP DBMS_OUTPUT.PUT_LINE(‘Current value is ‘|| i); END LOOP; END; Print numbers 1 to 10 on the screen Example

WHILE Loop WHILE condition LOOP statement1; statement2;... END LOOP; WHILE condition LOOP statement1; statement2;... END LOOP; Condition is evaluated at the beginning of each iteration. Syntax Use the WHILE loop to repeat statements while a condition is TRUE.

You can use the WHILE loop to repeat a sequence of statements until the controlling condition is no longer TRUE. –The condition is evaluated at the start of each iteration. –The loop terminates when the condition is FALSE. If the condition is FALSE at the start of the loop, then no further iterations are performed. If the condition is NULL, the loop is bypassed and control passes to the next statement. If the variables involved in the conditions do not change during the body of the loop, then the condition remains TRUE and the loop does not terminate—infinite loop. WHILE Loop

DECLARE v_counterNUMBER(2) := 1; BEGIN WHILE v_counter <= 10 LOOP DBMS_OUTPUT.PUT_LINE(‘Current Value is’ || v_counter); v_counter := v_counter + 1; END LOOP; END; DECLARE v_counterNUMBER(2) := 1; BEGIN WHILE v_counter <= 10 LOOP DBMS_OUTPUT.PUT_LINE(‘Current Value is’ || v_counter); v_counter := v_counter + 1; END LOOP; END; Print numbers 1 to 10 on the screen Example

Nested Loops and Labels Nest loops to multiple levels. –You can nest FOR, WHILE, and basic loops within one another –The termination of a nested loop does not terminate the enclosing loop unless an exception was raised Use labels to distinguish between blocks and loops. Exit the outer loop with the EXIT statement referencing the label.

A label is placed before a statement, either on the same line or on a separate line. Label loops by placing the label before the word LOOP within label delimiters ( >). If the loop is labeled, the label name can optionally be included after the END LOOP statement for clarity. Nested Loops and Labels

... BEGIN > LOOP v_counter := v_counter+1; EXIT WHEN v_counter>10; > LOOP... EXIT Outer_loop WHEN total_done = 'YES'; -- Leave both loops EXIT WHEN inner_done = 'YES'; -- Leave inner loop only... END LOOP Inner_loop;... END LOOP Outer_loop; END;... BEGIN > LOOP v_counter := v_counter+1; EXIT WHEN v_counter>10; > LOOP... EXIT Outer_loop WHEN total_done = 'YES'; -- Leave both loops EXIT WHEN inner_done = 'YES'; -- Leave inner loop only... END LOOP Inner_loop;... END LOOP Outer_loop; END; Using the label of the outer loop in the exit statement terminates the outer loop as well as the inner loop.

SummarySummary This week we learned about Loops –Basic loop –FOR loop –WHILE loop –EXIT statement Next Lesson we will learn how to use SELECT and DML statements inside PL/SQL blocks.