Saturday 22 December 2018

loop statements

  1. while loop (or) while statement :
The while loop is an entry control loop. In this, first condition is checked and if it true then body of the loop is executed, It will execute again and again till the condition is false. The syntax is:
             while(condition)
             {
                  body of the loop(statements);
             }
                statements;

  Ex:/*program for reverse of a number */
                     #include<stdio.h>
                     #include<conio.h>
                     void main()
                     {
                        int r,n,rev=0;
                        clrscr();
                        printf(“Enter value of n:”);
                        scanf(“%d”,&n);
                        while(n>0)
                         {
                                r=n%10;
                                rev=rev*10+r;
                                n=n/10;
                         }
                         printf(“\nReverse is %d”,rev);
                         getch();
                      }
2.      do-while loop (or) do-while statement:
                The do-while loop is an exit control loop. In this, first body of the loop is executed and then the condition is checked. If the condition is true, then the body of the loop is executed. If the condition is false, then it will exit from the loop.
The syntax for do-while loop is:
                                                do
                                                {
                                                    body of the loop(statements);
                                                }while(condition);
                                                statements;






















Ex:/*program for reverse of a number*/
                                #include<stdio.h>
                                #include<conio.h>
                                void main()
                                {
                                 int r,n,rev=0;
                                 clrscr();
                                 printf(“Enter value of n:”);
                                 scanf(“%d”,&n);
                 do
                                {
                                    r=n%10;
                                    rev=rev*10+r;
                                    n=n/10;
                                 }while(n>0);
                                  printf(“\n reverse is  %d” ,rev);
                                  getch();
                                }              
3.      for loop (or)  for statement :
                for loop is an entry control looping statement which repeats again and again till the condition is satisfied.It is a one step loop, which ‘initialize’, ‘check the condition and then ‘increment / decrement’.
The syntax is :
                                for(initial value; test condition; increment/decrement)
                                {
                                     body of the loop(statements);
                                }
                                statements;























In the above for loop, first the value is initialized, then in the loop the test condition is checked and further the loop will increment/decrement according to the requirement. After completion of the loop, i.e. when the condition becomes false then the statement will be executed.
Ex:/*program to print 1 to 10 numbers */
                #include<stdio.h>
#include<conio.h>
void main()
{
    int I;
    clrscr();
    printf(“the numbers from 1 to 10 are:”);
    for(i=1;i<=10;i++)
    {
       printf(“%d\n”,i);
    }
    getch();
}
Nested for:
                   When a for loop is executed within another for loop then it is called nested for loop. In this first the inner loop will be completed then the outer loop will be completed later according to the condition.  The syntax is:
   for(initial value-1; test condition-1; increment-1/decrement-1)
   {
       for(initial value-2; test condition-2; increment-2/decrement-2)
       {
                                Inner body of the loop(statements);
                       }
                      Outer body of the loop(statements);
                   }
                   Statements;
Ex:/*program to illustrate nested for loop*/
                   #include<stdio.h>
                   #include<conio.h>
                   void main()
                   {
                       int I,j,n;
                       clrscr();
                       printf(“Enter value of n:”);
                       scanf(“%d”,&n);
                       for(i=1;i<=n;i++)
                       {
                          for(j=1;j<=i;j++);
                          {
                                printf(“%d”,j);
                          }
                          printf(“\n”);
                       }
                       getch();
                   }
Output: Enter value of n 3
                   1
                   12
                   123

LOOPING STATEMENTS


LOOPING STATEMENTS :
                        When a single statement or a group of statements  executing again and again in a program i.e. in an iterative way, such type of processing is called Loop. These are classified into two parts:  a. body of the loop
                        B .control of the loop
                                The control loop statements tests certain conditions and then directs the repeated execution of the statements contained in the body of the loop. Mainly control loop are categorized into two parts: They are:
                                                  1. Entry control loop
                                      2. Exit control loop
1. Entry control loop:
                        In an entry control loop, first the condition is checked, if it is true, then body of the loop is executed otherwise the control is moved out of the loop i.e. we can exit from the loop when the condition becomes false.
                        Entry control loop is also called base loop. The “while” and “for” are the examples of entry control loop. The flow chart is as shown below:
2. Exit control loop:

                In an exit control loop, first body of the loop is executed and then condition is checked. If the condition is true, then again the body of the loop is executed. If the condition is false, then control will move out from the loop.
                Exit control loop is also called derived loop. The “ do-while “ statement is an example of exit control loop. The flow chart is shown below:


Conditional control statement


Conditional control statement:
                                                 The conditional control statement is based on conditional operator, that which executes fast in a single line. It takes the combination of “ ? and : “ for using the conditional statement. The syntax is:
                                                 exp1?exp2:exp3;
                        This is a substitute of “if-else” statement that which uses a single condition. Similarly, the nesting conditional control statement syntax is:
                                        exp1?(exp2?exp3:exp4):exp5;
                  In the above nested conditional control statement ,first exp1 will be executed and if it is true, then exp2 will be checked. If exp2 is true then the exp3 will be executed, otherwise exp4 will be executed. But if exp1 is false, then only the exp5 will be executed. This is the substitute of nested-if statement.
Ex:/*program to calculate salary depending basic pay*/
      #include<stdio.h>
      #include<conio.h>
       void main()
        {
                        int sal,bp;
                        clrscr();
                        printf(“Enter basic pay:”);
                        scanf(“%d”,&bp);
                        sal=(bp!=1000)?((bp<1000)?bp+100:4*bp+200):2000;
                        printf(“\n sal=%d”,sal);
                        getch();
         }

switch statement


switch statement :
                                Whenever we want to check more possible conditions for a single variable a no. of statements are necessary. C has a built-in multiway decision statement known as a “ switch “ statement. The switch statement tests the value of a given expression against a list of case values and when a match is found, a block of statements associated with that case is executed.
                                In switch case statement, no case can have two equal values. Default may appear at any place and not compulsory. The flowchart and syntax of switch statement is shown below :

     
       
  Syntax :                 switch(expression)

                                                 {
                                                        case value-1:block-1;
                                                                                   break;
                                                        case value-2:block-2;
                                                                                   break;
                                                        case value-3:block-3;
                                                                                   break;
 


                                                         case value-n:block-n;
                                                                                   break;
                                                          default        :block n+1;
                                                 }
                                                 statement-x;

                        Ex:
                                                 #include<stdio.h>
                                                 #include<conio.h>
                                                 void main()
                                                 {
                                                      int a,b,c,choice;
                                                      clrscr();
                                                    printf(“Enter values of a and b:”);
                                                    scanf(“%d%d”,&a,&b);
                                                    printf(\n1.ADDITION\n2.SUBTRACTION\n3.MULTIPLICATION\n4.DIVISION
                                                                                \n5.MODULo DIVISION”);
                                                    printf(“\n select ur choice:”);
                                                    scanf(“%d”,&choice);
                                                    switch(choice)
                                                    {
                                                           case 1:c=a+b;
                                                                                break;
                                                           case 2:c=a-b;
                                                                                break;
                                                            case 3:c=a*b;
                                                                                break;
                                                            case 4:c=a/b;
                                                                                break;
                                                            case 5:c=a%b;
                                                                                break;
                                                            default:printf(“\n Enter only a valid choice”);
                                                    }
                                                    printf(“\nResult is = %d”,c);
                                                    getch();
                                                 }