Programming Tutorials

while, do while and for loops in C

By: Ram in C Tutorials on 2008-09-16  

While Statement:

The while-loop has a condition and the statements in the curly braces are executed while the condition has the value "true".

while (condition)
   {
   statements;
   }

The conditional expression is evaluated every time the loop is executed, if the value of the expression is true, then it will carry on with the instructions in the curly braces. If the expression evaluates to false (or 0) then the instructions in the braces are ignored and the entire while loop ends. The computer then moves onto the next statement in the program.

Moreover, if the condition has the value false before the loop has been executed even once, the statements inside the braces will not get executed at all.

Example:

/*Program to reverse a digit*/
main()
{
int n,s,d;
printf("enter a number");
scanf("%d", &n);
s=0;
while(n!=0)
{
  d=n%10;
  s=s*10+d;
  n=n/10;
}
printf("reverse = %d" , s);
}

Do-While statement:

In a do..while loop the condition is at the end of this loop. This means that a condition will always be executed at least once, before the test is made to determine whether it should continue. This is the only difference between while and do..while.

do
   {
   statements;
   }
while (condition)

Example:

/*To print all divisors of a given number*/
main()
{
int a,r,n
printf("enter a number");
scanf("%d,&n);
a=2;
do
{
  r= n % a;
  if(r = = 0)
  printf("%4d\n",a);
  a=a+1;
}
while(a <= n/2)
}

For Statement:

The "for" loop statement is used to perform repeated processing. For all values of variable from value1 to value2 in steps of value3, repeat the following sequence of commands....

Syntax:

for (initialisation ; condition ; increment)
	statement;
		or
	{
		statements;
	}

Example:

/* to find factorial of a number as in 1!+2!+3!........n! */
main()
{
 int s,n,f,a;
printf("enter a number");
scanf("%d",&n);
s=0;
f=1;
for(a=1;a<=n;a++)
    {
 f=f*a;
s=s+a;
}
printf("1!+2!+3!........%d=%d",n,s);
}

Nested Loops:

A nested loop is a loop within another loop. It is mostly used in case of for loops. A for loop controls the number of times a particular set of statements will be carried out. Another outer loop could be used to control the number of times that a whole loop is carried out.

Example:

We shall try to print the following using the nested for loops.

1
1 2
1 2 3
1 2 3 4
---.
---..
---
10 times
main()
{
int i,a;
for(a=1;a<=10;a++)
  {
     for(i=1;i<=a;i++);
     printf("%3d",i);
     printf("\n"):
   }
}

Continue and Break Statements:

When a continue statement is encountered, a loop will stop whatever it is doing and will go straight to the start of the next loop pass. It to avoid executing a lot of irrelevant statements.

Example:

for (i = -10; i <= 10; i++)
   {
   if (i == 0)
      {
      continue;
      }
   printf ("%d", 20/i);
   }

The break statement is used to quit any of the three loops above at any stage, whether it has finished or not.

Example:

 main()
 {  
	x=5;
	do
	{
		printf("%d",x);
		if (x>7)
		break;
		x=x+1;
	}
	while(x<=20)
 } 





Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in C )

Latest Articles (in C)