Sunday, 20 May 2012

Explain the different types of control statement with flow chart and syntax.


C language has four types of control statement described below:
1. if statement
The if statement is a powerful decision making statement and is used to control the flow of execution of statements. It is basically a two way decision statement and is used in conjunction with an expression. It takes the following form: if (test condition)
The if statement may be implemented in different forms depending on the complexity of conditions to be tested. The different forms are:
a.     Simple if statement                                       

The general form of simple if statement is
            If (test expression)
             {
                  Statement block;
   }
            Statement x;
The ‘statement block’ may be a single statement or a group of statements. If the test expression is true, the statement-block will be executed; otherwise the statement block will be skipped and the execution will jump to the statement-x.
b.     if…else statement
The if…else statement is an extension of the simple if statement. The general form is
if (test expression)
    {
True block statement(s)
    }
else
     {
            False block statement(s)
    }
Statement-x
If the test expression is true, then the true-block statement(s), immediately following the if statements are executed; otherwise, the false-block statement(s) are executed. In either case, either true-block or false-block will be executed, not both.
c.      nested if…else statement
When a series of decisions are involved, we may have to use more than one if…else statement in nested form as shown bellow:
if (test condition-1)
  {
     if (test condition-2)
       {
           Statement-1;
         }
      else
       {
          Statement-2;
        }
     }
else
   {
        Statement-3;
    }
Statement-x;
The logic of execution is illustrated. If the condition-1 is false, the statement-3 will be executed; otherwise it continues to perform the second test. If the condition-2 is true, the statement-1 will be evaluated; otherwise the statement-2 will be evaluated and then the control is transferred to the statement-x.
d.     else if ladder
There is another way of putting ifs together when multipath decisions are involved. A multipath decision is a chain of ifs in which the statement associated with each else is an if. It takes the following general form:
if (condition 1)
  statement 1;
     else if (condition 2)
         statement 2;
              else if (condition 3)
                   statement 3;
                        else
                              default-statement;
statement – x;
This construct is known as the else if ladder. The conditions are evaluated from the top (of the ladder), downwards. As soon as a true condition is found, the statement is associated with it is executed and the control is transferred to the statement – x (skipping the rest of the ladder). When all the n conditions become false, then the final else containing the default statement will be executed.
2. The switch statement
C has a built in multiway decision statement known as a switch. The switch statement teststhe value of a given expression against a list of case values and when a match is found, a block of statements associated with that case is executed. The general form of switch statement is as shown bellow:
switch (exepression)
{
   case value-1;
              block 1
              break;
  case value-2;
              block-2
              break;
  ……
  …....
default:
              default-block
              break;
}
statement - x;
The expression is an integer expression or characters, Value-1, Value-2…..are constants and constant expressions and are known as case labels. Each of these values is unique within a switch statement. block-1,block-2….are statement lists and may contain zero or more statements. There is no need to put braces around these blocks. Note that case labels end with a colon (:).
The break statement at the end of each block signals the end of a particular case and cause an exit from the switch statement, transferring the control to the statement-x following the switch.
The default is an optional case. When present, it will be executed if the value of the expression does not match with any of the case values. If not present, no action takes place if all matches fail and the control goes to the statement-x.
3. Conditional operator statement
Conditional operator is a combination of ? and :, and takes three operands. It is useful for making two-way decisions. The general form of use of the conditional operator is as follows:
conditional expression ? expression1 : expression2
The conditional expression is evaluated first. If the result is nonzero, expression1 is evaluated and is returned as the value of the conditional expression. Otherwise expression2 is evaluated and its value is returned.
4. The Goto statement
C supports the goto statement to branch unconditionally from one point to another in the program. The goto requires a label in order to identify the place where where the branch is to be made. A label is any valid variable name, and must be followed by a colon. The label is placed immediately before the statement where the control is to be transferred. The general form of goto and  label statements are shown below:
goto labe;                  label:
………                      statement;
………                      ………….
………                      ………….
label:                         ………….
Statement;                 goto label;
The label: can be anywhere in the program either before or after the goto label; statement

No comments:

Post a Comment