SQL> --loops SQL> --write an anymous block to print numbers 1 to 5 using a while loop SQL> set serveroutput on SQL> declare 2 . 3 . SQL> ed Wrote file afiedt.buf 1 declare 2 i number; 3 begin 4 i := 1; 5 while (i<=5) 6 loop 7 dbms_output.put_line(i); 8 i:=i+1; 9 end loop; 10* end; SQL> / 1 2 3 4 5 PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 declare 2 i number; 3 begin 4 i := 1; 5 while (i<=5) 6 loop 7 dbms_output.put_line('i'); 8 i:=i+1; 9 end loop; 10* end; SQL> / i i i i i PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 declare 2 i number; 3 begin 4 i := 1; 5 while (i<=5) 6 loop 7 dbms_output.put_line('the value of i is'||i); 8 i:=i+1; 9 end loop; 10* end; SQL> / the value of i is1 the value of i is2 the value of i is3 the value of i is4 the value of i is5 PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 declare 2 i number; 3 begin 4 i := 1; 5 while (i<=5) 6 loop 7 dbms_output.put_line('the value of i is '||i); 8 i:=i+1; 9 end loop; 10* end; SQL> / the value of i is 1 the value of i is 2 the value of i is 3 the value of i is 4 the value of i is 5 PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 declare 2 i number := 1; 3 begin 4 while (i<=5) 5 loop 6 dbms_output.put_line('the value of i is '||i); 7 i:=i+1; 8 end loop; 9* end; 10 / the value of i is 1 the value of i is 2 the value of i is 3 the value of i is 4 the value of i is 5 PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 declare 2 i number := 1; 3 begin 4 while (i<=5) 5 loop 6 dbms_output.put_line('the value of i is '||i); 7 i:=i+1; 8 end loop; 9* end; SQL> --modift to print even number from 0 to 10 including boundaries SQL> ed Wrote file afiedt.buf 1 declare 2 i number := 0; 3 begin 4 while (i<=10) 5 loop 6 if mod(i,2) = 0 then 7 dbms_output.put_line('the value of i is '||i); 8 end if; 9 i:=i+1; 10 end loop; 11* end; 12 / the value of i is 0 the value of i is 2 the value of i is 4 the value of i is 6 the value of i is 8 the value of i is 10 PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 declare 2 i number := 0; 3 begin 4 while (i<=10) 5 loop 6 dbms_output.put_line('the value of i is '||i); 7 i:=i+2; 8 end loop; 9* end; SQL> / the value of i is 0 the value of i is 2 the value of i is 4 the value of i is 6 the value of i is 8 the value of i is 10 PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 declare 2 i number := 0; 3 begin 4 while (i<=10) 5 loop 6 dbms_output.put_line('the value of i is '||i); 7 i:=i+2; 8 exit; 9 end loop; 10* end; SQL> / the value of i is 0 PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 declare 2 i number := 0; 3 begin 4 while (i<=10) 5 loop 6 dbms_output.put_line('the value of i is '||i); 7 i:=i+2; 8 exit; 9 end loop; 10* end; SQL> / the value of i is 0 PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 declare 2 i number := 0; 3 begin 4 while (i<=10) 5 loop 6 dbms_output.put_line('the value of i is '||i); 7 i:=i+2; 8 if i =4 then 9 exit; 10 end if; 11 end loop; 12* end; 13 / the value of i is 0 the value of i is 2 PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 declare 2 i number := 0; 3 begin 4 while (i<=10) 5 loop 6 if i =4 then 7 exit; 8 end if; 9 dbms_output.put_line('the value of i is '||i); 10 if i =4 then 11 exit; 12 end if; 13 i:=i+2; 14 if i =4 then 15 exit; 16 end if; 17 end loop; 18* end; 19 / the value of i is 0 the value of i is 2 PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 declare 2 i number := 0; 3 begin 4 while (i<=10) 5 loop 6 exit when i=4; 7 dbms_output.put_line('the value of i is '||i); 8 i:=i+2; 9 end loop; 10* end; 11 / the value of i is 0 the value of i is 2 PL/SQL procedure successfully completed. SQL> --Write an anonymous block to print numbers 1 to 5 on screen. SQL> --Print the boundaries as well SQL> ed Wrote file afiedt.buf 1 --Write an anonymous block to print numbers 1 to 5 on screen. 2 --Print the boundaries as well 3 --use a basic loop 4 declare 5 i number := 1; 6 begin 7 loop 8 dbms_output.put_line(i); 9 i:=i+1; 10 exit when i=5; 11 end loop; 12* end; 13 / 1 2 3 4 PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 --Write an anonymous block to print numbers 1 to 5 on screen. 2 --Print the boundaries as well 3 --use a basic loop 4 declare 5 i number := 1; 6 begin 7 loop 8 dbms_output.put_line(i); 9 i:=i+1; 10 exit when i>5; 11 end loop; 12* end; SQL> / 1 2 3 4 5 PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 --Write an anonymous block to print numbers 1 to 5 on screen. 2 --Print the boundaries as well 3 --use a basic loop 4 declare 5 i number := 1; 6 begin 7 loop 8 dbms_output.put_line(i); 9 i:=i+1; 10 exit when i=6; 11 end loop; 12* end; SQL> / 1 2 3 4 5 PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 --Write an anonymous block to print numbers 1 to 5 on screen. 2 --Print the boundaries as well 3 --use a basic loop 4 declare 5 i number := 1; 6 begin 7 loop 8 exit when i=6; 9 dbms_output.put_line(i); 10 i:=i+1; 11 end loop; 12* end; SQL> / 1 2 3 4 5 PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 --Write an anonymous block to print numbers 1 to 5 on screen. 2 --Print the boundaries as well 3 --use a basic loop 4 declare 5 i number := 1; 6 begin 7 loop 8 dbms_output.put_line(i); 9 exit when i=5; 10 i:=i+1; 11 end loop; 12* end; 13 / 1 2 3 4 5 PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 --Write an anonymous block to print numbers 1 to 5 on screen. 2 --Print the boundaries as well 3 --use a basic loop 4 declare 5 i number := 0; 6 begin 7 loop 8 i:=i+1; 9 dbms_output.put_line(i); 10 exit when i=5; 11 end loop; 12* end; 13 / 1 2 3 4 5 PL/SQL procedure successfully completed. SQL> --for loop is very restrictive SQL> --for loop declares its own loop counter SQL> --stepsize is always 1 SQL> --you cannot modify the loop counter SQL> ed Wrote file afiedt.buf 1 declare 2 i number := 100; 3 begin 4 dbms_output.put_line('Before loop i='||i); 5 for i in 1 .. 5 6 loop 7 dbms_output.put_line('Inside loop i='||i); 8 i:=i+1; 9 end loop; 10 dbms_output.put_line('After loop i='||i); 11* end; SQL> / i:=i+1; * ERROR at line 8: ORA-06550: line 8, column 6: PLS-00363: expression 'I' cannot be used as an assignment target ORA-06550: line 8, column 6: PL/SQL: Statement ignored SQL> ed Wrote file afiedt.buf 1 declare 2 i number := 100; 3 begin 4 dbms_output.put_line('Before loop i='||i); 5 for i in 1 .. 5 6 loop 7 dbms_output.put_line('Inside loop i='||i); 8 end loop; 9 dbms_output.put_line('After loop i='||i); 10* end; SQL> / Before loop i=100 Inside loop i=1 Inside loop i=2 Inside loop i=3 Inside loop i=4 Inside loop i=5 After loop i=100 PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 begin 2 for i in 1 .. 5 3 loop 4 dbms_output.put_line('Inside loop i='||i); 5 end loop; 6* end; 7 / Inside loop i=1 Inside loop i=2 Inside loop i=3 Inside loop i=4 Inside loop i=5 PL/SQL procedure successfully completed. SQL> --you can print in reverse SQL> ed Wrote file afiedt.buf 1 begin 2 for i in 5 .. 1 3 loop 4 dbms_output.put_line('Inside loop i='||i); 5 end loop; 6* end; SQL> / PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 begin 2 for i in reverse 1 .. 5 3 loop 4 dbms_output.put_line('Inside loop i='||i); 5 end loop; 6* end; SQL> / Inside loop i=5 Inside loop i=4 Inside loop i=3 Inside loop i=2 Inside loop i=1 PL/SQL procedure successfully completed. SQL> begin 2 dbms_output.put_line('hello'); 3 dbms_output.put_line('there'); 4 end; 5 / hello there PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 begin 2 dbms_output.put('hello'); 3 dbms_output.put_line('there'); 4* end; SQL> / hellothere PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 begin 2 dbms_output.put('hello'); 3 dbms_output.put('there'); 4* end; SQL> / PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 begin 2 dbms_output.put('hello'); 3 dbms_output.put('there'); 4 dbms_output.new_line; 5* end; SQL> / hellothere PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 begin 2 dbms_output.put('hello'); 3 dbms_output.put('there'); 4 dbms_output.new_put_line; 5* end; SQL> / dbms_output.new_put_line; * ERROR at line 4: ORA-06550: line 4, column 14: PLS-00302: component 'NEW_PUT_LINE' must be declared ORA-06550: line 4, column 2: PL/SQL: Statement ignored SQL> ed Wrote file afiedt.buf 1 begin 2 dbms_output.put('hello'); 3 dbms_output.put('there'); 4 dbms_output.new_put_line(' '); 5* end; SQL> / dbms_output.new_put_line(' '); * ERROR at line 4: ORA-06550: line 4, column 14: PLS-00302: component 'NEW_PUT_LINE' must be declared ORA-06550: line 4, column 2: PL/SQL: Statement ignored SQL> ed Wrote file afiedt.buf 1 begin 2 dbms_output.put('hello'); 3 dbms_output.put('there'); 4 dbms_output.put_line(' '); 5* end; SQL> / hellothere PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 begin 2 for row_index in 1 ..4 3 loop 4 for col_index in 1 ..row_index 5 loop 6 dbms_output.put('*'); 7 end loop; 8 dbms_output.new_line; 9 end loop; 10* end; 11 / * ** *** **** PL/SQL procedure successfully completed. SQL> spool off