SQL> --loops SQL> set serveroutput on SQL> --write an anonymous block to prýnt numbers from 1 to 5 onscreen SQL> --use a whýle looð SQL> ed SP2-0107: Nothing to save. SQL> declare 2 i number; 3 begin 4 i :=1; 5 6 7 scö 8 ed 9 ed 10 11 / ed * ERROR at line 8: ORA-06550: line 8, column 1: PLS-00103: Encountered the symbol "ED" when expecting one of the following: := . ( @ % ; SQL> declare 2 i number; 3 begin 4 i :=1; 5 6 7 scö 8 ed 9 ed 10 11 d 12 13 14 15 . 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; 11 / 1 2 3 4 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(i); 7 i:=i+1; 8 end loop; 9* end; 10 / 1 2 3 4 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(i); 7 i:=i+1; 8 exit; 9 end loop; 10* end; SQL> / 1 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(i); 7 i:=i+1; 8 if i>2 then 9 exit; 10 end if; 11 end loop; 12* end; 13 / 1 2 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 if i>2 then 7 exit; 8 end if; 9 dbms_output.put_line(i); 10 if i>2 then 11 exit; 12 end if; 13 i:=i+1; 14 if i>2 then 15 exit; 16 end if; 17 end loop; 18* end; 19 / 1 2 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(i); 7 i:=i+1; 8 end loop; 9* end; 10 / 1 2 3 4 5 PL/SQL procedure successfully completed. SQL> --use a basic loop to print numbers 1 to 5 SQL> ed Wrote file afiedt.buf 1 declare 2 loop_index number; 3 begin 4 loop_index :=1; 5 loop 6 dbms_output.put_line(loop_index); 7 loop_index:=loop_index+1; 8 exit when loop_index=5; 9 end loop; 10* end; 11 / 1 2 3 4 PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 declare 2 loop_index number; 3 begin 4 loop_index :=1; 5 loop 6 dbms_output.put_line(loop_index); 7 loop_index:=loop_index+1; 8 exit when loop_index=6; 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 loop_index number; 3 begin 4 loop_index :=1; 5 loop 6 dbms_output.put_line(loop_index); 7 exit when loop_index=5; 8 loop_index:=loop_index+1; 9 end loop; 10* end; 11 / 1 2 3 4 5 PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 declare 2 loop_index number; 3 begin 4 loop_index :=1; 5 loop 6 dbms_output.put_line(loop_index); 7 loop_index:=loop_index+1; 8 exit when loop_index>5; 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 loop_index number; 3 begin 4 loop_index :=1; 5 loop 6 exit when loop_index>5; 7 dbms_output.put_line(loop_index); 8 loop_index:=loop_index+1; 9 end loop; 10* end; 11 / 1 2 3 4 5 PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 declare 2 loop_index number; 3 begin 4 loop_index :=1; 5 loop 6 exit when loop_index>5; 7 dbms_output.put_line(loop_index); 8 exit when loop_index>5; 9 loop_index:=loop_index+1; 10 exit when loop_index>5; 11 end loop; 12* end; SQL> / 1 2 3 4 5 PL/SQL procedure successfully completed. SQL> --print even numbers from 0 to 10 using basic loop SQL> ed Wrote file afiedt.buf 1 declare 2 loop_index number; 3 begin 4 loop_index :=0; 5 loop 6 if mod(loop_index,2) = 0 then 7 dbms_output.put_line(loop_index); 8 end if; 9 loop_index:=loop_index+1; 10 exit when loop_index>5; 11 end loop; 12* end; 13 / 0 2 4 PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 declare 2 loop_index number; 3 begin 4 loop_index :=0; 5 loop 6 if mod(loop_index,2) = 0 then 7 dbms_output.put_line(loop_index); 8 end if; 9 loop_index:=loop_index+1; 10 exit when loop_index>10; 11 end loop; 12* end; SQL> / 0 2 4 6 8 10 PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 declare 2 loop_index number; 3 begin 4 loop_index :=0; 5 while loop_index<=10 6 loop 7 if mod(loop_index,2) = 0 then 8 dbms_output.put_line(loop_index); 9 end if; 10 loop_index:=loop_index+1; 11 end loop; 12* end; 13 / 0 2 4 6 8 10 PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 declare 2 loop_index number; 3 begin 4 loop_index :=0; 5 while loop_index<=10 6 loop 7 dbms_output.put_line(loop_index); 8 loop_index:=loop_index+2; 9 end loop; 10* end; SQL> / 0 2 4 6 8 10 PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 declare 2 loop_index number :=100; 3 begin 4 dbms_output.put_line('Before loop value='||loop_index); 5 for loop_index in 1 ..5 6 loop 7 dbms_output.put_line('Inside loop value='||loop_index); 8 loop_index := loop_index+1; 9 end loop; 10* end; 11 / loop_index := loop_index+1; * ERROR at line 8: ORA-06550: line 8, column 7: PLS-00363: expression 'LOOP_INDEX' cannot be used as an assignment target ORA-06550: line 8, column 7: PL/SQL: Statement ignored SQL> -- we cannot modify the loop index SQL> ed Wrote file afiedt.buf 1 declare 2 loop_index number :=100; 3 begin 4 dbms_output.put_line('Before loop value='||loop_index); 5 for loop_index in 1 ..5 6 loop 7 dbms_output.put_line('Inside loop value='||loop_index); 8 end loop; 9* end; 10 / Before loop value=100 Inside loop value=1 Inside loop value=2 Inside loop value=3 Inside loop value=4 Inside loop value=5 PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 declare 2 loop_index number :=100; 3 begin 4 dbms_output.put_line('Before loop value='||loop_index); 5 for loop_index in 1 ..5 6 loop 7 dbms_output.put_line('Inside loop value='||loop_index); 8 end loop; 9 dbms_output.put_line('After loop value='||loop_index); 10* end; 11 / Before loop value=100 Inside loop value=1 Inside loop value=2 Inside loop value=3 Inside loop value=4 Inside loop value=5 After loop value=100 PL/SQL procedure successfully completed. SQL> --for loop does not use YOUR variable SQL> ed Wrote file afiedt.buf 1 begin 2 for loop_index in 1 ..5 3 loop 4 dbms_output.put_line('Inside loop value='||loop_index); 5 end loop; 6* end; SQL> / Inside loop value=1 Inside loop value=2 Inside loop value=3 Inside loop value=4 Inside loop value=5 PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 begin 2 for loop_index in 5 ..1 3 loop 4 dbms_output.put_line('Inside loop value='||loop_index); 5 end loop; 6* end; SQL> / PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 begin 2 for loop_index in reverse 1 ..5 3 loop 4 dbms_output.put_line('Inside loop value='||loop_index); 5 end loop; 6* end; SQL> / Inside loop value=5 Inside loop value=4 Inside loop value=3 Inside loop value=2 Inside loop value=1 PL/SQL procedure successfully completed. SQL> ed Wrote file afiedt.buf 1 begin 2 dbms_output.put_line('hello '); 3 dbms_output.put_line('there'); 4* end; SQL> / 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> / hello there 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> / hello there PL/SQL procedure successfully completed. SQL> spool off