🔥 NEW! Get my book The Developer's Guide to Having a Successful Blog

In this book I teach you all I know about starting and growing a blog.
Launch promo: 25% OFF until tomorrow! And a special challenge for you!


C offers us three ways to perform a loop: for loops, while loops and do while loops. They all allow you to iterate over arrays, but with a few differences. Let’s see them in details.

For loops

The first, and probably most common, way to perform a loop is for loops.

Using the for keyword we can define the rules of the loop up front, and then provide the block that is going to be executed repeatedly.

Like this:

for (int i = 0; i <= 10; i++) {
  /* instructions to be repeated */
}

The (int i = 0; i <= 10; i++) block contains 3 parts of the looping details:

  • the initial condition (int i = 0)
  • the test (i <= 10)
  • the increment (i++)

We first define a loop variable, in this case named i. i is a common variable name to be used for loops, along with j for nested loops (a loop inside another loop). It’s just a convention.

The variable is initialized at the 0 value, and the first iteration is done. Then it is incremented as the increment part says (i++ in this case, incrementing by 1), and all the cycle repeats until you get to the number 10.

Inside the loop main block we can access the variable i to know at which iteration we are. This program should print 0 1 2 3 4 5 5 6 7 8 9 10:

for (int i = 0; i <= 10; i++) {
  /* instructions to be repeated */
  printf("%u ", i);
}

Loops can also start from a high number, and go a lower number, like this:

for (int i = 10; i > 0; i--) {
  /* instructions to be repeated */
}

You can also increment the loop variable by 2 or another value:

for (int i = 0; i < 1000; i = i + 30) {
  /* instructions to be repeated */
}

While loops

While loops is simpler to write than a for loop, because it requires a bit more work on your part.

Instead of defining all the loop data up front when you start the loop, like you do in the for loop, using while you just check for a condition:

while (i < 10) {

}

This assumes that i is already defined and initialized with a value.

And this loop will be an infinite loop unless you increment the i variable at some point inside the loop. An infinite loop is bad because it will block the program, nothing else can happen.

This is what you need for a “correct” while loop:

int i = 0;

while (i < 10) {
  /* do something */

  i++;
}

There’s one exception to this, and we’ll see it in one minute. Before, let me introduce do while.

Do while loops

While loops are great, but there might be times when you need to do one particular thing: you want to always execute a block, and then maybe repeat it.

This is done using the do while keyword, in a way that’s very similar to a while loop, but slightly different:

int i = 0;

do {
  /* do something */

  i++;
} while (i < 10);

The block that contains the /* do something */ comment is always executed at least once, regardless of the condition check at the bottom.

Then, until i is less than 10, we’ll repeat the block.

Breaking out of a loop using break

In all the C loops we have a way to break out of a loop at any point in time, immediately, regardless of the conditions set fo the loop.

This is done using the break keyword.

This is useful in many cases. You might want to check for the value of a variable, for example:

for (int i = 0; i <= 10; i++) {
  if (i == 4 && someVariable == 10) {
    break;
  }
}

Having this option to break out of a loop is particularly interesting for while loops (and do while too), because we can create seemingly infinite loops that end when a condition occurs, and you define this inside the loop block:

int i = 0;
while (1) {
  /* do something */

  i++;
  if (i == 10) break;
}

It’s rather common to have this kind of loops in C.