What is iteration in the C.

Wiki

introduction

Suppose you want to develop a program that has to repeat the same commands several times at one point. How do you do it? One possibility would be to rewrite these commands in the source text over and over again. This is not a problem if these commands are only to be processed twice. If these instructions should be repeated much more often, for this possibility a big mess. Source texts become infinitely long and the risk of programming errors increases dramatically. That is why so-called loops were developed. These are also called iterations. There are three types of loops: the head-controlled, the foot-controlled and the counting loop.

Head controlled loops

The head-controlled loop is the most commonly used. It is structured as follows:

while ([expression]) {/ * statement to be repeated * /}

The word while means something like 'while'. From this one can deduce the following sentence: “As long as the condition is fulfilled, repeat the instruction.” Similar to if-then-else instructions, loops can contain both an instruction to be repeated and a block of curly braces to be repeated. The following example demonstrates a program that outputs the text "Hello World" ten times:

#include int main () {int i = 0; / * loop counter * / while (i <10) / * pass-through condition * / {printf ("Hello World \ n"); i = i +1; / * increment of the counter * /} return0;}

In this example a loop counter is required. This is a variable, usually of the type int, which ensures that the expression becomes false. Otherwise it would lead to an endless loop, which can only be ended by manual termination. Names like i, j and k are very often used for loop counters. Therefore you should not use these identifiers for other variables. From a purely mathematical point of view, the expression is completely wrong. From a programming point of view, however, this is a valid instruction. As a reminder, the single equal sign is an assignment operator in C. It follows that the variable i is increased by one. Now a more detailed description of the process:

while (i <10)

This statement checks whether the expression is true. For this, the loop counter i must be less than 10. After checking, the loop executes the instructions in the block. So the output of "Hello World" and the increase of the counter. Then the program jumps back to the loop head and checks the expression again. If the expression is wrong, i.e. the value is ten, the program jumps to the next line after the loop. Since the counter is initialized with the value 0, the loop runs ten times.

All of this may sound a little complicated, but once you understand it, it's very simple.

Here is a summary of the head-controlled loop: The program checks the printout in the loop head. If this is true, it executes the instructions in the body, after which it jumps back to the loop head and checks the expression again. This continues until the expression is wrong.

The head-controlled loop should always be used when it is possible that its content should not be executed at all.

Foot-operated loops

The foot-operated loop is rarely used, but it can still be extremely useful. The aim of the foot-controlled loop, in contrast to the head-controlled loop, is to execute its content at least once. The syntax looks like this:

do {/ * statement * /} while ([expression]);

Note: There must be a semicolon at the end of the loop. As you can see, this loop starts directly with the statement and only then checks the expression. So the statement is executed at least once. This query is no longer in the head, but in the foot. Hence it is a foot controlled loop. Here, too, is an example that outputs "Hello World" ten times:

#include int main () {int i = 0; do {printf ("Hello World \ n"); i = i +1;} while (i <10); return0;}

Here is a description of the process: The keyword “do” makes it clear to the program that this is a loop body. The instructions will now be carried out. The expression is now checked in the foot, i.e. while (i <10). If this is true, the program jumps back to the line that appears after the do.

Counting loops

The counting loop is a special form of head-controlled loops. The syntax looks like this:

for ([counter initialization]; [expression]; [final statement]) {/ * statement * /}

This looks pretty confusing now, but it's pretty easy. Here, too, is an example that outputs "Hello World" ten times.

#include int main () {int i; for (i = 0; i <10; i ++) printf ("Hello World \ n"); return0;}

There are three different types of commands within the head of the loop. The first part, which is only executed once, initializes the counter, the second part is the expression and the third part is the instruction that is executed last in a run. Now the sequence: The counter is initialized. The printout is then checked. The output is carried out and only now is the counter incremented. It is not necessary to write all three types of statements. For example, you could also use the loop:

#include int main () {int i = 0; for (; i <10; i ++) printf ("Hello World \ n"); return0;}

It is noticeable that there is a semicolon at the beginning of the head. Although it is not necessary to write the instructions, there must be at least two semicolons in the head. The following loop would be an infinite loop, but it could still be compiled:

for (;;) printf ("Hello World \ n");

break

Sometimes it doesn't really make sense to just end a loop with the expression. This can happen, for example, if another termination condition has not been defined in the header. In this case the 'break' command helps. If this command appears, the loop is aborted.

continue

It can happen that certain instructions in a loop body should not be executed every time it is run. The keyword 'continue' helps here. It's a jump instruction. If this command is evaluated, the loop jumps back to the beginning. It does not matter whether a few instructions follow.

Nested loops

Loops can of course also be nested. The following example program outputs a plus sign in ten columns and ten lines:

#include int main () {int i, j; for (i = 0; i <10; i ++) / * outer loop * / {for (j = 0; j <10; j ++) / * Inner loop * / printf ("+"); printf ("\ n");}}

The head of the outer loop is evaluated first, then that of the inner loop. Now the inner loop is done completely, then the rest of the outer loop. The same thing happens nine more times afterwards. Loops can be nested even deeper, but this happens less often. The more often a loop is nested, the greater the risk of an infinite loop. For example, if a loop is nested four times, the source code can quickly become confusing if each of these loops contains a break. The goto command, which is covered in a later chapter, can help.


| Author discussion