/

Working with Loops in C: An Introduction

Working with Loops in C: An Introduction

C, the programming language, offers three ways to perform loops: for loops, while loops, and do while loops. These loops allow you to iterate over arrays, but with slight differences. Let’s dive into the details of each loop.

For Loops

The most common way to perform a loop in C is through for loops. Using the for keyword, you can define the looping rules upfront and then provide the block of code to be executed repeatedly. Here’s an example:

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

The block (int i = 0; i <= 10; i++) consists of three parts: the initial condition (int i = 0), the test (i <= 10), and the increment (i++). In this example, we define a loop variable named i (a common convention for loop variables). The variable is initialized at 0 and incremented by 1 in each iteration until it reaches 10. Inside the loop block, you can access the variable i to know the current iteration. Printing 0 1 2 3 4 5 6 7 8 9 10 is achieved this way:

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

For loops can also start from a higher number and decrement the loop variable or increment it by a value other than 1. For example:

1
2
3
4
5
6
7
for (int i = 10; i > 0; i--) {
/* instructions to be repeated */
}

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

While Loops

While loops are simpler compared to for loops as they require less upfront setup. Instead of defining all the loop data at the beginning, you check a condition to determine if the loop should continue. Here’s an example:

1
2
3
while (i < 10) {
/* do something */
}

In this case, i is assumed to be already defined and initialized with a value. However, without incrementing i within the loop, an infinite loop will occur, blocking the program’s execution. To create a correct while loop, ensure you increment i within the loop block:

1
2
3
4
5
6
int i = 0;

while (i < 10) {
/* do something */
i++;
}

Note that there’s an exception to this rule, which we’ll discuss shortly. But before that, let’s understand do while loops.

Do While Loops

Do while loops are similar to while loops but with one key difference. With do while, a block of code executes at least once, regardless of the condition for the loop to continue. Here’s an example:

1
2
3
4
5
6
int i = 0;

do {
/* do something */
i++;
} while (i < 10);

The block within the loop is always executed first, and then, as long as the condition i < 10 remains true, the block is repeated.

Breaking out of a Loop Using break

In all C loops, there’s an option to break out of the loop at any point in time immediately, regardless of the loop’s conditions. This is achieved using the break keyword. It becomes useful when you want to check for a specific condition within a loop. For example:

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

The ability to break out of a loop is particularly useful for while and do while loops. You can create seemingly infinite loops that end when a specific condition occurs. For example:

1
2
3
4
5
6
7
int i = 0;
while (1) {
/* do something */
i++;
if (i == 10)
break;
}

These types of loops are commonly used in C.

tags: [“loops”, “C programming”, “for loops”, “while loops”, “do while loops”]