Loop structure:
1, Loop loop structure
Grammar:
LOOP
The statement to execute;
Exit when < conditions >--Exits the loop if the condition is met
END LOOP;
Example: An integer that loops out 1-10
DECLARE Number: = 1; BEGIN LOOP dbms_output.put_line (v_num); V_num:=+1; EXIT when Ten ; END LOOP; END;
The drawback of loop structure: The structure is not clear enough, exit easy to write.
2. While loop structure
Grammar:
While < Boolean expressions > LOOP
The statement to execute;
END LOOP;
Example: Displaying an integer of 1-10;
DECLARE Number: = 1; BEGIN while <= Ten LOOP Dbms_output.put_line (v_num); V_num:=+1; END LOOP; END;
3. For loop
Grammar:
For loop counter in [REVERSE] offline ... Upper LOOP
The statement to execute;
END LOOP;
Example: Displaying an integer of 1-10
DECLARE number; BEGIN for inch 1.. Ten LOOP Dbms_output. Put_Line (v_num); END LOOP; END;
In the For loop, once per loop, the loop variable is automatically +1, using the keyword reverse, the loop variable automatically-1, the number following in reverse must be small to large, and must be an integer, cannot be a variable or an expression, you can use Exit to exit the loop.
In the previous example, if you add reverse, the output is descending.
Summarize:
The loop structure can perform certain operations repeatedly, taking precedence over a for loop in the known number of cycles, and using a while loop for unknown times. Exit can be nested in any loop, terminating the loop directly. Loop is not used in general!
Pl-sql Programming Basics (3)