![]() |
In Oracle PL/SQL there are different loop statements to repeat a block of code until a certain condition is met to exit the loop. Loop…End Loop, While Loop…End Loop and For Loop … End Loop. In this article, we will discuss PL/SQL WHILE loop syntax, which involves variable declarations, loop initiation, condition definition, and an optional EXIT WHEN statement for controlled termination. This structure allows developers to create flexible and dynamic iterations tailored to specific programming needs. WHILE Loop StatementIn PL/SQL, the WHILE loop statement is a control structure statement that repeatedly executes a code block that is inside the ‘While Loop’ as long as the specific condition set in the ‘While Loop’ is TRUE. The condition set in the ‘WHILE’ statement is a boolean expression that evaluates to TRUE, FALSE, or NULL. To terminate the loop prematurely or based on some specific scenario then the EXIT or EXIT WHEN statement is used. The ‘WHILE LOOP’ is used when you are not sure about the number of times the code block needs to execute. Only during the WHILE Loop execution, the specific condition set to end the execution is made TRUE and the control moves out of the WHILE Loop statement. Syntax:
Examples of PL/SQL WHILE LoopExample 1: Using PL/SQL WHILE Loop for Iterative ExecutionIn this example, we delve into the usage of the PL/SQL WHILE loop for iterative execution. The code demonstrates a basic scenario where a counter variable is initialized and incremented within the loop, with the loop executing until a specified condition is met. DECLARE Explanation:
In the above example, the WHILE loop will iterate as long as the counter is less than or equal to 5. Output: Statement processed. Example 2: WHILE Loop Example Terminated by EXIT WHEN StatementThis example illustrates the use of a PL/SQL WHILE loop with the EXIT WHEN statement, showcasing a scenario where the loop iterates until a certain condition is met. The code calculates the sum of numbers until the total sum reaches or exceeds 10. DECLARE In the above example, the loop continues as long as the total_sum is less than 10. The loop adds the current_number to the total_sum in each iteration and increments the current_number. The loop will exit when the total_sum becomes greater than or equal to 10. Output: Statement processed. ConclusionIn conclusion, the WHILE loop in PL/SQL provides a powerful control structure for executing a block of statements repeatedly as long as a specified condition holds true. This WHILE loop structure allows for flexible control flow within a PL/SQL program, enabling us to perform iterative tasks efficiently. A better understanding of how to effectively use the WHILE loop is essential for writing efficient and readable PL/SQL code. This is true especially when dealing with scenarios that require repetitive execution until a specific condition is met. |
Reffered: https://www.geeksforgeeks.org
Databases |
Related |
---|
|
![]() |
![]() |
![]() |
|
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |