Statement in C

 The bodies of C functions (including the main function) are made up of statements. These can either be simple statements that do not contain other statements, or compound statements that have other statements inside them. Control structures are compound statements like if/else, while, for, and do..while that control how or whether their component statements are executed.

CONTENT:

  •   Simple Statement 
  •   Decision making statement

  •   Looping Statement 
  •   Nesting of control structure 
  •   Goto Statement


INTRO:

  •  In C, statements are executed sequentially. 
  •  C provides two styles of flow control to change the sequential execution of program: 
  •  Branching (Decision Making) 
  •  Looping 
  •  Branching is deciding what actions to take and looping is deciding how many times to take a certain action.

Decision Making Sataement:

  •  C language provides such decision making capabilities  by supporting “IF” statement. 
  •  Types of “IF” statements. 
  •  Simple if statement 
  •  if...else statement 
  •  Nested if...else statement 
  •  else if ladder

Simple if:

  •  Used to execute or skip one statement or group of statements for a particular  condition.
  •  Syntax:
          if (condition) 
          { 
          Block of statements; 
          } 
          Next Statement X;
  •  To execute only one statement, opening and closing braces are not required.





Example:


          #include<stdio.h>

            void main() 

          {

               int i = 5; 

                   if (i >1) 

                { 

                    printf(" i is greater than 1\n"); 

                }

                    printf(" End of program\n"); 

          } 


          OUTPUT:

            i is greater than 1 End of Program


Simple If else Statement:

          The if....else statement is an extension of the simple if statement. 

          Syntax:

          if (condition) 

          { 

              Block of statements-1; 

               }

               else 

               { 

              Block of statements-2;

          } 

              Next Statement;

         

          FLOWCHART:

            



            Example:

        #include<stdio.h> 
          void main()
          {
                int i = 5; 
                   if (i >0) 
             {
                 printf (" i is greater than 0\n"); 
             } 
                else 
             { 
                 printf (" i is not grater than 0\n"); 
             }
         }

     
        OUTPUT:

         i is greater than 0

      

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. 
  •        Syntax: 
       if(condition-p) 
       { 
              if(condition-q) 
           { 
                  statement-1; 
               } 
                  else 
               { 
                  statement-2; 
           } 
              statement-3; 
           } 
      else
     { 
      statement-4;
     }
       

   

        FLOWCHART:

     

     





       Example:-

       #include <stdio.h> 
       void main() 
       { 
       int a,b,c,big;
       a=4; 
       B=6; 
       C=3; 
            If (a>b) 
          {
            if (a>c) big=a; 
            else big=c; 
           }
            else 
        { 
            if (b>c) big=b; 
          else big=c; 
        } 
       printf (“Largest no=%d”,big); 
        }


       OUTPUT:

       Largest no=6
       

ELSE-IF…LADDER:

  •            When a series of many conditions have to  be checked we may use the ladder                 else if  statement.
  •            The structure of else-if ladder looks like a  ladder 
  •            Syntax: 
           If (condition 1)
              statement-1; 
                 else if (condition 2)
                    statement-2; 
                       else if (condition n)
                          statement-n; 
                             else 
                                default 
                                    statement;  next
  •            The conditions are evaluated from the top of the ladder to downwards. 
  •            As  soon  on  the  true  condition  is found,  the  statement  associated with it is             executed and the  control is transferred to the next statement – x (skipping the               rest of the ladder.) 
  •           When  all   the  condition false, the final else containing the  becomes default                statement  will  be  executed.

            FLOWCHART:

              

   

            Example: 


                void main() 
                {
                 int i=2;
                      if (i==0)
                 printf ("i==0\n"); 
                      else if (i==1) 
                 printf("i==1\n"); 
                      else if (i==2)
                 printf("i==2\n"); 
                      else 
                 printf(“enter the value between 0 to 2\n"); 
                }

              

            OUTPUT:

                   i==2

Switch Statment:

                   Syntax:
                   switch(expression) 
                   { 
                   case value1:
                             block1; 
                             break; 
                   case value2: 
                             block2; 
                             break; 
                      ...... 
                        .....
                   default: 
                              default block; 
                              break;
                   }
                   next statement;

              Flow chart:


                  


  • C has a built-in multiway decision statement known as a 
  • switch. The switch statement tests the value of a given variable (or expression)            against  a  list  of  case  values  and  when  a  match  is  found,  a  block  of statements associated with that case is executed. 
  • The expression is an integer expression or characters.  value1, value2, .... are constants or constant expressions and are known as case labels. 
  •  block1,  block2,......  are  statement  lists  and  may  contain  zero  or  more statements. 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 causes an exit from the switch statement, transferring the control to the statement-n following the switch. 
  •  The default is an optional case. When present, it will be executed if the expression does not match any of the case values. If not present, no action takes place if all matches fail and the control goes to the statement-n.

        Example:

         void main()
          {
         int i,n = 5; 
                for(i = 1; i<n; i= i+1) 
          { 
                 switch(i%2) 
          { 
        case 0 : 
           printf("the number %d is even \n",i); 
                     break; 
        case 1 : 
           printf("the number %d is odd \n",i); 
                     break; 
        }
        } 


       Output : 
       
       the number 1 is odd 
       the number 2 is even 
       the number 3 is odd 

Difference between if else and switch case:

        


Looping:

  •  In looping, a sequence of statements are executed until some conditions for termination of the loop are satisfied. 
  •  A program loop consist of two segments 
  •  Body of the loop 
  •  Control statement 
  •  Two types of control structure: 
  •  Entry-controlled loop 
  •  Exit-controlled loop

While loop:

  • Entry-controlled loop
  •  Syntax :
          while(condition 1)
          {
          body of the loop 
          } 
          next statement;





Example: 

void main() 

     int i = 0; 

     while (i<5) 

         {

            printf(" the value of i is %d\n", i);

            i=i+1 ; 

          }

}


Output : 

the value of i is 0 

the value of i is 1 

the value of i is 2 

the value of i is 3 

the value of i is 4


  • Program to calculate factorial of a given number using while loop.  

void main ( ) 

int n, fact =1; 

clrscr( ) ; 

     printf( “\n Enter the Number:”);

     scanf(“%d”, &n); 

     while(n>=1) 

    { 

fact = fact*n; n - - ; 

    }

 printf(“ \n factorial of given number is %d”, fact); 

getch( ); 

}


Do-While Loop:

  •  It tests the condition at the bottom of the loop after executing the body of the loop. we can be assured that the body of the loop is executed at least once. 
  •  It is mainly used in menu like programs where all the available choices are printed at least once.
  • Syntax : 

do 

   statement; 

   while(condition);


Flow chart:



Example:

 void main() 

  int i,n = 5; 
  i = 0;
 do
   {
    printf("the numbers are %d \n",i);
    i=i +1;
   } 
while( i<n) 
}


Output :

 the numbers are 0

 the numbers are 1

 the numbers are 2

 the numbers are 3

 the numbers are 4


 Program to calculate factorial of a given number using do while loop:

void main ( )

 { 

   int n, fact =1; 

   clrscr( ) ; 

    printf( “\n Enter the Number:”); 

    scanf(“%d”, &n); 

    do 

        {

          fact = fact * n; 

            n- -; 

        } 

    while 

           (n >= 1); 

   printf(“ \n factorial of given number is %d”, fact); 

   getch( ); 

}


For Loop:

  •  Entry-controlled loop 
  •  Used when the number of iterations is predetermined. 
  • Syntax : 

            for (initialization;test condition; increment/decrement) 
           {
            block of code
            }
           next statement;

Flow chart:






Example: 

void main()

int i, n=5; 
for (i=0;i<n; i=i+1)
printf (“The numbers are %d \n",i);
}

Output : 

The numbers are 0 

The numbers are 1 

The numbers are 2 

The numbers are 3 

The numbers are 4


Nested For Loop:

  • If a for loop is defined in another for loop, it is called nested for loop. 
  • The outer loop is executed first of all. 
  • The general format of nested for loop is:
              for (initialization; test condition; increment/decrement)
              { 
              for (initialization; test condition; increment/decrement) 
                   { 
                        block of code
                    } 
                        block of code
               } 
               next statement;


Write a program to display the stars as shown below:

 
 * * 
 * * * 
 * * * *
 * * * * *
 void main( ) 

int x, i, j ; 
printf(“How many lines stars (*) should be printed? :”); 
scanf(“%d”, &x); 
    for(i=1; i<=x; i++) 
    {
    for (j=1; j < =i; j++)
    { 
    printf( “*”);
    } 
    printf( “ \n”);
    }
 getch( );
 }





Example: 1

for(i=1;i<=10;i++)
    s = s + i; 
    p = p * i;
}


Example: 2

 i =1;
 while(i<=10) 
{
      s = s + i;
      p = p * i;
      i++;
 }


Example: 3

i =1;
do
{
     s = s + i; 
     p = p * i; 
     i++; 
while(i<=10);

Continue Statement:


  •  The continue statement is used to bypass the remainder of the current pass through a loop. 
  •  The loop does not terminate when a continue statement is encountered, instead, the remaining loop statements are skipped and the computation proceeds directly to the next pass through the loop. 
  •  The continue statement can be included within a while, a do-while, a for statement. 
  •  It is simply written as “continue”
  •  The   continue   statement   tells   the   compiler   “Skip   the   following Statements and continue with the next Iteration”
  •  In while and do loops continue causes the control to go directly to the  test – condition and then to continue the iteration process. 
  •  In the case of for loop, the updation section of the loop is executed before test-condition, is evaluated.

















Go to Statement: 


  •  The goto statement is a jump statement which jumps from one point to another point within a   function.
  •  Used to transfer the program control unconditionally from one statement to  another statement. 
  •  Syntax :
  •  goto label;
                 ………… 
                 ………… 
          label: statement; 


          or 


          label: statement;
                        .......
                         .......
          goto label;


Example: 

void main() 

     int n = 0; 

     Loop: 

     printf ("\n%d", n); 

     n++; 

     if (n<10)

     { 

     goto Loop; 

     } 

     getch();

 }


Output :


 0 
 1
 2
 3
 4
 5
 6
 7 
 8 
 9

      

Post a Comment

0 Comments