Friday, September 26, 2014

LOOPS IN C

                                            DIFFERNT TYPES LOOPS IN C


  What is a loop?
     When we want to execute certain statements repeatedly, loops are used i.e., the statements which must be executed repeatedly must be placed inside loop(body section).

For example if we want to print "c programming" 10 times on screen?

    main()
{
  printf("\n c programing");
  printf("\n c programing");
  printf("\n c programing");
  printf("\n c programing");
  printf("\n c programing");
    printf("\n c programing");
  printf("\n c programing");
  printf("\n c programing");
  printf("\n c programing");
  printf("\n c programing");
}

if we use the above strategy in printing name times it takes lot of time and burden to programmer.
In this situation we use loops. Basically there are three types of loops
i) for loop   ii) while loop   iii)do-while loop

Loops are categorized into two types.
a) entry control loop (while, for loop)    b) exit control loop(do-while loop)

a) Entry controlled loop: When using this loop the condition is tested first and if condition is true then body inside the loop will be executed.  If condition fails execution comes out from the loop and the statements immediately after the loop will be executed.

i) while loop:
    
     while(test expression)
        {
                       body of the loop;
         }


/* write a C program to print c programming on the screen */
 

  main()
{
     int i=1;
     while(i<=10)
     {
        printf("\n c programming");
        }
}


  output:  c programming
               c programming
               c programming
               c programming
               c programming
               c programming
               c programming
               c programming               
               c programming
               c programming

/* write a c program to print numbers from 1 to 10 using while loop */


  main()
    {
       int i;
        while(i<=10)
        {
           printf("\t %d",i);
         }
  }

output: 1     2     3     4     5     6     7     8    9   10






(c programming, c programming variable intialisation, datatypes in c, integer data types in c, float data types in c,int,float,char,data range, decision making, looping, entry control looops, exit control loops, for,while,do while , if else , nested if , switch, functions, recursive functions, nested loops, tower of hanoi, pointers, strcutres , unions , enum, files in c, binar files, random access in files, fseek,ftell, rewind, copy of one file to other file, merging, continue, break, goto, realtional operators, bitwise operators, logical operators, shift operators procedural languages.


 

No comments:

Post a Comment