4/6/11

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;