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


    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.

    1. True
    2. 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
  • 相关阅读:
    DeFi之道丨科普:一分钟了解以太坊layer2扩容
    4.B-Air302(NB-IOT)-功能扩展-Android扫码绑定Air302,并通过MQTT实现远程控制和监控PLC(三菱Fx1s-10MR)
    Air302(NB-IOT)-硬件BUG-电路硬件错误及其修改说明
    4.2-Air302(NB-IOT)-自建MQTT服务器-Android扫码绑定Air302,并通过MQTT实现远程通信控制
    Android 开发:数据库操作-android 使用 litepal 操作本地数据库
    0.5-Air302(NB-IOT)-连接自建MQTT服务器
    0.1-Air302(NB-IOT)-刷AT指令固件
    STM32+ESP8266+AIR202基本控制篇-00-开发板回收说明
    21-STM32+ESP8266+AIR202/302远程升级方案-扩展应用-移植远程升级包实现STM32F407VET6+串口网络模组(ESP8266/Air202/Air302)使用http或者https远程升级单片机程序
    数据处理思想和程序架构: 单片机STM32F407xx/F405xx/F415xx/417xx系列flash存储方案
  • 原文地址:https://www.cnblogs.com/arcer/p/3038528.html
Copyright © 2020-2023  润新知