Download - All IT eBooks
Transcript
The output now looks like what we would expect:
0
Numeral
1
Numeral
2
Numeral
3
Numeral
1
Numeral
3
Numeral
2
Numeral
.
0
1
2
3
1
3
2
One should always use break statements in a switch construct unless there is a very
good and compelling reason not to do so. The default statement is also a good idea.
In some coding style requirements, such as those employed in the aerospace industry,
a default statement is always required, even if it does nothing. One use for a switch
construct with a default statement is as an input verifier. Suppose a function expects
a specific set of input parameter values, and anything else is an error that must be
trapped and handled. Here’s a simple example code snippet:
switch(inval) {
case 0:
case 1:
case 2:
case 3:
rc = OK;
break;
default:
rc = BAD_VAL;
}
This will set the variable rc (i.e., the return code) to whatever the macro OK is defined
to be if the input value (inval) is between 0 and 3, inclusive. If it is anything else, rc is
set to BAD_VAL. It is assumed that rc will be checked further down in the code to handle
the invalid input condition. Although one could have written an equivalent bit of code
using an if and comparison operators, like so:
if ((inval >= 0) && (inval <= 3)) {
rc = OK;
else
rc = BAD_VAL;
the switch method also handles noncontiguous sequences of values quite nicely without
resorting to long and complex combinations of logical and comparison operators.
Developing Software in C | 147
Related documents
Télécharger le mode d`emploi 2015
Model HR-3P Projector Controller
TECHNISCHE UNIVERSITEIT EINDHOVEN Department of
A. LabVIEW
XLOGO User Manual
MEN Mikro F50C Manual
RabbitNet Peripheral Cards
User Manual
PC Oscilloscope USB based - Introduction
LabVIEW Graphical Programming Course
Bachelorarbeit von Dirk Meier-Eickhoff
Evorobot 1.1 User Manual