Question:

C programming switch statements

by  |  earlier

0 LIKES UnLike

C programming switch statements

 Tags:

   Report

3 ANSWERS


  1. In computer programming, a switch statement is a type of control statement that exists in most modern imperative programming languages (e.g., C, C++, C#, and Java). Its purpose is to allow the value of a variable or expression to control the flow of program execution. In some other programming languages, a statement that is syntactically different but conceptually the same as the switch statement is known as a case statement or a select statement.

    The following are simple examples, written in the various languages, that use switch statements to print one of several possible lines, depending on the value of an integer entered by the user. The lack of break keywords to cause fall through of program execution from one block to the next is used extensively. For example, if n=5, the third case statement will produce a match to the control variable. Since there are no statements following this line and no break keyword, execution continues through the 'case 7:' line and to the next line, which produces output. The break line after this causes the switch statement to conclude. If the user types in more than one digit, the default block is executed, producing an error message.

    switch(n) {

      case 0:

        printf("You typed zero.\n");

        break;

      case 1:

      case 9:

        printf("n is a perfect square\n");

        break;  

      case 3:

      case 5:

      case 7:

        printf("n is a prime number\n");

        break;

      case 2:

        printf("n is a prime number\n");

      case 4:

      case 6:

      case 8:

        printf("n is an even number\n");

        break;

      default:

        printf("Only single-digit numbers are allowed\n");

      break;

    }


  2. mainframe computing-http://gotogol.info/

  3. The C switch Statement

    The switch and case statements help control complex conditional and branching operations. The switch statement transfers control to a statement within its body.

    Syntax

    selection-statement:

    switch ( expression ) statement

    labeled-statement:

    case constant-expression : statement

    default : statement

    Control passes to the statement whose case constant-expression matches the value of switch ( expression ). The switch statement can include any number of case instances, but no two case constants within the same switch statement can have the same value. Execution of the statement body begins at the selected statement and proceeds until the end of the body or until a break statement transfers control out of the body.

    Use of the switch statement usually looks something like this:

    switch ( expression )

    {

       declarations

       .

       .

       .

       case constant-expression :

          statements executed if the expression equals the

          value of this constant-expression

          .

          .

          .

          break;

       default :

          statements executed if expression does not equal

          any case constant-expression

    }

    You can use the break statement to end processing of a particular case within the switch statement and to branch to the end of the switch statement. Without break, the program continues to the next case, executing the statements until a break or the end of the statement is reached. In some situations, this continuation may be desirable.

    The default statement is executed if no case constant-expression is equal to the value of switch ( expression ). If the default statement is omitted, and no case match is found, none of the statements in the switch body are executed. There can be at most one default statement. The default statement need not come at the end; it can appear anywhere in the body of the switch statement. A case or default label can only appear inside a switch statement.

    The type of switch expression and case constant-expression must be integral. The value of each case constant-expression must be unique within the statement body.

    The case and default labels of the switch statement body are significant only in the initial test that determines where execution starts in the statement body. Switch statements can be nested. Any static variables are initialized before executing into any switch statements.

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.