Pascal - 嵌套 Case 语句
可以将 case 语句 作为外部 case 语句 的语句序列的一部分。 即使内部和外部的case常量包含共同的值,也不会产生冲突。
语法
嵌套 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}
示例
下面的程序说明了这个概念。
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