Technology and General Blog

In the programing language, loops perform an essential part in conducting the packages proficiently. Handbook execution needs a large amount of time that results in the working system to slow down the velocity of its responsibilities to be carried out. In this posting, we will discuss working with a person of the usually made use of loops that is for-loop.


This loop is used when the amount of iterations is recognized. The to start with part is to initialize the variable the second is made up of the ailment to clearly show the iteration number. The third element is to increment. In the human body of a loop, we can apply conditions, etc.

For (initialize variable situation increment)

  Entire body of for loop

Instance 1

To describe the idea of for loop, you want to have any text editor in Ubuntu. We will use the a single that is existing by default. We will create the c code and will preserve this file. We have used a solitary condition of the For loop. An array of float values is released right here. If we want to print all these values, we will use for loop in this scenario. It will start off from the zero indexes and till it reaches the 8th index.

To acquire the output of the code, we generate in the file. We will make the most of the terminal to execute the command of output. We initial compile the code and then execute it. For compilation, we will need a compiler, and that is GCC.

-o is made use of to help you save the written content and open up it in an output file.

Immediately after compilation, the output is received by using the underneath-appended command.

The output demonstrates 9 values as the loop goes to the 8th index. As they are only 4 factors in the array, proceeding 5 elements will be both or in a unfavorable range. But all of these are in decimal notation.

Instance 2:

In the past example, we have viewed a single affliction initiated by for loop. But in this case in point, we have released far more than just one situation in a solitary loop. Two variables that are first declared in the code are initiated within the loop. Then there exist two ailments that are used with OR logic pipe symbols. This means both a single or equally problems can be thriving. If one is correct, then progress further more.

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.

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

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.

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

$ GCC –o file3 file3.c

$ ./file3

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.

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

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.

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

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

Now compile and then execute the command

$ GCC –o file5 file5.c


The very same output methodology will be accomplished here. Following obtaining all the figures one particular by a person, the output will be demonstrated right after calculations.


In this posting, the illustrations of the for-loop are mentioned in various facets to boost your expertise in working with this loop in your courses.

Leave a Reply

Your email address will not be published. Required fields are marked *