Looping in C language

Introduction of looping -: Suppose if we want to calculate all sum of integer 1 to 1000. It is not possible in  IF statement. This will be written in this way

sum = sum + n*n;

1000 time it is called the loop.

In looping a sequence of statement are executed until some conditional for termination of the loop are satisfied. A programme loop there for consists of two segments. The control statement tests certain condition and then direct the repeated execution of the statements contained in the body of loop.

Looping process in c :- there are four types of looping process in c

  1. Setting and initialization of a condition variable
  2. execution of the statement in the loop
  3. test for a specified value of the condition variable for execution of the loop
  4. incrementing and updating  the condition of variable.

 

The C programming providing three type of loop these are

  1. The while statement
  2. do statement
  3. for statement

The while statement -: This is the simplest loop in c language. The formula of while loop is simple

while (test condition )

{

   body of loop

}

The while loop is the entry loop controlled loop statement. the test condition is evaluated if the condition is true then the body of loop executed and it runs again and again check the condition if is again true then body will be executed again it repeat and repeat. If the test condition is false it will be exit .

we write a programme here

sum=0;

n=1

while (n<=10)

{

   sum=sum+ n*n;

   n=n=1;

}

printf("sum=%d\n",sum);

in this example body of the loop executed 10 times for n=-1,2,3.....

2. do loop -: some time the test condition is not true and necessary to test the body of the loop this condition we use the do while loop  the formula of this loop is

do

{

   body of the loop

}

while (test condition)

in this condition the process the body of the loop is executed first and end of the loop the first condition of while statement is evaluated.

3.For loop-: or For statement – The for loop is another entry controlled loop that providing a more concise loop structural. the general form a loop statements

for(initialization ; test condition ; increment)

{

  body of loop

}

the execution of for statement is here

  1. initialization of the control variable is done first, using assignment statement such as i=1 and count =0. The variable i and count are known as loop control variable
  2. The value of the control variable is tested using the test condition .The test condition a relation expression such as i<10 that determine when the loop will exit. if the condition is true the body of the loop is executed. otherwise the loop will be terminated.
  3. when the body of loop executed the control is transferred back to the for statement after evaluating the last statement in the loop. Now the control variable is increment using an assignment statement such as i=i++; or i=i+1; and the new value of the control variable is assign  tested to see whether it satisfied the loop condition . if the loop condition is true it executed again . if false it will exit.

example :-

                              for (x=0;  x<=10;  x+1)

{

  printf("%d",x);

}

printf("\n");


 

 

Leave a Comment