while, do while and for loops in C

By: Ram Emailed: 1589 times Printed: 2038 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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)

 } 


C Home | All C Tutorials | Latest C Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(3)


1. View Comment

the best!! it helps me a lot.....

View Tutorial          By: shin at 2009-02-15 18:12:31
2. View Comment

how to create a do while,the output wil show the multiplacation table???

View Tutorial          By: waa at 2009-09-02 19:23:45
3. View Comment

sir please check the program in for loop

View Tutorial          By: vc ramreddy at 2009-09-28 23:35:10

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Ram
smskannel SMS gateway run in background
while, do while and for loops in C
Statements in C
Writing The First C program

More Tutorials in C
Sum of the elements of an array in C
Printing a simple histogram in C
Sorting an integer array in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Command-line arguments in C
Calculator in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
Unicode and UTF-8 in C
Formatting with printf in C
if, if...else and switch statements in C with samples
Statements in C

More Latest News
Most Viewed Articles (in C )
UNIX read and write system calls sample program in C
Using memset(), memcpy(), and memmove() in C
Open, Creat, Close, Unlink system calls sample program in C
perror() Function - example program in C
Find square and square root for a given number in C
Conditional Expressions in C
Precedence and Order of Evaluation in C
register Variables in C
Formatting with printf in C
Sum of the elements of an array in C
Printing a simple histogram in C
scanf and sscanf sample program in C
Using calloc() Function in C
Using Shift Operators in C
Using the qsort() and bsearch() functions with values - example program in C
Most Emailed Articles (in C)
Open, Creat, Close, Unlink system calls sample program in C
The C Character Set
Using malloc() Function in C
Simple arithmetic calculations in C
Increment and Decrement Operators in C
Bitwise Logical Operators in C
Using qsort() and bsearch() with strings - example program in C
Formatting with printf in C
Printing a simple histogram in C
Relational and Logical Operators in C
While and For Loops in C
goto and labels in C
getch and ungetch in C
register Variables in C
#if, #elif, #ifndef, #ifdef in C (Conditional Inclusion)