Loops


The versatility of computer lies in its ability to perform as set of instructions repeatedly. This involves a repeating some position of a program either a specified number of a time or until a particular condition is being satisfied. This repeative operation is done through a loop control structure.
There are 3 methods by way of which we can repeat a part of a program. They are

  1. Using a while statement
  2. Using a do while statement
  3. Using a for while statement

While loop: It is a conditional control loop statement The while is an entry controlled loop statement. In entry controlled loop, the control conditions are tested before start of the loop execution. If the conditions are not satisfied then the body of the loop will not be executed.
General form
While (test condition) { Body of the loop }
The condition being tested may use relational or logical operators.
Eg: program to print "n" natural numbers while using loop.

                main()
                {
                  int n, i=1;
                  clrscr ();
                  printf ("enter the values of n");
                  scanf ("%d", &n);
                  printf ("natural numbers from 1 to %d ", n);
                  while (i<=n)
                  {
                    printf ("%d\ t", i);
                    i++;
                  }
                  getch ();
                }
              


Eg: program to display even numbers &odd numbers from 1 to n

                void main ()
                {
                  int n, i=1;
                  clrscr();
                  printf("enter the values of n");
                  scanf("%d", &n);
                  printf("even numbers from 1 to %d ", n);
                  while(i<=n)
                  {
                    if(i%2==0)
                    printf("%d \t", i);
                    i++;
                  }
                  printf("odd numbers from 1to %d ", n);
                  i=1;
                  while(i<=n)
                  {                       
                    if(i%2==1)
                    printf("%d\t", i);
                    i++;
                  }
                  getch();
                } 
              

do while

On some occasions it might be necessary to execute the body of the loop before test is performed. Such situations can be handled with the help of do while statement.
General form:
do
{
Body of the loop
} while (test condition);
On reaching the do statement the program proceeds to evaluate the body of the loop first. At the end of loop, the test condition in the while statement is evaluated. If the condition is true, the program continues to evaluate the body of loop once again. This program continues as long as the condition is true. When the condition becomes false, the loop while be terminated and the control goes to the statement that appears immediately after while statement.
Since, the test condition is evaluated at the bottom of the loop,the do while construct provides an exit-controlled loop and the form the body of the loop is always executed at least once.
Flow chart for exit control:

Example: To print ā€˜nā€™ natural number using do while

                #include < stdio.h>
                #include < conio.h>
                void main()
                {
                  int n,i=1;
                  clrscr();
                  printf("enter any number");
                  scanf("%d",&n);
                  printf("natural numbers from 1 to %d",n);
                  do
                  {
                    printf("%d\t",i);
                    i++;
                  }while(i<=n);
                  getch();
                }             

FOR Loop

The for loop is another entry controlled loop that provides a more concise loop control structure.
The general form of for loop is
for(initialization; test condition; increment)
{
Body of the loop
}
1.The initializing control variables using assignment statement such as i=1&count=0,we can initialize control variables. The variables i & count are known as
LOOP CONTROL VARIABLES 2. The value of control is tested using the test condition. The test condition is a relational expression, such as i<10 determine, when the loop will exit. If condition is true ,the body will executed otherwise the execution continues with the statement that immediately follows the loop.
3. When the body of the loop is executed, the control is transferred back to for statement after evaluating the last statement in the loop. Now the control variable is incremented using assignment statement such as i=i+1 and the new value of control variable is again tested to see whether the loop condition is satisfied or not.
Ex: To print 1 to 100 digits

                  #include < stdio.h>
                  #include < conio.h>
                  void main()
                  {
                    clrscr();
                    for(i=1,i<=100,i++)
                    {
                      printf("%d/t",i);
                    }
                    getch();
                  }
                


Ex: To print multiplication table of any numbers from 1 to 20

                  #include < stdio.h>
                  #include < conio.h>
                  void main()
                  {
                    int n,i;
                    clrscr();
                    printf("enter any number");
                    scanf("%d",&n);
                    for(i=1,i<=20,i++)
                    {
                      printf("/n%d*d is %d", n,i,n*i);
                    }
                    getch();
                  }
                
Copyright © 2018-2020 TutorialToUs. All rights reserved.