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.
- 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 aNull
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 thePLAYBACK
commands.
Examples
- The following
switch
command contains severalcase
clauses and onedefault
clause. Each clause contains a function call and abreak
command. Thebreak
commands prevent control from passing down through subsequent commands in theswitch
body.Ifkey
has the value'⁄'
, theswitch
command calls the functiondivide
. On return, control passes to the command following theswitch
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 ofc
is equal to 'A', all 3 counters are incremented. If the value ofc
is equal to 'a',lettera
andtotal
are increased. Onlytotal
is increased ifc
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++; } }