Wednesday, January 9, 2013


Control Statements

A program consists of a number of statements which are usually executed in sequence. Programs can be much more powerful if we can control the order in which statements are run.
Statements fall into three general types;
  • Assignment, where values, usually the results of calculations, are stored in variables.
  • Input / Output, data is read in or printed out.
  • Control, the program makes a decision about what to do next.
This section will discuss the use of control statements in C. We will show how they can be used to write powerful programs by;
  • Repeating important sections of the program.
  • Selecting between optional sections of a program.
  • The if else Statement:

    This is used to decide whether to do something at a special point, or to decide between two courses of action.
    The following test decides whether a student has passed an exam with a pass mark of 45
    
    if (result >= 45)
            printf("Pass\n");
    else
            printf("Fail\n");
    It is possible to use the if part without the else.
    
    if (temperature < 0)
            print("Frozen\n");
    Each version consists of a test, (this is the bracketed statement following the if). If the test is true then the next statement is obeyed. If is is false then the statement following the else is obeyed if present. After this, the rest of the program continues as normal.If we wish to have more than one statement following the if or the else, they should be grouped together between curly brackets. Such a grouping is called a compound statement or a block.
    
    if (result >= 45)
    {       printf("Passed\n");
            printf("Congratulations\n")
    }
    else
    {       printf("Failed\n");
            printf("Good luck in the resits\n");
    }
    Sometimes we wish to make a multi-way decision based on several conditions. The most general way of doing this is by using the else if variant on the if statement. This works by cascading several comparisons. As soon as one of these gives a true result, the following statement or block is executed, and no further comparisons are performed. In the following example we are awarding grades depending on the exam result.
    
    if (result >= 75)
            printf("Passed: Grade A\n");
    else if (result >= 60)
            printf("Passed: Grade B\n");
    else if (result >= 45)
            printf("Passed: Grade C\n");
    else
            printf("Failed\n");
    In this example, all comparisons test a single variable called 
    result. In other cases, each test may involve a different variable or some combination of tests. The same pattern can be used with more or fewer else if's, and the final lone else may be left out. It is up to the programmer to devise the correct structure for each programming problem.
  • The switch Statement

    This is another form of the multi way decision. It is well structured, but can only be used in certain cases where;
    • Only one variable is tested, all branches must depend on the value of that variable. The variable must be an integral type. (int, long, short or char).
    • Each possible value of the variable can control a single branch. A final, catch all, default branch may optionally be used to trap all unspecified cases.
    Hopefully an example will clarify things. This is a function which converts an integer into a vague description. It is useful where we are only concerned in measuring a quantity when it is quite small.
    
    estimate(number)
    int number;
    /* Estimate a number as none, one, two, several, many */
    {       switch(number) {
            case 0 :
                    printf("None\n");
                    break;
            case 1 :
                    printf("One\n");
                    break;
            case 2 :
                    printf("Two\n");
                    break;
            case 3 :
            case 4 :
            case 5 :
                    printf("Several\n");
                    break;
            default :
                    printf("Many\n");
                    break;
            }
    }
    Each interesting case is listed with a corresponding action. The break statement prevents any further statements from being executed by leaving the switch. Since case 3 and case 4 have no following break, they continue on allowing the same action for several values of number.Both if and switch constructs allow the programmer to make a selection from a number of possible actions.
    The other main type of control statement is the loop. Loops allow a statement, or block of statements, to be repeated. Computers are very good at repeating simple tasks many times, the loop is C's way of achieving this.

0 comments:

Post a Comment

Powered by Blogger.