Oracle PL/SQL - What is the output: FOR LOOP Index?

Question

What is the output of the following code?

BEGIN 
  FOR i IN 1..3 LOOP 
    IF i < 3 THEN 
      DBMS_OUTPUT.PUT_LINE (TO_CHAR(i)); 
    ELSE 
      i := 2; 
    END IF; 
  END LOOP; 
END; 
/ 


Click to view the answer

i := 2; 
       * 
ERROR at line 6: 
ORA-06550: line 6, column 8: 
PLS-00363: expression 'I' cannot be used as an assignment target 
ORA-06550: line 6, column 8: 
PL/SQL: Statement ignored

Note

The index of a FOR LOOP statement is implicitly declared as a variable of type PLS_INTEGER that is local to the loop.

The statements in the loop can read the value of the index, but cannot change it.

Statements outside the loop cannot reference the index.

After the FOR LOOP statement runs, the index is undefined.

The FOR LOOP statement tries to change the value of its index, causing an error.

Related Quiz