您的位置:首页 > 其它

[bbk4984]第10集 - Chapter 05- Writing Control Structures(02)

2013-04-23 18:41 471 查看

FOR Loops Rules

Reference the counter only within the loop;it is undefined outside the loop.

Do not reference the counter as the target of an assignment.

Neither loop bound should be NULL.

Use the REVERSE keyword to force the loop to decrement from the upper bound to the lower bound.You must still make sure that the first value in the range specification is less than the second value.Do not reverse the order in which you specify these values when you use the REVERSE keyword.

BEGIN
DBMS_OUTPUT.PUT_LINE('----------Normal----------');

FOR i IN 1..3
LOOP
DBMS_OUTPUT.PUT_LINE('The Outer is ' || i);
END LOOP;

DBMS_OUTPUT.PUT_LINE('----------Reverse----------');

FOR i IN REVERSE 1..3
LOOP
DBMS_OUTPUT.PUT_LINE('The Outer is ' || i);
END LOOP;

END;

/


Suggested Use of Loops

Use the basic loop when the statements inside the loop must execute at least once.

Use the WHILE loop if the condition must be evaluated at the start of each iteration.

Use a FOR loop if the number of iterations is known.

Nested Loops and Labels

You can nest loops to multiple levels.

Use labels to distinguish between blocks and loops.

Exit the outer loop with the EXIT statement that reference the label.

Example

...
BEGIN
<<Outer_loop>>
LOOP
v_counter := v_counter + 1;
EXIT WHEN  v_counter > 10;
<<Inner_loop>>
LOOP
...
-- Leave both loops
EXIT Outer_loop WHEN total_done = 'YES';
-- Leave inner loop only
EXIT WHEN inner_done = 'YES';
...
END LOOP Inner_loop;
...
END LOOP Outer_loop;
END;
/


PL/SQL CONTINUE Statement

CONTINUE:11g的new feature;之前是没有的.

EXIT Clause:

  EXIT;

  EXIT WHEN condition1 THEN ...;

  EXIT label_name WHEN condition1 THEN  ...;


Definition

-Adds the functionality to begin the next loop iteration.

-Provides programmers with the ability to transfer control to the next iteration of a loop

-Use parallel structure and semantics to the NEXT statement.

Benefits

-Eases the programming process

-May provide a small performance improvement over the previous programming workarounds to simulate the CONTINUE statement.

DECLARE
v_total SIMPLE_INTEGER := 0;
BEGIN
FOR i IN 1..10
LOOP
v_total := v_total + i;

DBMS_OUTPUT.PUT_LINE('Total is :' || v_total);

CONTINUE WHEN i > 5;

v_total := v_total + i;

DBMS_OUTPUT.PUT_LINE('Out of Loop Total is :' || v_total);
END LOOP;
END;

/


DECLARE
v_total NUMBER := 0;
BEGIN
<<BeforeTopLoop>>
FOR i IN 1..10 LOOP
v_total := v_total + 1;
dbms_output.put_line('Total is :' || v_total);
FOR j IN 1..10 LOOP
CONTINUE BeforeTopLoop WHEN i + j > 5;
v_total := v_total + 1;
END LOOP;
END LOOP;
END two_loop;
/


BEGIN
<<outer>>
FOR i IN 1..5
LOOP
DBMS_OUTPUT.PUT_LINE('Outer index = ' || TO_CHAR(i));
<<inner>>
FOR j IN 1..5
LOOP
DBMS_OUTPUT.PUT_LINE('--->Inner index = ' || TO_CHAR(j));
CONTINUE outer;
END LOOP inner;
END LOOP outer;
END;

/


CONTINUE;

CONTINUE condition WHEN ...;

CONTINUE label_name WHNE condition;

EXIT Clause 与 CONTINUE Clause加上标签,就相当于GOTO Clause。

The GOTO Statement

The GOTO statement performs unconditional branching to another executable statement in the same execution section of a PL/SQL block.If you use GOTO appropriately and with care,your programs will be stronger for it.

Syntax:

GOTO label_name;


The GOTO Example

BEGIN
GOTO second_output;
DBMS_OUTPUT.PUT_LINE('This line will never execute.');
<<second_output>>
DBMS_OUTPUT.PUT_LINE('We are here!');
END;
/
~


Quiz

There are three types of loops:basic,FOR,and WHILE.

True

False

循环又分为两种:数字的for循环,显示游标的for循环.

Summary

In this lesson,you should have learned to change the logical flow of statements by using the following control structures

PL/SQL 的控制结构分两种,分支判断和循环语句;

分支判断

  |-IF Clause

  |-CASE clause

循环语句

  |-basic loop

  |-while loop

  |-for loop

Conditional(IF statement)

CASE expressions and CASE statements

Loops

- Basic loop

- WHILE loop

- FOR loop

EXIT statement

CONTINUE statement

GOTO statement
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: