Introduction to For Loop in C
A loop is one of the most fundamental logical structures in computer programming. Defining loops in code allows computers to repeat specific tasks. Defining the loop in a computer program is required for various reasons, depending on the tasks performed.
Loops are a common programming concept used to create sophisticated and effective programs. In this article, we will talk about the For loop in the C programming language. This loop basically executes a set of statements in the code repeatedly.
We will be covering the following sections today:
So, without further ado, let’s get started!
For Loop in C
The for loop in C is a repetition control structure that aids in the creation of a loop that runs a section of code repeatedly according to the loop’s specifications.
For Loop Syntax
Following is the syntax for a for loop in C:
Here,
- Initialization – this statement initializes variable(s) and is executed only once.
- testExpression – if the specified test expression is True, the body of the for loop is executed. In case the expression is False, the for loop is terminated right away.
- Update – this statement updates the value of initialized variables and then the test expression is checked again.
Let’s examine the for loop flowchart shown below as an example:
C++ For Loop Implementation Examples
Example 1:
#include \n \n <span class="textrun scxw74690171 bcx0" lang="en-in" xml:lang="en-in" data-contrast="none">\n \n <span class="normaltextrun scxw74690171 bcx0">\n \n <\n \n <span class="normaltextrun spellingerrorv2themed scxw74690171 bcx0">\n \n stdio.h\n \n <span class="normaltextrun scxw74690171 bcx0">\n \n >\n \n <span class="eop scxw74690171 bcx0" data-ccp-props="{"201341983":0,"335559739":0,"335559740":276,"469777462":[916,1832,2748,3664,4580,5496,6412,7328,8244,9160,10076,10992,11908,12824,13740,14656],"469777927":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"469777928":[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]}">\n \n \n int main() { \n int i; \n for (i = 1; i <= 10; i++) \n { \n printf("%d ", i); \n } \n return 0; \n } \n </span class="eop scxw74690171 bcx0" data-ccp-props="{"201341983":0,"335559739":0,"335559740":276,"469777462":[916,1832,2748,3664,4580,5496,6412,7328,8244,9160,10076,10992,11908,12824,13740,14656],"469777927":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"469777928":[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]}">\n \n </span class="normaltextrun scxw74690171 bcx0">\n \n </span class="normaltextrun spellingerrorv2themed scxw74690171 bcx0">\n \n </span class="normaltextrun scxw74690171 bcx0">\n \n </span class="textrun scxw74690171 bcx0" lang="en-in" xml:lang="en-in" data-contrast="none">
Output 1:
In the example illustrated above, we first set the variable i = 1. Then, we state the test expression as i <= 10. Until the test expression is True, the loop will continue to execute.
We define the update statement as i++, which, provided the condition is True, will increase the variable i by 1 each time the loop runs.
To understand how the for loop functions, let’s look at the following table:
Iteration | Initialization | Condition | Action and Update |
---|---|---|---|
1st | i = 1 | True | 1 is printed. i incremented to 2. |
2nd | i = 2 | True | 2 is printed. i incremented to 3. |
3rd | i = 3 | True | 3 is printed. i incremented to 4. |
4th | i = 4 | True | 4 is printed. i incremented to 5. |
5th | i = 5 | True | 5 is printed. i incremented to 6. |
6th | i = 6 | True | 6 is printed. i incremented to 7. |
7th | i = 7 | True | 7 is printed. i incremented to 8. |
8th | i = 8 | True | 8 is printed. i incremented to 9. |
9th | i = 9 | True | 9 is printed. i incremented to 10. |
10th | i = 10 | True | 10 is printed. i incremented to 11. |
11th | i = 11 | False | The loop is terminated. |
Example 2:
We will find the sum of n-natural numbers using for loop in C –
#include <stdio.h>\n \n <stdio.h>\n \n \n \n int main() { \n \n int n, i, sum = 0; \n \n \n \n printf("Enter a positive integer: "); \n \n scanf("%d", &n); \n \n \n \n for (i = 1; i <= n; ++i) { \n \n sum += i; \n \n } \n \n \n \n printf("Sum: %d", sum); \n \n return 0; \n \n } \n \n \n \n \n \n </stdio.h>
Output 2:
We have set the three variables num, i, and sum = 0 in the code above. The user enters a value for the variable num. The number up to which we want to find the sum of natural numbers will be this value.
- The variable i is then initialized to 1.
- To make sure that the loop runs from 1 to num, specify the test expression as i <= num.
- The update statement includes the notation i++, which increases i by 1 with each iteration.
The sum is reported when i = 11 and the test expression returns False:
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 = 91
The for loop then comes to an end.
Best-suited IT & Software courses for you
Learn IT & Software with these high-rated online courses
Infinite For Loops
The for loop in C will run indefinitely until it runs out of memory if the test expression is always True. Consider the following code example:
// infinite for loop
#include \n \n <iostream>\n \n <iostream>\n \n using namespace std; \n \n \n \n int main() { \n \n \n \n for(int i = 1; i > 0; i++) { \n \n cout << i << " "; \n \n } \n \n \n \n return 0; \n \n } \n \n \n \n \n \n </iostream>
Output 3:
To avoid running infinite loops, the test expression in the loop must be correct. As a beginner in programming, you could ask yourself a few questions to help you work with loops –
- What do I want this loop to do?
- How many times do I want the loop to run?
- At what point should my loop stop?
Based on this, you can construct your for loop accordingly. ?
Endnotes
I hope that this article was useful in helping you comprehend how the for loop is created and used in C. Additionally, we covered infinite for loops. Explore our C articles to find out more about the language and consolidate your knowledge of the fundamentals.
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio