Examples of FOR loop usage in C language

Dear customers, today we would like to draw your attention to the FOR loop definition and it's usage in C programming language.

In software development, term cycle means execution of the same command several times until the condition of the loop is true. From that we can understand that program's code is executed repeatedly in the FOR loop. As you have already noticed, an important element of the loop is the condition that allows to break operations. The program will be repeated continuously until the condition is true. If it is always true, then you will not have an exit point from the loop. Such cycles are called "unconditional".

Instructions and code that are used in the loop are called body of the cycle. A single execution of an instruction is called iteration of a cycle. The number of executions are counted by the iterator, in case if it is used.

In addition, cycles can be distinguished by usage of precondition and postcondition incremental counters. The first one increments variable before it is used and the second after it is value has been read and used in calculations. But the loop is executed at least once in any case.

Now we will see how the cycle FOR works. The syntax of it contains three expressions that have to be separated by semicolons. The first expression denotes the initial operation, a second is condition to check whether it is true, and the third refers to changing or updating some value after each iteration of the program.

Please consider such example of the FOR loop in the code: for (initial code, conditions, operation) { instruction of loop body 1; instruction of loop body 2; … instruction of loop body n; }

Let's see example how this cycle works. We will work in the Linux operating system. Also, let's create a file named sample1.c by using such command in the console: dev@ubuntu:~/examples$ touch sample1.c

Then you may want to write the program and save it into the file. This is a simple code, the purpose of it is the multiplication of the results until the condition is true, namely - index <10. C language program will look as on the image below.

FOR loop

Here we can see that m and index variables of integer type were initially declared . Variable m is set to a value of 1. There are three expressions in the loop that define variable state, and initial value for the variable index, is also set to 1. The second expression defines the exit condition for us, according to which index < 10. The last expression is an indication that with each iteration of the cycle, this variable will be increased by 1. The record index + + is a postfix method. The instruction of multiplying the variable m by the index is executed in the loop. Then we display result on the screen by using the printf function, which we write after the end of the cycle. If operators would not exist after a cycle, then we may not put any braces. Let's compile the program and run it in the console by using the command gcc sample1.c. After that, we can see the result of execution by running the command ./a.out in console. dev@ubuntu:~/examples$ gcc sample1.c dev@ubuntu:~/examples$ ./a.out Result: 362880 dev@ubuntu:~/examples$

Now we want to change the program code to increase the variable m by using postfix method. The variable will be increased by 1 after it's value is read and used in the current statement. for (index = 1; index < 10; ++index) { m = m++ * index; }

Let's look at the example of how the outcome will be changed. dev@ubuntu:~/examples$ gcc sample1.c dev@ubuntu:~/examples$ ./a.out Result: 986410

In case if we want to use the prefix method in our program, then the code may look like this: for (index = 1; index < 10; ++index) { m = ++m * index; }

The output on the screen will be displayed as value: “Result: 1349289”.

Finally, we will show the example of for cycle formation from the first version of our application by using algorithmic representation.

Algorithm

Obviously, the conclusion is that the for loop is an indispensable instrument in writing both simple, large and complex programs. It it is multi functional and useful at the same time. We made the implementation of this cycle by using the C programming language, learned it's syntax and tried to use postfix and prefix methods of increasing variables.

Stay tuned with us, our goal is to help you!

Best regards, DYAMAR Engineering.

Language: 
Category: 
Platform: 

All rights reserved. You may not copy or distribute, electronically or otherwise, any part of this article for any purpose without prior written permission from the DYAMAR Engineering.