In the programing language, loops play an important role in conducting the programs efficiently. Manual execution requires a lot of time that causes the operating system to slow down the speed of its tasks to be performed. In this article, we will discuss using one of the commonly used loops that is for-loop.

Syntax

This loop is utilized when the number of iterations is known. The first part is to initialize the variable; the second contains the condition to show the iteration number. The third part is to increment. In the body of a loop, we can apply conditions, etc.

For (initialize variable; condition; increment)

{

  Body of for loop

}

Example 1

To explain the concept of for loop, you need to have any text editor in Ubuntu. We will use the one that is present by default. We will write the c code and will save this file. We have used a single condition of the For loop. An array of float values is introduced here. If we want to print all these values, we will use for loop in this situation. It will start from the zero indexes and till it reaches the 8th index.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-468.png" data-lazy- height="285" src="data:image/svg xml,” width=”603″>

To obtain the output of the code, we write in the file. We will utilize the terminal to execute the command of output. We first compile the code and then execute it. For compilation, we will need a compiler, and that is GCC.

-o is used to save the content and open it in an output file.

After compilation, the output is obtained by using the below-appended command.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-469.png" data-lazy- height="205" src="data:image/svg xml,” width=”567″>

The output shows 9 values as the loop goes to the 8th index. As they are only 4 elements in the array, proceeding 5 elements will be either 0 or in a negative number. But all of these are in decimal notation.

Example 2:

In the past example, we have seen a single condition initiated by for loop. But in this example, we have introduced more than one condition in a single loop. Two variables that are first declared in the code are initiated inside the loop. Then there exist two conditions that are used with OR logic pipe symbols. This means either one or both conditions can be successful. If one is true, then proceed further.

For (a=70, b= 30: a>20 || b<5; a = a5, b )

After applying the condition, a mathematical operation is performed here. The present value of one variable subtracts 5 from it, and the value is stored back after that one variable is incremented.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-470.png" data-lazy- height="285" src="data:image/svg xml,” width=”610″>

Mostly, these sorts of arithmetic operations are performed inside the body. Moving towards the output, the same approach to get output is utilized by using the GCC compiler. Where the command contains an output file and the file of the c program.

$ GCC –o file2 file2.c

$ ./file2

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-471.png" data-lazy- height="219" src="data:image/svg xml,” width=”514″>

The output shows 10 results. As the difference between the values of a and b is 40, so by incrementing the variable b, when the value of 40 is attained, the loop stops working because after that condition is not true anymore.

Example 3

This example contains the for loop inside another for a loop. This type of presentation is known to be a nested loop. To execute these loops, we need two variables used in separate loops, unlike the previous example. Both initial values are the same, but the conditions of both are different. The outer loop will iterate 4 times, whereas the inner loop will iterate for 6 times.

For (int a=1; a<5; a )

For(int b=1; b<7; b )

As the first loop will run 5 times. It means for each round; the inner loop will run 7 times. In the body of the loop, we have utilized a condition statement, “if-statement.” If this condition is satisfied, then the output will be shown.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-472.png" data-lazy- height="361" src="data:image/svg xml,” width=”643″>

To achieve the desired output, we will first compile and execute the command.

$ GCC –o file3 file3.c

$ ./file3

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-473.png" data-lazy- height="111" src="data:image/svg xml,” width=”465″>

From the output, you can see that we will get 4 results only when both the values of variables are the same. This is because the outer loop has to iterate 4 times as both loops start from the same number, that is, one. So, the values will be the same after each 6 times completion of the inner loop.

Example 4

In this example, for loop is utilized to coordinate directly with the user. We will apply mathematical operations to the values provided by the user. Firstly you will take the sum of all the numbers, and then the average will be calculated through a formula. Consider the below-given code from the file. For loop is used in such a way.

5 values are required from the user. As you can see in this line, one thing is different from the other for loops in the iteration section. Generally, the variable current value is iterated with one more value. But in this code, first, the value is iterated and then added to the variable. Moving forward, in the body of the loop, the values from the user are obtained through;

Printf (“Enter #%d: “, a);

This will show the message to the user to enter the value.

This code is used to save the entered number in the variable. And both the formulas for addition and average are:

Sum = sum num;

Avg =sum/5;

The average formula contains the sum of all the numbers and then divides them with the total number.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-474.png" data-lazy- height="395" src="data:image/svg xml,” width=”624″>

Now, we will see how the output will be shown in the terminal. As we compile the code and execute it

$ GCC –o file4 file4.c

$ ./file4

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-475.png" data-lazy- height="59" src="data:image/svg xml,” width=”430″>

From the output, you will see that as you execute both commands, the first line is shown only; when you enter the number, it will proceed further, and in the next line, the number is asked to enter until it attains the limit of 5. It is shown below.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-476.png" data-lazy- height="110" src="data:image/svg xml,” width=”440″>

After entering the last number, the result will be shown when you press the tab: the whole sum and the average calculated.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-477.png" data-lazy- height="167" src="data:image/svg xml,” width=”517″>

Example 5

This example will again input the values from the user and then calculate the minimum and the maximum number in all of them. Here the first value that is entered is assigned as a minimum and maximum value. Then this number will compare with all other numbers entered by the user. As the first value is already received, then for loop will start from the second index.

In the body of for loop, the values are entered from the user. After this, the conditional statements will be used to compare the numbers. If-statement is used. Firstly, the comparison is for the maximum number. So, the entered number is compared with the number we have assigned maximum at the start. The same logic is applied for the minimum number.

If (num > max)

{max =num;

}

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-478.png" data-lazy- height="524" src="data:image/svg xml,” width=”706″>

Now compile and then execute the command

$ GCC –o file5 file5.c

$./file5

The same output methodology will be done here. After getting all the numbers one by one, the output will be shown after calculations.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-479.png" data-lazy- height="186" src="data:image/svg xml,” width=”441″>

Conclusion

In this article, the examples of the for-loop are discussed in different aspects to enhance your knowledge in using this loop in your programs.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Author-Image-150×150.jpg6100f093f15d8.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.