Showing posts with label Loops in C. Show all posts
Showing posts with label Loops in C. Show all posts

Saturday, October 23, 2010

Decision Making - Looping

Here we talk about Decision Making concept in 'C'

What are LOOPS?

Sometimes you will want to do something a lot of times. An example would be printing a character at the beginning of each line on the screen. To do this you would have to type out 24 printf commands because there are 25 rows on the screen and the 25th row will be left blank. We can use a loop to do this for us and only have to type 1 printf command. There are 3 basic types of loops which are supported by 'C' Language

  • for loop
  • while loop
  • do while loop
 In looping process in general would include the following four steps
1. Setting and initialization of a counter
2. Exertion of the statements in the loop
3. Test for a specified conditions for the execution of the loop
4. Incrementing the counter

The test may be either to determine whether the loop has repeated the specified number of times or to determine whether the particular condition has been met.


  • for loop
The for loop lets you loop from one number to another number and increases by a specified number each time. This loop is best suited for the problem where we know exactly how many times we need to loop for.The general form of the for loop is:

for (initialization; test condition; increment)
{
body of the loop
} 


the for construct is composed of three parts, each divided by a semicolon, see above
  • In the first section, the initialization, we initialize an index variable (which is usually being named i, j or k) to its initial value;
  • In the second part, we test whether a variable (usually the same we have just initialized in the first part) satisfies a certain condition: if it does, we enter the loop one more time, otherwise we exit from it;
  • In the third and last part, we update the variable — usually by incrementing or decrementing it by 1.
Additional features of the for loop:

We can include multiple expressions in any of the fields of for loop provided that we separate such expressions by commas. For example in the for statement that begins

For( I = 0; j = 0; I < 10, j=j-10)

Sets up two index variables I and j the former initialized to zero and the latter to 100 before the loop begins. Each time after the body of the loop is executed, the value of I will be incremented by 1 while the value of j is decremented by 10.

Just as the need may arise to include more than one expression in a particular field of the for statement, so too may the need arise to omit on or more fields from the for statement. This can be done simply by omitting the desired filed, but by marking its place with a semicolon. The init_expression field can simply be “left blank” in such a case as long as the semicolon is still included:

The above statement might be used if j were already set to some initial value before the loop was entered. A for loop that has its looping condition field omitted effectively sets up an infinite loop, that is a loop that theoretically will be executed for ever.

Example

#include <stdio.h> 
void main()
{
int max = 5;
int i = 0; 
for(i = 0; i < max;i++)
{
printf("%d\n",i);
     }
}
  • while loop
The while loop is different from the for loop because it is used when you don't know how many times you want the loop to run. You also have to always make sure you initialize the loop variable before you enter the loop. Another thing you have to do is increment the variable inside the loop. 
The general format of the while statement is:

while (test condition)
{
body of the loop
}
Example

#include<stdio.h>
void main()
{
  int n, i=0;
  printf(“Enter the upper limit number”);
  scanf(“%d”, &n);
  while(I < = n)
  { 
    printf(“\t%d”,I);
    I++; 
  } 
}

In the above program the looping concept is used to generate n natural numbers. Here n and I are declared as integer variables and I is initialized to value zero. A message is given to the user to enter the natural number till where he wants to generate the numbers. The entered number is read and stored by the scanf statement. The while loop then checks whether the value of I is less than n i.e., the user entered number if it is true then the control enters the loop body and prints the value of I using the printf statement and increments the value of I to the next natural number this process repeats till the value of I becomes equal to or greater than the number given by the user.

  •  do while loop
The do while loop is also a kind of loop, which is similar to the while loop in contrast to while loop, the do while loop tests at the bottom of the loop after executing the body of the loop. Since the body of the loop is executed first and then the loop condition is checked we can be assured that the body of the loop is executed at least once.

The syntax of the do while loop is:

Do
{
statement;
}
while(expression);
Here the statement is executed, then expression is evaluated. If the condition expression is true then the body is executed again and this process continues till the conditional expression becomes false. When the expression becomes false. When the expression becomes false the loop terminates.
Example
#include<stdio.h>
 
void main()
{
   int i,times;
   scanf("%d",&times);
   i = 0;
   do
   {
      i++;
      printf("%d\n",i);
   }
   while(i <= times);
   return 0;
} 

Break and continue

You can exit out of a loop at any time using the break statement. This is useful when you want a loop to stop running because a condition has been met other than the loop end condition.
#include<stdio.h>

int main()
{
int i;
i=0;
while(i < 10)
{
i++;
if (i == 5)
break;
}
return 0;
}

 
You can use continue to skip the rest of the current loop and start from the top again while incrementing the loop variable again. The following example will never print "Hello" because of the continue.
#include<stdio.h>

int main()
{
int i;
i=0;
while (i < 10)
{
i++;
continue;
printf("Hello\n");
}
return 0;
}