For loop in c programming, step by step guide with an easy explanation and detailed source code.
Introduction: For Loop in C
C Programming or in fact every programming provides 3 types of loop,
1. While Loop
2. For Loop and
3. Do While Loop
While loop and do while we have to write initialization, condition, increment in 3 different lines. But in FOR Loop all of 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 loop 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 know more about C Programming and it’s history please read this blog first “C Programming Complete Guide 2020“
You can watch the video over here which explains the blog in simple video with VFX animation.
Syntax of while loop:
The syntax of the for loop is:
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 gets 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.
Business Logic to For Loop
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 semicolon and then increment. In curly brackets, we write printf statement to print i.
You can view the source code from Newtum github repository link over here.
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. Lets quickly see how this program works. When the program first reaches the FOR statement, it sets the value of i, that 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 goes 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 condition 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 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.
The program printed 1 to 9. This was a small variation in the loop that we can have. Please remember these two semicolons in the FOR loop.
Three things are very important in the 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 the 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 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