Loops in C Programming

Let’s explore different loops in C Programming. The programs we have developed so far in this series of C Programming. These programs use either a sequential or decision control statements to solve task. The real beauty of computer programs lies in its ability to execute block of code repeatedly. This involve repeating some portion of code the program either specified number of times or until particular condition met. These kind of repetitive actions can be performed using loops in C Programming. There are three types of loops in C Language: For loop, While loop, Do-while loop.

learn-embedded-system

For Loop in C Programming

for loop is used to execute a set of statements repeatedly until a particular condition is satisfied. We can say it an open ended loop.

Syntax of For Loop
for(initialization; condition ; increment/decrement)
{
  // Statements;
}

In for loop we have two semicolons, one after initialization and second after condition. In this loop we can have more than one initialization or increment/decrement, separated using comma operator. for loop allows only one condition.

Example Program: for loop

In for loop, loop is executed until condition becomes false.

#include <stdio.h>

int main()
{
    int i;

    for(i=0; i<10; i++)
    {
        printf("%d \n", i);
    }
}

The output of this program is given below:

Output of for loop in C Programming
Output of for loop in C Programming

While Loop in C Programming

while loop can be addressed as an entry control loop. It is completed in 3 steps.

  1. Variable Initialization. (e.g. int x=0)
  2. Condition ( e.g. while(x<=10) )
  3. Variable Increment or Decrement ( x++ or x– or x=x+2 )
Syntax of while Loop
variable initialization;
while (condition)
{
 statements;
 variable increment/decrement; 
}

Some basic rule of while loop: The statements within the while loop would keep getting executed till the condition being tested remains true. When the condition becomes false, the control passes to the first statement that follows the body of the while loop. The condition being tested may use relational or logical operators as shown in the following examples: while(x<=20), while(x>=20 && x<=50), while(x>=10 || (i>=10 && i<=20) || y==15).

Almost always, the while must test a condition that will eventually become false, otherwise the loop would be executed forever, indefinitely. It is not necessary that a loop counter must only be an int. It can even be a float.

Example Program: while loop
#include <stdio.h>

int main()
{
  int i=0;

  while(i<5)
  {
    printf("%d \n", i);
    i = i+1;  // i++
  }
}

The output of this program is given below:

Output of while loop
Output of while loop

Do-While Loop in C Programming

do..while loop in C Programming is useful for things that want to loop at least once.  Here is Syntax:

do
{
 statement1...
 statement2...
}
while(condition)

In some situations it is necessary to execute body of the loop before testing the condition. Such situations can be handled with the help of do-while loop. do statement evaluates the body of the loop first and at the end, the condition is checked using while statement.

Example Program: do..while loop
#include <stdio.h>

int main()
{
  do
  {
    printf("It works...!!! \n");
  }while(5<1);
}

The output of this program is given below:

Output of do-while loop
Output of do-while loop

In above program, the printf() would be executed once, since first the body of loop is executed and then the condition tested, 5<1 condition false so loop terminate and go to next statement. Always make sure and don’t forget about semicolon at the end of while. Lets have a look at one more example:

#include <stdio.h>

int main () {

   int i = 1;

   do {
      printf("value of i: %d\n", i);
      i++;
   }while(i<10);
}

Now we know how loops in C Programming being used to perform repetitive task. We will find loops in most of our future lessons. When and which loop among for, while, do-while to be used in program will depend on individuals requirements/choice. In next lesson we will learn about control statements i.e. breaks and continue in C Programming Language.

Get Free Courses & Webinars
You'll receive only high quality learning material, tips & tricks
I agree to have my personal information transfered to MailChimp ( more information )
We respect your privacy

About Umesh Lokhande

Umesh Lokhande holds a Master degree in Scientific Instrumentation from University of Applied Sciences Jena, Germany. and has previously worked at Orbotech, Alere Technologies etc. Umesh is also a founder and first author of BINARYUPDATES.COM

Login

Register | Lost your password?