Social Icons

Thursday 9 January 2014

Control structures



1   1)   Control structures are very useful thing in the C programming as the name indicates which controls our program by some of structures.  now Let us see what are the types of the control structures

2  2)      The control structure is divided into three types selection, sequence and loop. The selection consists of simple if,if-else,nested-if ,else-if ladder and switch the loop control consists of do-while, while, for.

Simple if:
The simple if statement has only if condition in it.If the given condition in it is true then it executes the action in the loop if not the loop will be stopped and considers other actions.Let us see the syntax of simple if statement
     if(expression is true)
     {
        action 1;
      }
       action 2;
If-else:
The if-else statement is used to express decisions. It is different from simple if statement,in this if the expression in if is true it continues in if loop otherwise it goes to else loop and prints that action if both expressions are false it goes to other action.The syntax is as follows

    if(expression is true)

    {

      action 1;

    }

    else

    {

      action 2;

    }
     action 3;

So this statement is used for a decision if one is true then it prints action 1,if it is

not true then it goes to else part if that is also false it goes to action 3.
Nested if: 
Nested if ,means having more than one if statement in a program this is very useful than simple if and if else statement,because in that both statements if we want to consider 2 if conditions that will not be possible.Let us see the syntax and example of nested if statement
     if(expression 1 is true)
       {
          action 1;
       }
     if(expression 2 is true)
      {
        action 2;
      }
So in this way we can implement nested if statement so now we see an example so that you can understand 
  example:   
 
#include <stdio.h>
int main ()
{ 
   int a = 100;
    int b = 200;
      if( a == 100 )
       {
          if( b == 200 )
             {
                  printf("Value of a is 100 and b is 200\n" );
              }
       }     
   printf("Value of a is : %d",a);
   printf("Value of b is : %d",b); 
 
So now you have understood about nested if statement,we have given two values a,b then in two if statements we 
are checking the conditions and printing that values,suppose if we give other values than 100 and 200 the loop 
terminates and it prints other values. 

Switch statement:
The switch statement is also an selection statement.It is mostly a matter of preference

which you use,if we want to perform addition,subtraction,multiplication and division at your selection this can be done by switch statement.The syntax of switch statement is as follows
     switch(expression)

     {                                                       

       case 1:

        {

          action 1;

        }

       case 2:

        {

          action 2;

        }

       case 3:

        {

          action 3;

        }

       default:

        {

          action 4;

        }

so this makes us a two or more way of decision that is a multi way decision
Do-while statement:
A do-while statement is a looped structure which is repeated for that particular condition. The

syntax of do-while statement is as follows

      do      

      {

     statement  

      }

     while (expression);

If the statement is executed, then expression is evaluated. If it is true, statement

is evaluated again, and so on. When the expression becomes false, the loop terminates
While loop:
This statement is also a looped structure, in this we pass some condition in while if that

condition is true the loop will continue if it is false the loop terminate.

Let us see the syntax

   while(condition is true)

   {

     action 1;

   } 

    action 2;
For loop:
This statement is very simpler than the other statements in this we declare initialization

value, test value and increment or decrement at a time. Let us see the syntax and one example

of this statement

     for(initial value;test;increment or decrement)

      {

        action 1;

      }

        action 2;

example:

for(i=1;i<=n;i++)

{

  f=f*i;

}

So these are required control structures mostly used in C programming. The other structure called as sequence structure where the program can be done in a sequential manner.

No comments:

Post a Comment