Reading:  

Getting started with C Language


Loops in C

Loops

Loops means executing some sequence of steps or statements until any condition for termination is satisfied.  The loops consist of two parts the body of loop and the test conditions which is also called control statements. According to the control statements the body of loops are executed repeatedly. To repeat the statements in body the conditions in  control statements must be satisfied.

The loops are classified in to two, depending upon  the position of control statements in the loop. They are

  • Entry controlled loops
  • Exit controlled loops

Entry controlled loops

In entry controlled loops the test conditions are tested before executing the body of loop. It means if the conditions are not satisfied, then the body of loop will not be executed.

Loops in C

Exit controlled loop

In exit controlled loop the condition is tested at the end of the body of the loop and as the result the body of the loop is executed at least once unconditionally. When it checks the conditions and if it is false then begin to execute the next step after the body of loop.

There  are many things to care about, they are

  • Setting the initialization counter
  • Execution of the loop statements
  • Testing the conditions for execution
  • Incrementing the counter

The most important thing to care about loop is writing the conditions. These conditions decides how many times the loop want to work and how to stop the iterations.  The conditions determine whether the iteration possible for the number of times or to determine if the conditions has been met.

 

In c language there are three types of loops. They are

  • for loop
  • while loop
  • do… while loop

for loop

It is an entry controlled loop. The test condition is evaluated and if the condition is true then the body of the loop is executed. After execution of body of the for loop the test condition is once again evaluated and if it is true the body is executed once again. This process of repetition continues  until the test condition becomes false and then the control transferred out of the loop. After exiting from the body of the loop the immediate step after the body of the loop is executed.

The general form of the for loop is as follows:

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

Example:

for(i=0;i<10;i++)
{
     sum= sum + i;
}

Execution of for statement :

  1. initialization of loop variables

    The assignment operations are performed here. Using an assignment operator the values are given to the loop variables.

    In the above example
    i=0;
    is the initialization of variable.
  1. The value of the looping variable is tested using the condition.

    The condition checking mostly use relational operators.
    i<10;
    it is the conditional statement in the above  example each time before entering into the loop’s body, checks the condition i<10 and determines whether to exit or enter into the body of the loop.
  1. When the body of the loop is executed the control passes back to the ‘for’ statement and increments the value of the loop variable as here it is i++ (i=i+1) and the checks the condition. According to the condition determines where to pass the control. If the condition is true then passes to the body of the loop and if false execute the immediate next step after the body of the loop. This process continues till the condition become false.

 

 While loop

The while loop is another entry controlled loop where checks the condition before entering in to the loops body. Then  the loop checks whether the  test expression is true or not. If the condition  is true, the statements inside the body of loop is executed ,which means the statements  inside the braces { } are executed if there is only one statement in the loops body we can also avoid the braces { } . There  will be an increment statement then it will change the value of loop variable and then again the test expression is checked whether test expression is true or not. This process continues until the test expression becomes false.

Syntax :

while(condition)
{
Body of the loop
}

Example :

i= 0;
while (i<10)
{
   sum = sum+i;
   i++;
}

Using this code we can find the sum of first 10 natural numbers sum.

do…while loop

The only one exit controlled loop in c language is do.. while loop. Here the body of the loop is executed at least once even if the condition is false. In the do.. while loop the loop condition is tested at the end of the body of the loop, and by this reason the loop is executed at least one. The do-while is not widely  used. The programmers goes for the while loop or for loop if it is possible, because of simplicity and ease of use.

Syntax :

do
{
     Body of the loop;
}while(condition);    

                 

Here the block of statement following the do is executed without any condition check. After this expression is evaluated and if it is true the block of statement in the body of the loop is executed again. Thus the block of statement is repeatedly executed till the expression is evaluated to false.

Example :

i=1;
do
{
sum=sum+i;
i=i+1;
}while(i<10);

It add all the numbers from 1 to 10, which means including 10.

Program to print the first n natural numbers using for loop.

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

 

Program to print the first n natural numbers using while loop.

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

Program to print the first n natural numbers using do…while loop.

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

 

 

 

 

                 

 

 

Description

This tutorial introduces you to C language. Many tutorials has been written. We are writing this tutorial to be brief and to the point, so that you concentrate on what matter, rather than finding yourself in a mess. We want you to get a start and build form knowledge you will gain from this tutorial



Environment

We will cover environment needs as we progress through this tutorial. At the very least you will need a PC with windows or Linus operating system.

Audience

Programmers new to C Language

Learning Objectives

Learn basics of C language and build from that knowledge.

Author: Subject Coach
Added on: 3rd Jan 2015

You must be logged in as Student to ask a Question.

None just yet!