Friday, 15 March 2019

C LANGUAGE LOOP

LOOP- C language provide three types of loop


  1. For loop
  2. while loop
  3. do while loop

For loop

  1. This is most commonly used loop in C Language.
  2. The syntax and flow of this loop is simple and easy to learn.
  3. for loop is an entry controlled loop.
  4. for loop is preferred statement is to be executed for fixed or know number of times.
  5. for loop consists of three components.
  6. Syntax for loop-for(Initialization;condition;Increment/Decrement)   








                                               
For loop flow chart








  1. Ex-  Using for loop print 1 to 10 number.

          #include<stdio.h>
          #include<conio.h>
           void main()
            {
                 int x;
                 clrscr();
                for(x=1:x<=10;x++)
                {
                        printf("%d\n",x);
                         }
                            getch();
                               }



while loop-   





  1. while loop is also Entry controlled loop.
  2. while loop conditions are checked if found true then and then only code is executed.
  3. Initialization, Inreamantaion  and condition steps are no different line.
  4. While(1)- While(1) is used for Infinite loop.
  5. Single line code opening and closing braces are not needed.
  6. While loop is used to repeat a section of code an unknown number of times until a specific condition is met.

  1. While loop syntax;

                                 Initialization;
                                 while(condition)
                                    {
                                     .........................
                                     .........................
                                     Increment;
                                       }

While loop flow chart

  • Ex- Using while loop print 1 to 10 no.

               #include<stdio.h>
               #include<conio.h>
               void main()
                 {
                      int x=1;
                      clrscr();
                       while(x<10)
                          {
                                     printf("%d\n",x);
                                     x++;
                                              }
                                      getch();
                                                    }                                 

do while loop-

  1. It is Exit Control loop.
  2. Initialization, Incrementation and Condition steps are on different line.
  3. It is also called Bottom Tested loop.
  4. do while condition is tested at bottom and body has to execute at least once.
  5. when do while loop has to be executed at least one time.

  1. do while loop syntax;

        Initialization;
        do{
            ................
            ................
            Incrementation;
                }while(condion);

       
do while loop flow chart
         

  • Ex-Using do while to print 1 to 10 no.

                 #include<stdio.h>
                 #include<conio.h>
                  void main()
                      {
                                int x=1;
                                 do{
                                            printf("%d\n",x);
                                             x++;
                                              }while(x<10);
                                             getch();
                                                 }