switch command (C and C++)

The switch command enables you to transfer control to different commands within the switch body, depending on the value of the switch expression. The switch, case, and default keywords must be lowercase and cannot be abbreviated.

Read syntax diagramSkip visual syntax diagramswitch(expression){switch_body};
switch_body
Read syntax diagramSkip visual syntax diagramcase_clausedefault_clausecase_clause
case_clause
Read syntax diagramSkip visual syntax diagramcasecase_expression:command
default_clause
Read syntax diagramSkip visual syntax diagramdefault:command
expression
A valid z/OS® Debugger C expression.
case_expression
A valid character or optionally signed integer constant.
command
A valid z/OS Debugger command.

The value of the switch expression is compared with the value of the expression in each case clause. If a matching value is found, control is passed to the command in the case clause that contains the matching value. If a matching value is not found and a default clause appears anywhere in the switch body, control is passed to the command in the default clause. Otherwise, control is passed to the command following the switch body.

If control passes to a command in the switch body, control does not pass from the switch body until a break command is encountered or the last command in the switch body is performed.

Usage notes

  • Declarations are not allowed within a switch command.
  • The switch command does not end with a semicolon. A semicolon after the closing brace is treated as a Null command.
  • Although this command is similar to the switch statement in C, it is subject to z/OS Debugger restrictions on expressions.
  • Duplicate case_expression values are not supported.
  • You cannot use the switch command while you replay recorded statements by using the PLAYBACK commands.

Examples

  • The following switch command contains several case clauses and one default clause. Each clause contains a function call and a break command. The break commands prevent control from passing down through subsequent commands in the switch body.
    If key has the value '⁄', the switch command calls the function divide. On return, control passes to the command following the switch body.
    char key;
    
    printf("Enter an arithmetic operator\n");
    scanf("%c",&key);
    
    switch (key)
    {
       case '+':
          add();
          LIST (key);
          break;
       case '-':
          subtract();
          LIST (key);
          break;
       case '*':
          multiply();
          LIST (key);
          break;
       case '⁄':
          divide();
          LIST (key);
          break;
       default:
          printf("Invalid key\n");
          break;
    }
  • In the following example, break commands are not present. If the value of c is equal to 'A', all 3 counters are incremented. If the value of c is equal to 'a', lettera and total are increased. Only total is increased if c is not equal to 'A' or 'a'.
    char text[100];
    int  capa, i, lettera, total;
    
    for (i=0; i < sizeof(text); i++) {
    
        switch (text[i]) {
           case 'A':
             capa++;
           case 'a':
             lettera++;
           default:
             total++;
        }
    }