OraclePL/Sql编程——流程控制语句

一、选择语句

  1. if…then 语句
    if…then 语句是选择语句中最简单的一种形式
if <condition_expression> then -- condition_expression是条件表达式,为true时执行if下面的语句
plsql_sentence --当 condition_expression为true时执行
end if;

eg:定义两个字符串变量,然后赋值,接着用if…then语句比较两个字符串的长度

set serveroutput on
DECLARE
var_name1 varchar2(20);
var_name2 varchar(30);
begin
var_name1 := 'aielsa';
var_name2 := 'susan';
if length(var_name1) < length(var_name2) then
dnms_output.put_line('字符串'||var_name1||'比字符串'||var_name2||'的长度小');
end if;
end;
  1. if…then…else 语句
if <condition_expression> then -- condition_expression是条件表达式,为true时执行if下面的语句
plsql_sentence --当 condition_expression为true时执行
else
plsql_sentence --当 condition_expression为false时执行
end if;

eg:通过if…then…else判断年龄只有大于等于56岁的人才能申请退休

set serveroutput on
DECLARE
age int ;
begin
if age >= 56 then
dnms_output.put_line('可以申请退休');
else
dnms_output.put_line('不可以申请退休');
end if;
end;
  1. if … then … elsif 语句
if <condition_expression> then -- condition_expression是条件表达式,为true时执行if下面的语句
plsql_sentence --当 condition_expression为true时执行
elsif  <condition_expression2> then
plsql_sentence --当 condition_expression2为true时执行
end if;
 

eg:指定一个月份数值,然后用if…then…elsif确定它属于的季节

set  serveroutput on
DECLARE
month int := 10;
begin
if month >= 0 and month <= 3 then
dnms_output.put_line('这是春季');
elsif month >= 4 and month <= 6 then
dnms_output.put_line('这个是夏季');
elsif month >= 7 and month < = 9 then
dnms_output.put_line('这个是秋季');
elsif month >= 10 and month <= 12 then
dnms_output.put_line('这个是冬季');
else
dnms_output.put_line('对不起,月份不合法');
end if;
end;
  1. case 语句
    Orcale 9i以后PL/SQL也像其他编程语言一样使用case语句,在case关键字后有个选择器,它通常是一个变量,程序就是从这个选择器开始执行,接下来是when语句。
case<selector>
when <express_1>  then plsql_sentence1;
when <express_2>  then plsql_sentence2;
.....
when <express_n>  then plsql_sentencen;
[else plsql_sentence;]
end case;

eg:指定一个季度值,然后用case语句判断所包含的月份

set  serveroutput on
DECLARE
season int := 3;
seasoninfo varchar2(30);
begin
case<season >
when 1  then 
seasoninfo := season||'季度包括1 2 3月';
when 2  then 
seasoninfo := season||'季度包括4 5 6月';
when 3 then
seasoninfo := season||'季度包括7 8 9 月';
when 4 then 
seasoninfo := season||'季度包括10  11  12月';
else 
seasoninfo := season||'季度不合法';
end  case;
dnms_output.put_line(seasoninfo );
end ;

循环语句

  1. loop 语句
    loop语句会先执行一次循环体,然后在判断exit when 后面的条件是false还是true,如果是true则退出循环体,false就再次执行循环体,这样保证了循环体至少执行一次。
loop
plsql_sentence;
exit when end_condition_exp
end loop;

eg: 使用loop 语句求得前100个自然数的和

set  serveroutput on
DECLARE
sum_i int := 0;
i int := 0;
begin
loop
 i := i+1 ;
sum_i := sum_i + i;
exit when i > 100
end loop;
dnms_output.put_line('前100个自然数的和是'||sum_i);
end;
  1. while 语句
    while 语句根据它的条件表达式执行零次或者多次
while condition_expression loop
plsql_sentence;
end loop;

eg : 使用while语句求前100个自然数之和

set  serveroutput on
DECLARE
sum_i int := 0;
i int := 0;
begin
while  i < 100
sum_i := sum_i + i;
 i := i+1 ;
end loop;
dnms_output.put_line('前100个自然数的和是'||sum_i);
end;
  1. for 语句
    for 语句是一个可预置循环次数的循环语句
-- 当循环中使用reverse关键字时 计数器的值会随循环递减
for variable_counter_name in [reverse] lower_limit..upeer_limit loop
plsql_sentence;
end loop;

eg :使用 for 语句求前100个自然数偶数和的值

set  serveroutput on
DECLARE
sum_i int := 0;
begin
for  i in reverse 1..100 loop
if mod(i,2) = 0 then
sum_i := sum_i +i ;
end if;
end loop;
dnms_output.put_line('前100个自然数偶数的和是'||sum_i);
end;
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值