For loop in C programming, step-by-step guide with an easy explanation and detailed source code. C Programming, or in fact, every programming, provides 3 types of loops.
- While Loop
- For Loop and
- Do While Loop
While loop and do while, we have to write initialization, condition, and increment in 3 different lines. But in FOR Loop, all 3 parameters (initialization, condition, increment) come in the same line, separated by semicolons. For loop is helpful while writing complex logic and multiple loops. The functional aspect of all the loops is the same; the only syntax is different.
Let’s see how to write For Loop in an easy and fun way and also understand the difference between different types of loops. This blog demonstrates a while loop in C programming. To learn more about C Programming and its history please read this blog first, “C Programming Complete Guide“.
Table of Contents
Syntax of For loop in C
The syntax of the for loop is:
for (initialization; condition; increment or decrement) { //Statements to be executed repeatedly }
First initialization happens, and the counter variable gets initialized. In the second step, the condition is checked, where the counter variable is tested for the given condition; if the condition returns true, then the C statements inside the body of for loop get executed; if the condition returns false, then the for loop gets terminated and the control comes out of the loop.
After the successful execution of statements inside the body of the loop, the counter variable is incremented or decremented, depending on the operation (++ or – -).
Now suppose you want to print 1 to 9. What will you do? Will you write the printf statement 9 times? NO! it’s too much work. What you will do is you will write a printf statement one time, and you will execute it 9 times. Let’s open our source code again. What we will do is, We will put up the printf statement inside while.
Declaring Variables in C Program
Here, to start, we will create a program that will add numbers. Here we will create integers using the keyword int variables i.
Program Logic to For Loop in C
Write a small program. Now we need to print 1 to 10. Here we have main, declare a variable i. FOR, in curved brackets, we write i=1, then semicolon, then i < 10, then a semicolon, and then increment. In curly brackets, we write printf statement to print i.
main() { int i; for (i=1; i<10; i++) { printf("%d ", i); } }
You can view the source code from the Newtum GitHub repository link over here.
Output:
1 2 3 4 5 6 7 8 9
Look at the code. This is a “FOR” statement. Well, the only difference between a WHILE loop and a FOR loop is the way they are written. In for loop, we have initialization, condition, and increment in the same line separated by semicolons. We have initialization, condition, and increment in one line, and then we have statements in curly brackets.
Let’s open our source code again. Let’s quickly see how this program works. When the program first reaches the FOR statement, it sets the value of i, which is 1. Then it goes for the condition. Since i is less than 10, it goes inside FOR loop. And it printed the value 1, then it went to increment. It increments the value of i becomes 2, and then again it goes to condition. If the condition is true, then it prints the printf statement and then goes to increment. This process will go on until it reaches the value of 10. And the loop will break.
While using for loop in C Programming, please make a note that initialization, this part, is executed only once at the start of the loop and thereafter, only conditions and increments are checked.
Remember, in IF and ELSE statements we don’t have to use curly brackets if we have only one statement. Similarly, in the case of FOR loop or WHILE loop, you can ignore the curly brackets if you have only one statement. So we remove curly brackets and rerun the program. We will get the same output.
Here make a note, in FOR loop in C Language, these two semicolons are a must even if you don’t write condition or increment. Open our last example of FOR loop. We are removing the increment part over here and then we will put the increment part inside the FOR loop statements. Now run the program.
main() { int i; for (i=1; i<10;) { printf("%d ", i); i++; } }
You can watch the video over here, which explains the blog in a simple video with VFX animation.
Output :
1 2 3 4 5 6 7 8 9
The program printed 1 to 9. This was a small variation in the loop that we could have. Please remember these two semicolons in the FOR loop.
Three things are very important in the for loop
- First is the initialization
- Second is Condition
- Third is Increment
Just try to understand the program. First Computer reaches the part of initialization that is i equal to 1. Next, it reaches the while condition. In a while, It checks whether i is less than 10, but currently, the value of i is 1, So 1 is definitely less than 10. The result is True
How to view all topics on C Programming
Watch the full programming series on Amazon Prime free with Amazon Prime Membership.
- Amazon Prime link for the USA: https://www.amazon.com/dp/B07WFYJW6G
- Amazon Prime link for the UK: https://www.amazon.co.uk/dp/B07WFYQZ1G
- For Other Countries: To watch C Programming Content in another country, please visit the URL: https://newtum.com/course-details/c-programming-online.