When we write a program for different software or hardware applications, there are some commands or tasks that we need to repeatedly execute until we need them or till that statement is false. Have you ever thought about how programmers program their code to do a specific task multiple times? Do they write the specific code for that specific program several times to get it iterated till it is required? If we do this manually, it would be easy if we had to iterate it a few times, say 2 or maybe 3, but if we needed hundreds of iterations, it could become a hurdle for us to write the same program a hundred times. This is where for loops come to the rescue. These loops allow for iteration of the specific program several times with every increment in their condition statements until it is required.
Procedure
This guide is going to give you a detailed explanation of the definition of “for loops.” Why do we need them? What is the syntax that we are required to know before implementing for loops? And where we may use or implement the for loops in our program. We will perform different cases of for loop on different types of examples in the guide.
Syntax
Let’s first discuss the syntax for the for loop since we need to know about it before practically executing it in the program. The for loop has the three basic statements, or the conditions on which it is executed, and they are as follows:
{
// body of the program that needs to be iterated several times
}
Condition 1, is the basic step where we first declare the index of the for loop with some data type, e.g., integer, and the initialization value mostly equals zero. Then we come forward to condition 2, which states that till what value of iteration the body of the loop should be executed. Condition 3, which is an increment of either 1 step or as per our requirement that we have to iterate the program till we reach to the final set value of iterations in condition 2.
Example # 01
We will learn in this example how we can execute a simple loop for a program to get our hands on the syntax of the “for loop”. For this, we need to create a new project in the Visual Studio C and then add the project to the C directory by saving it with the .c extension. Then we will import the required library as
This is the header file that will allow the program to include all the information related to reading the input and displaying the output. In the main, we will declare and initialize the for loop as described above, e.g., for (condition1 =; condition 2=; condition 3 ++;) {}. For condition 1, we will declare a variable “i” with data type “int” and initialize it by assigning it the numeric value “0”.
In condition 2, we will use the already declared variable “i” and assign it a value less than equal to “10” since we want our program in the body of the for loop to be iterated 10 times. Then we will define condition 3 with the increment of plus one as “ i++”. In the body of the for loop, we will write the function to simply print these values of index “i” and come out of the loop and return zero, then exit the main loop as well.
int main ()
{ int i;
for (i = 0; i < 10; i++)
{
Printf ("%d ", i);
}
return 0;
}
We have implemented the above example in which we have written a program that needs to be executed 10 times and display the values of the index “i” as the output.
Example # 02
The previous example showed how we can execute a simple for loop to print the values of the index. This example is going to show us how we can take the input (as a number) from the user and then find the first ten multiples of that number. Let’s assume we want the user to pick a number “2” so that our program can find its first ten multiples. To do so, import the header file as
After the above step, in the main function, we will initialize a variable with a data type integer and assign it some value, “1”. Then we will initialize another variable “user_input_num” having int as its data type. We will assign this variable a value equal to “0” and read the input from the user for this number by calling the method scanf () and passing the address of this variable as “& user_input_num” in the arguments of hthe scanf () method.
Then we will run a for loop with the already declared and initialized variable “a=1” ‘a’ is less than equals to 10 since we want only the first ten multiples to display in the output, with the increment in the index ‘a’ as “a++” and print the index ‘a’ by multiplying it with the number stored and read in ‘user_input_num’ as “a*user_input_num”. This program is practically executed in the C language as follows:
The output first displays the user’s input number. After reading and writing the number read from the user input program then, it enters the loops, where it first multiplies the current value of the index ‘a’ with the number that is being input from the user and then displays them. This process is executed repeatedly after every increment in the value of the index till the applied limit on the index iterations reaches a value of less than 10. When index ‘a’ reaches the value of 9, it will exit the for loop by returning ‘0’ in the output. Hence, only the first ten multiples of the user input number will be displayed in the output.
Conclusion
For any programs that need to be executed several times, we use the for loops. Instead we manually write the programs multiple times. This saves time and energy expenditure and gives more robustness in terms of accuracy to the program. In the article, we have given a detailed explanation of the syntax and practical execution of the for loops for the various cases.