Wednesday, December 15, 2010

To create a switch in C statement

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

Instructions

    Create a switch statement
  1. 1

    Select the value that determines an action in your switch statement.

It can be only one variable, the result of invoking the function or any expression.
  • 2

    Set this value or an expression after the switch as this statement:

    Switch {(Variable)}

  • 3

    Make 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!
  • 4

    It is very common for the values with the # defined constants statement Précompilateur C. define

  • 5

    Include a statement in the box for each value in the following format:

    Value of the case:

  • 6

    Follow each statement box with one or more actions.

  • In contrast to most of the C structures you need braces for further instructions.
  • 7

    Type 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."
  • 8

    Create 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. }

    No comments:

    Post a Comment