Friday, January 27, 2012

Loops in c language

What is loop?. A basic idea .
Loops are used to repeat a block of code. Being able to have your program repeatedly execute a block of code is one of the most basic but useful tasks in programming -- many programs or websites that produce extremely complex output (such as a message board) are really only executing a single task many times. (They may be executing a small number of tasks, but in principle, to produce a list of messages only requires repeating the operation of reading in some data and displaying it.) Now, think about what this means: a loop lets you write a very simple statement to produce a significantly greater result simply by repetition.
There are three basic types of loops which are:
  • “for loop”
  • “while loop”
  • “do while loop”
Foe loop
The “for loop” loops from one number to another number and increases by a specified value each time.
The “for loop” uses the following structure:

      for (Start value; end condition; increase value)
                               statement;
  
Example
                                         for (i = 0; i < 10; i++)
                                         {
                                         printf ("Hello\n");
                                         printf ("World\n");
                                         }


  
  
  
  
  
  
  
  
 


Let’s look at the “for loop” from the example: We first start by setting the variable i to 0. This is  where we start to count. Then we say that the for loop must run if the counter i is smaller then ten. Last we say that every cycle i must be increased by one (i++).
In the example we used i++ which is the same as using i = i +1. This is called incrementing. The instruction i++ adds 1 to i. If you want to subtract 1 from i you can use i–. It is also possible to use ++i or –i. The difference is is that with ++i the one is added before the “for loop” tests if i < 10. With i++ the one is added after the test i < 10. 


While loop

The while loop can be used if you don’t know how many times a loop must run. Here is an example:
                               counter = 0;
                               while ( counter < howmuch)
                               {
                               counter++;
                               printf("%d\n", counter);
                               }

Let’s take a look at the example: First you must always initialize the counter before the while loop starts ( counter = 1). Then the while loop will run if the variable counter is smaller then the variable “howmuch”. If the input is ten, then 1 through 10 will be printed on the screen. A last thing you have to remember is to increment the counter inside the loop (counter++). If you forget this the loop becomes infinitive.
 

  
  
  
  
  
  
  
  
 

Do while Loop
he “do while loop” is almost the same as the while loop. The “do while loop” has the following form:
                                    do{
                                         do something; 
                                        }               while (expression);  Do something first and then test if we have to continue. The result is that the loop always runs once. (Because the expression test comes afterward). Take a look at an example:
                                      Int counter=0; 
                                       do
                                         {
                                            counter++;   
                                            printf("%d\n", counter); 
                                          }
                                         while ( counter < howmuch);                Note: There is a semi-colon behind the while line. Don’t forget it.
  



No comments:

Post a Comment