7.1 – While iteration statement

by subbu on October 2, 2013

While iteration statement in C language:

While developing applications, there are circumstances were we want to do the same thing many times. Say for example, if we want to produce serial numbers from 1 to 20 then we need to print using 20 printf() statements, but it is easier to use a loop to do the same thing. The only thing you have to do is to setup a loop that executes the same printf() and printing and increment the value of a variable by 1 for 20 times.

Executing a statement or multiple statements repeatedly is called a loop or iteration. C language provides three statements to build a loop, those are while, do-while and for iteration statements.
The while iteration statement is a basic and fundamental iteration statement, it is easy to understand and use.

while iteration statement

The while iteration statement has a keyword while, a condition to control the loop and a statement or block of statements under the conditional statement.
Like if selection statement the wile condition is checked, if the condition is true then executes a statement or block of statements under the while conditional statement.
Unlike in if selection statement control goes back to the conditional statement to continue iterations.
If the while condition is false then terminates the execution of loop.

Let us see an example to understand the nature of while loop

#include<stdio.h>
int main()
{
int x=5;
while(x!=8)
{
   printf("%d\t",x);
   x++;
}
return 0;
}

Output:
5        6        7

Here in this example x is assigned with 5. The condition 5!=8 gives true, so proceeds to execute the body of the loop. The printf() prints the value of x (5) and x++ increments the value of x by 1 (6).
The control goes back to the conditional statement. The condition 6!=8 gives true, so executes the body and goes back to the conditional statement again.
The loop is terminated only when x is 8 and 8!=8 gives false

Example:

#include<stdio.h>
int main()
{
int x=5;
while(x==8)
{
  printf("%d\t",x);
  x++;
}
return 0;
}

Here in this example loop doesn’t execute even for a single time because the condition gives false during the first check. In while loop, condition is checked while entering into the loop, so it is called entry controlled iteration statement.

while(1)
   printf("Hello World");
while(10<20)
   printf("Hello World");

The above two loops execute forever because condition is always true. The loops of this kind are called infinite loops or unlimited loops.
More often, we want a loop to execute certain number of times called limited loop. To do this we use a common variable to count how many times a loop is executed called iterative or loop variable. Loop variables are often named with simple names like i, j or k.

Specification: print 15 to 20 natural numbers.

Logic:
printing 15 to 20 natural numbers

We will take i as a loop driver variable, it must be started from 15 (i=15;) and must be incremented by 1 (i++;)for every iteration. The loop must continue so long as the value of i is less than or equal to 20 (i<=20).

#include<stdio.h>
int main()
{
int i=15;               /* driver variable */
while(i<=20)            /* condition        */
{
   printf("\t%d",i);
   i++;                /* incrementing the value by 1 */
}
return 0;
}

Output:
15      16      17      18      19      20


Specification: Print the “codingfox.com” for 5 times.

Logic:
printing codingfox for 5 times

We will take x as driver variable, it will be initialized with 1 (x=1;) and must be incremented by 1 (x++) for every iteration. The loop must go as long as the value of x is less than or equal to 5 (x<=5).

#include<stdio.h>
int main()
{
int x=1;
while(x<=5)
{
 printf("codingfox.com\n");
 x++;
}
return 0;
}

Output:
codingfox.com
codingfox.com
codingfox.com
codingfox.com
codingfox.com

Here driver variable x is only used to run the loop for 5 times, printing “codingfox.com” for every iteration is done using printf() statement.

Specification: Print the natural numbers from 20 to 15

Logic:
printing 20 to 15 natural numbers
Here we will take y as driver variable, it will be initialized with 20 (y=20;) and must be decremented by 1 (y- -;) for every iteration. The loop must go as long as the value of y is greater than or equal to 15.

#include<stdio.h>
int main()
{
int y=20;
while(y>=15)
{
  printf("\n%d",y);
  y--;
}
return 0;
}

Output:
20      19      18      17      16      15

Quiz 1:
1) What would be the output of following program?

#include<stdio.h>
int main()
{
int i=1;
while(i<=5)
   printf("\t%d",i++);
return 0;
}
Show Answer
Output:
1        2        3        4        5

Here i is initialized with1, the value of which is printing and then incrementing by 1 for every iteration. The loop runs as long as the value of i is less than or equal to 5

2) What would be the output of following program?

#include<stdio.h>
int main()
{
int i=7;
while(i>=3)
    printf("\t%d",--i);
return 0;
}
Show Answer
Output:
6        5        4        3        2

Here i is initialized with 7, the value of which is first decrementing by 1 and then printing. The loop runs as long as the value of i is greater than or equal to 3. Control enters into the loop when i is 3 but prints 2 because decremented by one before printing.

3) What would be the output of following program?

#include<stdio.h>
int main()
{
int i=0;
while(++i<=5)
   printf("\t%d",i);
return 0;
}
Show Answer
Output:
1        2        3        4        5

4) What would be the output of following program?

#include<stdio.h>
int main()
{
int i=1;
while(1)
{
   printf("\t%d",i++);
   if(i==5)
      goto abc;
}
abc:
return 0;
}
Show Answer
Output:
1        2        3        4

Here while(1) forms an infinite loop, but control get out of loop when i==5 due to goto statement.

5) What would be the output of following program?

#include<stdio.h>
int main()
{
int i=1;
while(++i<=5);
   printf("%d",i);
return 0;
}
Show Answer
Output:
6

Here the while conditional statement is terminated with a ; hence iterations complete with in the conditional statement and comes out of the loop when i is 6. The value of i that is 6 is printed as the output.


Quiz-2
1) What would be the output of following program?

#include<stdio.h>
int main()
{
while()
   printf("Hello world");
return 0;
}
Show Answer
Output:
Error

A while loop can’t be written without an expression

2) What would be the output of following program?

#include<stdio.h>
int main()
{
int x=5;
while(x)
  printf("\t%d",x--);
return 0;
}
Show Answer
Output:
5        4        3        2        1

Here x is initialized with 5, printing and decrementing the value of x by 1 for every iteration. The loop is terminated when x is 0 (false)

3) What would be the output of following program?

#include<stdio.h>
int main()
{
int x=5;
while(x);
  printf("\t%d",x--);
return 0;
}
Show Answer
Output:
Infinite loop without any output

Here the conditional statement is terminated with semicolon and not changing the value of x. Hence the condition is true forever.

4) What would be the output of following program?

#include<stdio.h>
int main()
{
int x=5;
while(x--);
  printf("\t%d",x);
return 0;
}
Show Answer
Output
-1

Here, as the while conditional statement is terminated with a semicolon the loop runs with in the conditional statement. Loop terminates when x is 0 and then decrements the value of x by 1 (-1) as it is post decrement.

5) What would be the output of following program?

#include<stdio.h>
int main()
{
short x=32765;
while(x)
  printf("\t%d",x++);
return 0;
}
Show Answer
Output:
32766  32767  -32768  -32767……… -2    -1

We may think that it is an infinite loop but, prints negative numbers from -32768 to -1 and stops at 0. The reason to this is overflow of short type results falling towards negative side and reaches to 0.

Previous post:

Next post: