Sunday 16 August 2015

C language Control statements Tutorials

Control Statements

    These statements are useful to decide which statements are executed and which statements are not to be executed and also decide the number of times that a particular set of statements are to be executed.

--> Control statements are two types:

      1) Decision control statements

      2) Loop control statements

1) Decision control statements

These statements are useful to decide which statements are to be executed and which are not to be executed. These are of two types

      i) if statement

      ii) switch case statement

--> If statements are three types.

a) if statement:

--> It is an alternative to conditional operator

   Syntax:

         if( condition )

         {

            statements

         }

--> The condition is evaluated first, If it is TRUE, then the statements in the if block get executed. After completing the execution the control transfers to the next statement and continnue the execution.

--> If it is FALSE, then the control transfers to the next statement and continue the execution.

Program:

#include< stdio.h >
#include< conio.h >
void main( )
{

      int n;
      clrscr( );
      printf("\nEnter your age: ");
      scanf("%d", &n);
      if(n>=18)
      {

         printf("\nYour are Major");

      }

      printf("\nYour are Minor");

}

--> When the if block contain only one statement, braces are optional.

b) if-else statement:

   Syntax:

         if( condition )
        {

             statements

        }
        else
        {

            statements

        }

--> The condition is evaluated first. If it is TRUE, then the statements in the if block get executed. After completing the execution the control transfers to the next statement and continue the execution.

--> It it is FALSE, then the control transfers to else block and continue the execution.

Program:

#include< stdio.h >
#include< conio.h >
void main( )
{

      int a, b;
      clrscr( );
      printf("\nEnter values of a & b: ");
      scanf("%d%d", &a, &b);
      if(a > b)
      {

               printf("%d is Bigger", a);

      }
      else
      {

               printf("%d is Bigger", b);

      }

}

--> if block should be immediately followed by else block.

--> When using if statement, specifying else block is optional.

--> It is useful when we have multiple options and we are required to select one among them.

c) else-if ladder:

   Syntax:

      if( condition 1)
      {

         statements

      }
      else if( condition 2)
      {

         statements

      }
      else if( condition 3)
      {

         statements

      }

      .

      .

      .
      else
      {

         statements

      }

--> Condition 1 is evaluated first. If it is TRUE, then the statements in the if block get executed. After completing the execution the control transfers to the next statement and continue the execution.

--> If it is FALSE, then condition 2 is evaluated and the same process is repeated.

Program:

#include< stdio.h >
#include< conio.h >
void main( )
{

      int a, b, c;
      clrscr( );
      printf("\nEnter values of a, b & c: ");
      scanf("%d%d%d", &a, &b, &c);
      if(a > b && a > c)
      {

              printf("%d is Bigger", a);

      }
      else if(b > a && b > c)
      {

              printf("%d is Bigger", b);

      }
      else
      {

              printf("%d is Bigger", c);

      }

}

Note:

--> exit( ) is the function which is used to terminate the program as required.

--> It is available in the header file stdlib.h.

--> It accepts an integer value as an argument.

--> "0" represent successful termination and nonzero value represent that some error as occur.

ii) switch case statement:

--> If statement is useful when we know range of the control variable or exact value of the control variable .

--> switch case statement is useful only when we know exact value of control variable. So, all the program that use switch case statement can also be implimented using if statement, but the reverse is not possible.

   Syntax:

      switch(variable/expression)
      {

            case < values > : statements
                                       break;
            case < values > : statements
                                       break;

            .

            .

            .

            .

            default: statements

      }

--> By using switch statement we can specify only control variable or expression(no relational operators).

--> The value of the var/exp, will be first verified with the value of first case.

--> If it match, then the statements in the corresponding case get executed.

--> If it doesn't match, then it is verified with the next case value and the same process is repeated.

--> If no case match, then the default case get executed.

--> Every case should be ended with a break statement.

--> We can have as many number of statements as possible and required.

--> The default case is optional.

--> If the default case is present it should be present after all the case statements.

--> The given set of statements can be associated with as many number of cases as possible and required.

Program:

#include< stdio.h >
#include< conio.h >
void main( )
{

      int n;
      clrscr( );
      printf("\nEnter any number(1-7): ");
      scanf("%d", &n);
      switch(n);
      {

            case 1: printf("It is Monday");
                        break;
            case 2: printf("It is Tuesday");
                        break;
            case 3: printf("It is Wednesday");
                        break;
            case 4: printf("It is Thursday");
                        break;
            case 5: printf("It is Friday");
                        break;
            case 6: printf("It is Saturday");
                        break;
            case 7: printf("It is Sunday");
                        break;
            default: printf("Let Us Code in C");

}

Note:

--> goto statement is referred as branching statement.

--> It is useful to transfer the control of execution from one statement to the other statement either if forward direction or in reverse direction only within that function.

--> If the goto statement is associated with a condition, then it is referred as conditional branching.

--> If the goto statement is not associated with any condition, then it is referred as un-conditional branching.

   Syntax:

      goto   < label >;

--> label is the name given to the statement.

--> The rules for naming a label are same as that of naming a variable.

--> label names should be unique i.e no two labels should have same name.

--> If the program can have as many number of goto statements as possible and required.

Drawbacks of goto statement:

--> usage of too many labels and goto statements results confusion in understanding the flow of control.

--> Mis-placing a label with another label results in abnormal behaviour in the program.

--> goto statements are strictly prohibited in C language.

2) Loop control statements

These statements are useful to repeatedly execute the given set of statements as long as some condition is satisfied.

-->These statements are of three types of follows.

      a) while

      b) do while

      c) for

a) while:

   Syntax:

         while( condition )
         {
                   statements
          }

--> The condition is evaluated first. If it is TRUE, then the statements in the while block get executed. After completing the execution the control transfers to the condition again and the same process is repeated as long as the condition is TRUE

--> If it is FALSE, then the control transfers to the next statement after the while statement and continue the execution.

--> while statement use pre-condition checking, which is also referred as Entry control loop mechanism.

--> The statements in the while block will be execited zero or more number of times.

--> If the statements in the while block get executed n times, then the condition will be evaluated (n+1) times.

Program:

#include< stdio.h >
#include< conio.h >
void main( )

{

      int n,r,sum=0;
      printf("Enter any Number: ");
      scanf("%d",&n);
      while(n>0)
      {

            r = n%10;
            sum = sum + r;
            n = n/10;

      }

      printf("\nSum of digits of given number is ",sum);

      }

}

--> When the while block contain only one statement then braces are optional.

--> Sometimes there may be "a" requirement then "a" statements in the while block are to be execited atleast one.

b) do-while:

   Syntax:

      do{

            statements

      }while( condition );

--> The statements in the do-while block are executed and after completing the execution the condition is evaluated.

--> If it is TRUE, then the control transfers to be giving of the do-while block and the same process is repeated as long as the condition evaluates to TRUE.

--> If it is FALSE, then the control transfers to the next statement after the do-while block and continue the execution.

--> So, do-while statement use post-condition checking which is also referred as Exit Control loop Mechanism.

--> So, the statements in the do-while block get executed one or more number of times.

--> If the statements in the do-while block are executed n times, then the condition will be evaluated n times.

Program:

#include< stdio.h >
#include< conio.h >
void main( )
{

      int n,r,sum=0;
      printf("Enter any Number: ");
      scanf("%d",&n);
      do
      {

            r = n%10;
            sum = sum + r;
            n = n/10;

      }while(n>0);
      printf("\nSum of digits of given number is ",sum);
      }

}

--> The variable used in the condition based on whose value the decision is being made and it is referred as loop control variable.

Drawbacks with while statements:

--> The loop control variable should be compulsorily assign with some initial value before the while block fails, which behaves the program abnormal.

--> The loop control variable should be compulsorily modified to the while block fails ,it results from the formation of infinite loop.

--> To overcome these problems, while statement is modified as for statement, whose syntax is as follows.

c) for:

   Syntax:

      for(initialization;condition;increment/decrement)
      {

            statements

      }

--> The initialization is performed first and then the condition is evaluated.

--> If it is TRUE, then the statements in the for block get executed. After completing the execution the control transfer to the inc/dec and then checks condion. This process repeated until condion is TRUE.

--> If it is FALSE, then the control transfer to the next statement after the for statement and continue the execution.

--> So, while and for statements are syntactically different, but semantically same.

--> If the for block contain only one statement, braces are optional.

--> In the initialization part we can specify as many number of statements as possible and required and they should be seperated by comma(,).

--> Similarly in the inc/dec part also we can specify as many number of statements as possible and required, they should be seperated by comma(,).

Eg:-

      for(n=1,p=1;n<=k;p++,n++;)
      {

            statements

      }

Program:

#include< stdio.h >
#include< conio.h >
void main( )
{

      int n,i,f0=0,f1=1,f2;
      printf("\nEnter N Value: ");
      scanf("%d",&n);
      printf("%d\t%d",f0,f1);
      for(i=1;i<=n-2;i++)
      {

            f2=f0+f1;
            printf("\t%d",f2);
            f0=f1;
            f1=f2;

      }

}



0 comments:

Post a Comment