4.循环控制语句和go语句以及 null值的使用

      循环控制语句的话,我理解的是 有三个基本的循环:loop,while,for 三大循环

      简单的loop 循环:这是最基本的循环种类,包含loop-end loop 语句和一些exit退出语句。
      数据式for循环:这种循环结构允许指定循环要执行的次数,当指定的次数满足时才退出循环
      while 循环:仅当特定的循环满足时才执行循环,当条件不在满足时循环终止。exit语句也分为如下2种类型。
            exit 语句:直接退出循环。
            exit when语句:当when 指定的条件满足时退出循环。

1. 简单的 loop 循环
declare
   v_count number(2) :=0; -- 定义循环计数变量
begin
   loop                  -- 开始执行循环
      v_count :=v_count+1;--循环计数器加1
      -- 打印字符信息
      dbms_output.put_line(''||v_count||':hello pl/sql!');
      --如果计数条件为10,则退出循环
      if v_count=10
      then
         exit;
      end if;
    end loop;
    -- 循环退出后,将执行这条语句
    dbms_output.put_line('循环已经退出了!');
end; 
2.loop和exit when 的用法
declare
   v_count number(2) :=0;  --定义循环计数变量
begin
   loop                      -- 开始执行循环
      v_count :=v_count+1;   -- 循环计数器加1
      --打印字符信息
      dbms_output.put_line(''||v_count||':hello pl/sql');
      --如果计数器条件为10, 则退出循环
      exIt when v_count=10;
  end loop;
  -- 循环退出后,将执行这条语句
    dbms_output.put_line('训话已经退出了!');
end;
3.continue 重新开启循环
declare
   x number :=0;     -- 定义循环计数器变量
begin
   loop              -- 开始循环,当遇到continue 语句时,将重新开始loop的执行
      dbms_output.put_line('内部循环值:x='||to_char(x));
      x:=x+1;
      if x<3;
      then              --如果计数器小于3,则重新开始执行循环
      
         continue;
     end if;
     -- 当循环计数大于3时执行的代码
     dbms_output.put_line('constinue 之后的值:x='||to_char(x));
     
      exit when x=5;  --当循环计数为5时,退出循环
   end loop;
   --输出循环的结束值
   dbms_output.put_line('循环体结束后的值:x='||to_char(x));
end;   
4. continue when 重新开始循环
declare
   x number :=0;
begin
   loop           -- 开始循环,当遇到continue 语句时,将重新开始loop的执行
      dbms_output.put_line('内部循环值:x='||to_char(x));
      x:=x+1;
      continue when x<3;
      -- 当循环计数大于3时执行的代码
      dbms_output.put_line('continue 之后的值:'x=||to_char(X));
      exit when x=5;   --当循环计数为5时,退出循环
   end loop;
   -- 输出循环的结束值
   dbms_output.put_line('循环体结束后的值:x='||to_char(x));、
end;
5.简单while循环
declare
   v_count pls_integer :=1; -- 循环计数器值
begin
   while v_count <=5       -- 循环计数器小于等于5
   loop
     -- 循环计数索引的输出
     dbms_output.put_line('while 循环索引值:'||v_count);
     v_counr :=v_count+1; --变更索引值以免死循环
   end loop;
end;
6.简单for 循环
declare 
   v_total integer :=0; --循环累计汇总数字
begin
   for i in 1 .. 3   --使用 for 循环开始循环计数
   loop
      v_total :=v_total+1 ; --汇总列加
      dbms_output.put_line('循环计数器值:'||i);
   end loop;
   -- 输出循环结果值
   dbms_output.put_line('循环总计:'||v_total);
end; 
7.reverse在for 循环中的使用
declare
   v_total integer :=0; -- 循环累计汇总数字
begin
   for i in reverse 1 .. 3 -- 使用reverse 从高到低进行循环
   loop
      v_total :=v_tota+1;     --汇总累加
      dbms_output.put_line('循环计数器值:'i);
   end loop;
   -- 输出循环结果值
   dbms_output.put_line('循环总计:'||v_total);
end;
8.动态指定循环边界值
declare
   v_counter integer :=&counter;  -- 动态指定上限边界值变量
begin
   for i in 1 .. v_counter   -- 在循环中使用变量定义边界
   loop
      dbms_output.put_line('循环计数:'||i);
   end loop;
end;
9.go 语句
declare
   p varchar2(30);      -- 定义输出字符串变量
   n pls_integer :=37;  -- 定义要判断的数字
begin
   for j in 2 .. round(sqrt(n)) loop  -- 外层循环
      if n mod j=0 then               -- 判断是否为一个素数
         p :='不是素数'-- 初始化p的值
       goto print_now;                -- 跳转到print_now 标签位置
     end if;
   end loop;
   p:='不是一个素数';
   -- 跳转到标签位置
   <<print_now>>
   dbms_output.put_line(to_char(n)||p);
end;
    
10. go 语句模拟循环语句
declare
   v_counter int :=0; --定义循环计数器变量’
begin
   <<outer>>        --定义标签
   v_counter :=v_counter +1;
   dbms_output.put_line('循环计数器:'||v_counter);
   -- 判断计数器条件
   if v_counter <5
   then
      goto outer;     -- 向上跳转到标签位置
   end if;
end;   
11.null语句使用示例
declare
   v_counter int :=&counter;   -- 允许用户输入变量值
begin
   if c_counter >5;            -- 如果变量值大于5
   then
      dbms_output.put_line('v_counter>5');  --输出信息
   else                                     -- 否则
      null;                                 -- 仅占位符,不做任何事情
   end if;
end;   
12.null与 标签使用
declare
   done boolean;
begin
   for i in 1 .. 50;
   loop
      if done
      
      
   then
   goto end_loop;
 end if;
 -- 标签定义
<<end_loop>>
null; -- 使用null什么也不做
end loop;
end;
13.异常语句中的 null
declare
   v_result in :=0;    -- 保存结果值的变量
begin
   v_result :16/0;    --故意被0除
   dbms_output.put_line('现在时间是:'
                    || to_char(sysdate,'yyyy-mm-dd hh24:mi:ss');
exception             -- 异常处理语句块
   when   others
   then
      null;   --当触发任何异常时,什么也不做
end;
原文地址:https://www.cnblogs.com/etllearn/p/15043088.html