嵌套的 case 语句(nested case statements)
优质
小牛编辑
129浏览
2023-12-01
可以将case statement作为外部case statement的语句序列的一部分。 即使内部和外部的case constants包含共同的值,也不会产生冲突。
语法 (Syntax)
嵌套case语句的语法如下 -
case (ch1) of
'A': begin
writeln('This A is part of outer case' );
case(ch2) of
'A': writeln('This A is part of inner case' );
'B': (* case code *)
...
end; {end of inner case}
end; (* end of case 'A' of outer statement *)
'B': (* case code *)
'C': (* case code *)
...
end; {end of outer case}
例子 (Example)
以下程序说明了这个概念。
program checknestedCase;
var
a, b: integer;
begin
a := 100;
b := 200;
case (a) of
100: begin
writeln('This is part of outer statement' );
case (b) of
200: writeln('This is part of inner statement' );
end;
end;
end;
writeln('Exact value of a is : ', a );
writeln('Exact value of b is : ', b );
end.
编译并执行上述代码时,会产生以下结果 -
This is part of outer switch
This is part of inner switch
Exact value of a is: 100
Exact value of b is: 200