Quantum Computing Beyond the Basics
We keep dialing a number until the call connects
We read pages until a chapter ends
We add items to a cart until shopping is complete
Programming works in the same way. Instead of writing the same statement again and again, PL/SQL uses looping statements to repeat a block of code automatically.
Looping statements help PL/SQL programs to:
Reduce code length
Improve readability
Handle repetitive tasks efficiently
Work with large data sets
Without loops, PL/SQL programs would be long, slow, and difficult to maintain.
A loop is a control structure that allows a set of statements to be executed repeatedly based on a condition or a fixed number of times.
In simple words:
- A loop tells PL/SQL: “Repeat this task until a certain rule is satisfied.”
Every loop has three important parts:
Initialization – starting point
Condition / Termination – when to stop
Iteration – how repetition happens
PL/SQL provides the following looping statements:
Basic LOOP
WHILE LOOP
FOR LOOP
Nested LOOP
EXIT and EXIT WHEN
CONTINUE statement
Each one is explained in very small detail below.
Important point:
PL/SQL does not stop this loop automatically
You must write an EXIT condition manually
LOOPstatements;EXIT WHEN condition;END LOOP;
Loop → checking gate
EXIT → supervisor arrives
DECLAREcounter NUMBER := 1;BEGINLOOPDBMS_OUTPUT.PUT_LINE('Counter value: ' || counter);counter := counter + 1;EXIT WHEN counter > 5;END LOOP;END;
LOOP starts execution
Statements execute repeatedly
EXIT WHEN checks stopping condition
END LOOP marks loop end
If EXIT is missing, the loop becomes infinite, which is dangerous.
This loop is useful when:
The number of iterations is not fixed
Execution depends on a condition
WHILE condition LOOPstatements;END LOOP;
Condition → money > 0
Action → buy items
DECLAREnumber NUMBER := 1;BEGINWHILE number <= 5 LOOPDBMS_OUTPUT.PUT_LINE('Number is: ' || number);number := number + 1;END LOOP;END;
Condition checked first
Loop may execute zero times
Safe compared to basic LOOP
Reading records until condition fails
Processing user input
Monitoring status flags
The FOR LOOP is used when:
Number of repetitions is known in advance
Loop control variable is required automatically
PL/SQL handles:
Initialization
Condition checking
Increment / decrement
Internally, making it clean and error-free.
FOR counter IN start_value .. end_value LOOPstatements;END LOOP;
Start → 1
End → 30
Action → call name
BEGINFOR i IN 1..5 LOOPDBMS_OUTPUT.PUT_LINE('Iteration number: ' || i);END LOOP;END;
Counter variable is automatically declared
Counter cannot be modified inside loop
Loop runs fixed number of times
PL/SQL also supports reverse iteration.
FOR i IN REVERSE 1..5 LOOPDBMS_OUTPUT.PUT_LINE(i);END LOOP;
A nested loop means:
One loop inside another loop
Used when:
Repetition depends on another repetition
Working with tables, matrices, or grouped data
For each student
For each subject
Record marks
BEGINFOR i IN 1..3 LOOPFOR j IN 1..2 LOOPDBMS_OUTPUT.PUT_LINE('i = ' || i || ', j = ' || j);END LOOP;END LOOP;END;
Inner loop completes fully for each outer loop cycle
Complexity increases quickly
Use carefully for performance
The EXIT statement is used to terminate a loop immediately, even if iterations are remaining.
EXIT;
or
EXIT WHEN condition;
Stop searching once file is found
DECLAREnum NUMBER := 1;BEGINLOOPDBMS_OUTPUT.PUT_LINE(num);EXIT WHEN num = 3;num := num + 1;END LOOP;END;
Prevents infinite loops
Improves performance
Allows early termination
The CONTINUE statement skips the current iteration and moves to the next iteration of the loop.
Important:
Loop does not terminate
Only current cycle is skipped
CONTINUE;
or
CONTINUE WHEN condition;
Skip absent students
Continue checking others
BEGINFOR i IN 1..5 LOOPCONTINUE WHEN i = 3;DBMS_OUTPUT.PUT_LINE('Value: ' || i);END LOOP;END;
Value 3 is skipped
Loop continues normally
Forgetting EXIT in basic LOOP
Modifying FOR loop counter
Creating infinite loops
Misplacing END LOOP
Not updating loop variable in WHILE
| Loop Type | Condition Check | Use Case |
|---|---|---|
| Basic LOOP | Inside loop | Unlimited repetition |
| WHILE LOOP | Before loop | Condition-based repetition |
| FOR LOOP | Automatic | Fixed repetition |
Handle large data efficiently
Reduce manual coding
Implement real-world logic
By mastering:
LOOP
WHILE
FOR
EXIT
CONTINUE
You gain the ability to write professional PL/SQL programs used in:
Database processing
Reports
Payroll systems
Data validation
Comments
Post a Comment