How to Print 1 to 10 Without Using Loop in PL/SQL?

How to Print 1 to 10 Without Using Loop in PL/SQL?

  • PLSQL
  • 1 min read

Here I am giving an example to print 1 to 10 without using the loop in PL/SQL. You can print up to any number by changing the value 10 to any number, for example, 100.

PL/SQL Program to Print 1 to 10 Without Using Loop

In the below program, I am using PL/SQL labels instead of the loop to increment the value and print. In label named (label_main), I am incrementing the value of the variable (i) and checking if its value greater than 10 then passing control to a label named (label_end) else passing control to label (label_main). You can change the value in the IF condition from 10 to the desired number to print up to that number.

SET SERVEROUTPUT ON;

DECLARE
   i   NUMBER;
BEGIN
   i := 0;

  <<label_main>>
   i := i + 1;

   IF i > 10
   THEN
      GOTO label_end;
   END IF;

  <<label_print>>
   DBMS_OUTPUT.put_line (i);
   GOTO label_main;

  <<label_end>>
   NULL;
END;
/

Output

1
2
3
4
5
6
7
8
9
10
PL/SQL procedure successfully completed.