Exploring PL/SQL Part 2: Loops, Conditions, and Mathematical Functions

mauriciogmauriciog
4 min read
  1. FOR Loop:

    The FOR loop is a powerful tool in PL/SQL for iterating over a set of values. An interesting clarification is that it's not necessary to declare the iteration variable (i) when using the PLS_INTEGER data type. This is because the PLS_INTEGER type is automatically initialized and optimizes loop performance.

sqlCopy codeFOR i IN 1..10 LOOP
  -- Your code here
  DBMS_OUTPUT.PUT_LINE(i);
END LOOP;

In addition to iterating in ascending order, the FOR loop can also traverse a collection in reverse order using the REVERSE keyword. This functionality is particularly useful when you need to process the elements in the opposite direction of the index.

sqlCopy codeFOR i IN REVERSE 1..10 LOOP
  -- Your code here
  DBMS_OUTPUT.PUT_LINE(i);
END LOOP;
  1. WHILE:

    With a Boolean Condition: PL/SQL also supports WHILE loops, which execute while a certain condition is met. An interesting application of this loop is using a Boolean variable as the condition. This allows us to control the flow of the loop based on the state of the Boolean variable, providing flexibility in code execution.

sqlCopy codeDECLARE
  condition BOOLEAN := TRUE;
  counter PLS_INTEGER := 1;
BEGIN
  WHILE condition LOOP
    -- Your code here
    DBMS_OUTPUT.PUT_LINE('Iteration ' || counter);
    counter := counter + 1;

    IF counter > 5 THEN
      condition := FALSE;
    END IF;
  END LOOP;
END;

The WHILE NOT loop is a variation of the WHILE loop and is used when you want the loop to repeat while a condition is false. This control structure is useful when you need to process a block of code until a specific condition is met.

sqlCopy codeDECLARE
  condition BOOLEAN := FALSE;
  counter PLS_INTEGER := 1;
BEGIN
  WHILE NOT condition LOOP
    -- Your code here
    DBMS_OUTPUT.PUT_LINE('Iteration ' || counter);
    counter := counter + 1;

    IF counter > 5 THEN
      condition := TRUE;
    END IF;
  END LOOP;
END;

Sometimes, you may need to exit a WHILE loop before the termination condition is met. In PL/SQL, we can achieve this using the EXIT statement. This statement allows us to immediately exit the loop and continue execution of the code following the loop.

sqlCopy codeDECLARE
  condition BOOLEAN := TRUE;
  counter PLS_INTEGER := 1;
BEGIN
  WHILE condition LOOP
    -- Your code here
    DBMS_OUTPUT.PUT_LINE('Iteration ' || counter);
    counter := counter + 1;

    IF counter > 5 THEN
      EXIT; -- Exit the loop
    END IF;
  END LOOP;
END;
  1. GOTO

    The GOTO statement is used to redirect the program flow to a specific location. Although its usage should be careful to avoid complexity and spaghetti code, in specific situations, it can improve code readability and efficiency. Additionally, PL/SQL allows labeling sections of code using the <<label_name>> syntax for better control and clarity of jumps.

sqlCopy code<<label_name>>
LOOP
  -- Your code here
  IF condition THEN
    GOTO label_name; -- Jump to the labeled section
  END IF;
END LOOP;
  1. ROUND and SQRT

The ROUND function is used to round a number to a specific number of decimal places. This function is particularly useful in financial or scientific applications where decimal precision is crucial. It can be applied to numeric values or columns in a table, allowing precise rounding of data.

sqlCopy codeDECLARE
  number_value NUMBER := 3.14159;
  rounded_value NUMBER;
BEGIN
  rounded_value := ROUND(number_value, 2); -- Round to 2 decimal places
  DBMS_OUTPUT.PUT_LINE('Rounded Value: ' || rounded_value);
END;

The SQRT function calculates the square root of a given number. This function is valuable when working with mathematical or scientific calculations that involve square roots. It provides accurate results and can be used with numeric values or columns in a table.

sqlCopy codeDECLARE
  number_value NUMBER := 25;
  sqrt_value NUMBER;
BEGIN
  sqrt_value := SQRT(number_value);
  DBMS_OUTPUT.PUT_LINE('Square Root: ' || sqrt_value);
END;

In this short blog post, we have explored some fundamental features of PL/SQL, focusing on loops, conditions, and mathematical functions. We have seen how to use the FOR loop, taking advantage of the optimization of the PLS_INTEGER iteration variable. Additionally, we have explored the WHILE loop with different conditions and the use of the EXIT statement for premature loop termination. Lastly, we examined the ROUND and SQRT functions, which provide essential tools for manipulating numbers in PL/SQL.

2
Subscribe to my newsletter

Read articles from mauriciog directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

mauriciog
mauriciog