Pascal - Nested Case Statements
- Details
- Category: Chapter 2
- Published: Sunday, 14 April 2013 14:27
- Written by Sternas Stefanos
- Hits: 22469
It is possible to have a case statement as part of the statement sequence of an outer case statement. Even if the case constants of the inner and outer case contain common values, no conflicts will arise.
1. Syntax:
The syntax for a nested case statement is as follows:
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}
2. Example:
The following program illustrates the concept.
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.
When the above code is compiled and executed, it produces following result:
This is part of outer switch This is part of inner switch Exact value of a is: 100 Exact value of b is: 200