How can I code a 'CASE Structure' (Multiway Branch) in NCL.

Document ID : KB000011193
Last Modified Date : 10/04/2018
Show Technical Document Details
Question:

In NCL, there is no designated verb for a 'Case Structure' (also called a Multi-way Branch) which is available for some other languages.

How can I code a 'CASE Structure' (Multiway Branch) in NCL?

Answer:

We can create a 'CASE Structure' sub-routine as shown in the following example:

Assume that a panel is displayed, presenting a list of five options, which can be selected by specifying '1' through to '5'. The User enters their required number choice and presses <ENTER>. The NCL code for processing the Users selection is shown here (assuming the 'User entered' selection is stored in the variable &SELECT)...


&CONTROL NOLABEL
   ... 
   .CASE 
   &PANEL... 
   &GOTO .#&SELECT 
   processing for wrong selection 
   &GOTO .CASE 
   .#1 
   processing of selection 1 
   &GOTO .ENDCASE 
   .#2 
   processing of selection 2 
   &GOTO .ENDCASE 
   .#3 
   processing of selection 3 
   &GOTO .ENDCASE 
   .#4 
   processing of selection 4 
   &GOTO .ENDCASE 
   .#5 
   processing of selection 5 
   &GOTO .ENDCASE 
.ENDCASE

...For every valid value of '&SELECT', there is a Label to which processing 'jumps', via line...

&GOTO .#&SELECT

To follow the rules of 'Structured Programming', the processing jumps to the end of the 'CASE Structure' via Label '.ENDCASE'.

It is necessary to code...

&CONTROL NOLABEL

...prior to the 'CASE Structure' sub-routine, to avoid the NCL abending, due to a wrong selection, causing a 'jump' to a

non-existent Label, i.e. ...

If the User enters a '7', for example, then a Branch to Label '.#7' is attempted, but the Label does not exist. With '&CONTROL NOLABEL' in effect, processing continues after the '&GOTO'. This allows the NCL to react to incorrect or missing entries, at that place in the NCL.

Additional Information:

There is a YouTube video which describes how to do this. See Case Structures in NCL