複製鏈接
請複製以下鏈接發送給好友

CASE

(函數釋義)

鎖定
CASE,是一種函數釋義,是實現選擇結構程序設計的一種語句。一般形式是case <表達式>of。
外文名
CASE
解    釋
實現選擇結構程序設計的一種語句

目錄

CASEpascal

pascal 函數
它的使用有時比IF語句來得簡單直觀。
CASE語句的一般形式是
case <表達式>of
<值表1>:<語句1>;
<值表2>:<語句2>;
……
<值表N>:<語句N>;
End;
在語句頭上的表達式必須是有序類型(整型、字符型、布爾型..)。值表是一些由逗號分開的常數,表達式所有可能的值必須在值表中出現,且每個值只能出現一次。
根據不同情況執行不同語句,例:
var a:integer;
begin
read(a);
case a of
1:writeln('a');
2:writeln('b');
3:writeln('c');
4:writeln('d');
else writeln('e');
end;
end.
這個的作用相當於:
var a:integer;
begin
read(a);
if a=1 then writeln('a')
else if a=2 then writeln('b')
else if a=3 then writeln('c')
else if a=4 then writeln('d')
else writeln('e');
end;
end.
當然,CASE中也可以插入語句塊:
case a of
1:begin
writeln('a');
...
...
end;
2:writeln('b');
3:writeln('c');
4:writeln('d');
else writeln('e');
end;
如果情況常量表的值在某範圍內是連續的,可將常量表寫成
:<語句>;
Case a of
1..4:writeln('a');
5..7:writeln('s');
End;

CASEoracle

示例:case exp
when exp1 then ...A
when exp2 then ...B
when exp3 then ...C
...
else ...D
end
當exp的值為exp1時,則A; exp的值為exp2時,則B,exp的值為exp3是,則C......
exp的值不滿足以上條件是,則D
select empno, ename, sal,
case deptno
when 10 then '財務部'
when 20 then '研發部'
when 30 then '銷售部'
else '未知部門'
end 部門
from emp;