4/2/11

In general, Program is a set of or block of statements which will execute one by one (flow of execution) or one after another sequentially.

But in some suituation, need to move one part of the program to another part of the same program either based on some condition or without condition.

In some suituation we may need to execute either a single or a set of statements repeatedly either for specified number of times or until a given condition is satisfied.

So, In such suituation we need to control the flow of execution. for that we need control statements, which will control the flow of execution.


Types of Control Statements: We can classify control statement in two way:
1. Conditional Control statements
2. Unconditional Control statements 

 
1. Conditional Control Statement
In conditional control statements, flow of execution will be based on the given condition. i.e. by checking the condition, it will decide which part of the program have to execute.

The following are Conditional Control statements in C:
1. If statement
2. Switch Statement
3. Iterations / Loops:

IF Statement:
The IF statement is used to control the flow of execution based on some condition.

Types of IF statements:

1. Simple if statement
2. if else statement
3. if else if ladder statement
4. nested if statement

Syntax: Simple IF Statement:

if (condition)
{
true-statement-block;
}
statement-n;

In Simple If statement, if the given condition is satisfied, the true-statement block will be executed and follows the statement-n.
If the given condition is not satisfied (not satisfied), the true statement block will be skipped and follows the statement-n.

Ex:


Syntax:IF ELSE Statement:

if (condition)
{
true-statement-block;
}
else
{
false-statement-block;
}
statement-n;



Syntax: IF ELSE IF LADDER Statement:

if (condition-1)
{
true-statement-block1;
}
else if(condition-2)
{
true-statement-block2;
}
else
...
..

else if(condition-n)
{
true-statement-block-n;
}
else
{
false-statement-block;
}
statement-n; 


Nested IF Statement: Nested if means an if statement within another if statement is called Nested if statement.
 

Syntax:



If (condition-1)
{
         if (condition-2)
         {
                  True-statement-block-1;
         }
         else if(condition-3)
        {
                 True-statement-block-2;
        }
        else
        {
               False-statement-block;
        }
}
else if(condition-3)
{
      True-statement-block-3;
}
else 
{
        False-statement-block;
}

Switch Statement:
Switch statement is an alternate to if else if ladder statement. 
  • Switch statement have number of cases, in that which case is true that statement-block will be used. 
  • In switch we can check whether the given operand is equal to various values or not.
  • Switch statement reduce the execution time when compare to if else if ladder statement 
Syntax:
Switch(expression)
{
case value-1:
              statement(s);
              break;
case value-2:
              statement(s);
              break;
...
...
...



case value-n:
              statement(s);
              break;
default:
              statement(s);
              break;
};
statement-n;



Unconditional Control Statement: Unconditional Control statements will control the flow of execution without checking any conditions. Which means without checking any condition, it will jump from one part of the program to another part of the same program.

The following are list of Unconditional Control Statement:
  • return
  • break
  • continue
  • goto
Iterations / Loops:
Loops are used to execute some set of statements repeatedly either for a specific number of times or until a given condition is satisfied.
For example if i want to print my office address 5 times which contains 3 lines, i have to  of addressthe address of my office