Use the C switch statement to a large number of values for a single variable or evaluate an expression.
Switch can much be satisfied, a long if else statements string. Level of difficulty: Moderately easy It can be only one variable, the result of invoking the function or any expression.
2Set this value or an expression after the switch as this statement:
Switch {(Variable)}3Make a list of all the values you want to capture.
Note that it is possible that multiple values can not use the same action but, beaches, only lists of values. For example, can not you have 301 to 400 values so if you want to list all values of 100 one after the other!
4It is very common for the values with the # defined constants statement Précompilateur C. define
5Include a statement in the box for each value in the following format:
Value of the case:
6Follow each statement box with one or more actions.
In contrast to most of the C structures you need braces for further instructions.
7Type each set of commands with a break statement to start the following case.
If you do not, in the following cases are "falls through" which is rarely desirable. It is in the break, if something else Links(c_return) offers (such as an instru) are never "down."
8Create a sp
to capture what case that it is not accompanied by a previous case ezielle standard. It is only by default, not by default.
9{Close with a block}, as in this example:
Switch (Evaluate_color (red, green, blue))
)) {Red case: printf ("a brilliant red sunset fills the sky.")} \n") ; X = 11. Break. ("Case yellow: printf (" sunset beats without mercy ") \n"); X = 14; Break. Green case: X = 10; evaluate_green(); Break. Blue case: printf ("congratulations, you win!") \n ");" Return; Default: Printf ("nothing special happens.") \n ");" Break. }