ORACLE PL/SQL Instance sixth iteration control one of the fine solutions

Source: Internet
Author: User
Tags reserved terminates

6.1 Simple Loops

A simple loop, just like its name, is one of the most basic loops. Simple loops have the following structure

LOOP

STATEMENT 1;

STATEMENT 2;

...

STATEMENT N;

END LOOP;

A reserved word loop identifies the beginning of a simple loop. STATEMENT1 to Statement N is a sequence of statements that are executed repeatedly. These statements consist of one or more standard programming constructs. End loop is a reserved word that identifies the end of the loop structure

At each iteration of the loop, some column statements are executed, and then the first statement of the loop is executed again. The preceding statement sequence is executed without restriction because no statement specifies when the loop terminates. Therefore, a simple loop is called an infinite loop because the loop cannot be exited, and the loop that is constructed correctly requires an exit condition that determines the condition of the loop termination. There are two forms of exit conditions: Exit and exits when

1. Exit statement

Using the exit clause, the loop terminates when the exit condition is true. The IF statement is used when the exit condition is evaluated. When the exit statement evaluates to TRUE, execution goes to the first executable statement after the end Loop statement

LOOP

STATEMENT 1;

STATEMENT 2;

IF Condidtion Then

EXIT;

END LOOP;

STATEMENT 3;

2. EXIT when statement

The loop is terminated only if the exit when statement condition evaluates to True. The execution then goes to the first executable statement after the end LOOP statement.

Loop structure of EXIT when clause

LOOP

STATEMENT 1;

STATEMENT 2;

EXIT when condidtion;

END LOOP;

STATEMENT 3;

6.1.2 using a simple loop with exit when condition

6.2 While Loop

While loop structure:

While CONDITION LOOP

STATEMENT 1;

STATEMENT 2;

......

STATEMENT N;

END LOOP;

The reserved word while identifies the start of the looping structure. Condition is the test condition of this lecture, and the result is true or false. The test result determines whether the loop is executed. Statement 1~n is a sequence of statements that are executed repeatedly. End loop is a reserved word that identifies the end of the loop structure

Understanding: It is first to judge Condidtion if it is true to meet on the loop, does not meet the end loop directly executes the next statement

Terminate cycle prematurely

In the while loop body, you can use the exit and exit when statements. If the exit condition is true before the test condition is false, the loop is terminated prematurely. If the test condition is true before the exit condition is true, the measurement does not count as an early termination of the loop.

While Test_condidtion LOOP

STATEMENT 1;

STATEMENT 2;

IF Exit_condition Then

EXIT;

END IF;

END LOOP;

STATEMENT 3;

Or

While Test_condidtion LOOP

STATEMENT 1;

STATEMENT 2;

EXIT when exit_condition;

END LOOP;

STATEMENT 3

6.3 Numeric for-loop

For loop_counter in [REVERSE] Lower_limit....upper_limit loop

STATEMENT 1;

STATEMENT 2;

.....

STATEMENT N;

END LOOP

1. Use the reverse option in the loop

Earlier in this section, when the computer loops the value of a counter, you can use two options: in and reverse. The use of in options in loops has been discussed earlier. The following example shows how to use the In reverse option in a loop

  

ORACLE PL/SQL Instance sixth iteration control one of the fine solutions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.