C Programming

Loops

Basics of Loops

A loop is a programming technique used to repeat a part of the program specified number of times or until the condition is satisfied.

There are three types of loop:
1. For Loop
2. While Loop
3. Do-While Loop

The While Loop

A while loop is used to repeat the code based on the boolean condition given. The while loop is generally used when we don't know the number of iteration to be executed. The while loop can be thought of as repeating if statement.

Syntax

while (boolean condition)
{
         // do this
}

Examples

1.Print numbers from 1 to 10.

#include <stdio.h>
int main()
{
  int i = 1;
  while (i <= 10)
        {
    printf("%d\n", i);
    i++;
  }
  return 0;
}

In the above program, we initialize i with 1. In the while loop, we print the value of i and increment it until it is less than or equal to 10.

2.Another way to print number from 1 to 10.

#include <stdio.h>
int main()
{
  int i = 0;
  while (i++ < 10)
    printf("%d\n", i);
  printf("I am outside the while block");
  return 0;
}

In the above program, i is initialized to zero. Inside the while condition, i is incremented first and then compared with 10.
Notice the there are no curly braces to define while block.So, in this case, the statement executed immediately after the while statement is executed repeatedly.

3.A subtle example

Guess the output of given program?

#include <stdio.h>
int main()
{
  int i = 0;
  while (i++ < 10);
  printf("%d\n", i);
  return 0;
}

Notice ; in the while statement. In this case, while block contains nothing. So while loop runs without any statements inside it and control comes out of while loop when i becomes 11.

The For Loop

The for loop is generally used when we know number of iterations to be executed. The for loop contains three parts.

  1. Initializing the loop counter.
  2. Testing the loop counter.
  3. Changing the value of loop counter each time part of the program within the loop is executed.

All these parts are optional.

Syntax

for (initialize counter; test counter; changing counter) 
{ 
    // do this ; 
}

Examples
1.Print numbers from 1 to 10.

#include <stdio.h>
int main()
{
  int i;
  for (i=1; i<=10; i++)
        {
    printf("%d\n", i);
  }
  return 0;
}

2.No initialize counter.

#include <stdio.h>
int main()
{
  int i = 1;
  for (; i<=10; i++)
       {
    printf("%d\n",i);
  }
  return 0;
}

3.No changing counter.

#include <stdio.h>
int main()
{
  int i;
  for (i=1; i<=10; )
       {
    printf("%d\n", i);
    i++;
  }
  return 0;
}

4.No initialize counter and changing counter.

#include <stdio.h>
int main()
{
  int i=1;
  for (; i<=10; )
        {
    printf("%d\n", i);
    i++;
  }
  return 0;
}

5.For loop without curly braces

#include <stdio.h>
int main()
{
  int i = 0;
  for( ;++i<=10;)
    printf("%d\n",i);
  printf("I am out of the loop");
  return 0;
}

In the case, for loop repeats only the statement which is immediately after the for loop statement.

6.Nothing to repeat

#include <stdio.h>
int main()
{
  int i = 0;
  for ( ; ++i<=10; );
    printf("%d\n", i);
  return 0;
}

In this case, notice ; in the for loop.So for loop repeats nothing. As the result, i is incremented and in the end, 11 is printed.

The Do-While Loop

The do-while loop is a variation of the while loop.In which we first executed code in do block and then check the while condition.

Syntax

do
{
    // do this
}
while(condition);

Example

Print numbers from 1 to 10.

#include <stdio.h>
int main(){
  int i =1;
  do{
    printf("%d\n", i);
    i++;
  }while (i<=10);
  return 0;
}

Recent Comments

Feedback

For any feedback,email us at feedback@geeksforgeeks.org

Report a Bug

For any report or bug,email us at support@geeksforgeeks.org