Loops in C language:
What are loops?
Loops are used for performing a task again and again in other words loops are used for checking number of iterations performed.
There are 3 main types of loops in c language.
- for loop
- while loop
- do while loop
We are going to do for loop in this article with some couple of problems.
For executing a loop there are mostly three main steps
- Increment or Decrement operator.
It provides the starting value to the variable to start the loop or the value from where the loop should be started.
It is used for checking the condition if the condition is true than the process is executed until the condition becomes false.
3.Increment or Decrement Operator:
It is used for incrementing or decrementing the value variable each time after the loop is executed
(i++) - Increases the value of variable by 1.
(i+=1) - Increases the value of variable by 1.
(i=i+1) - Increases the value of variable by 1.
(i=i+2) - Increases the value of variable by 2.
(i--) - Decreases the value of variable by 1.
(i-=1) - Decreases the value of variable by 1.
(i=i-1) - Decreases the value of variable by 1.
(i=i-2) - Decreases the value of variable by 2.
General Process Flow:
The loop first intialize then checks the condition after the condition is true it executes the code written in it and then goes for incrementing or decrementing the value of variable.
for(initialization ; condition ; increment)
code to be executed if the condition is true;
- There will be no semicolon after round brackets.
- The code for the for loop is written in curly brackets.
- There should always be semicolon between initialization, condition and the operator.
Example Code 1:
Write a program to print even numbers by accepting number of terms from user.
printf("\n enter the number of terms");
printf("\n the even numbers are");
for(i=2 ; i<=n*2 ; i=i+2)
The above codes is written in code blocks
enter the number of terms
the even numbers are
In the above code we have declared two integer variables n and i
n- stores the value of number of terms
i- used for executing the loop
As per the syntax of for loop we intialised the for loop from i=2 because as the first even number is 2.
For condition we have multiplied the value of n by 2 because we need to print 5 even numbers starting from 2 so they are 2,4,6,8,10 so we need the condition i<=10 thats the reason.
In case of incrementing we have incremented the value of i by 2 because we need to add 2 after every even number for getting the next even number. So after adding 2 to i whose value is 2 we get 4.
This goes on and on till the condition is false that is the value of i is greater than 10. After reaching 10 the next value of i after incrementing becomes 12 now when the loop checks the condition as it is false the loop stops executing and i comes out of the loop with a final value 12.
Example Code 2:
Write a program to print table of number entered by user.
printf("\n enter the number");
printf("\n the table is");
for(i=1 ; i<=10 ; i++)
printf("\n %d * %d = %d",n,i,r);
enter the number
the table is
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
In the above code we can see that the loop is initiated from 1 and the condition is i should be less than or equal to 10 also we are incrementing the loop by 1 so it is clear that the loop is going to be executed 10 times starting from i=1 till i=10 and we are calculating the result of multiplication by multiplying n by i and storing the result in r at the same time we print the statement for the table as the value of r changes every time after each iteration.
So for every value of i there is a different value of r and the loop keeps on printing till the condition fails. So when the value of i is greater than 10 it stops printing the table as the condition fails.
If you want to study the further topic of while and do while loop you can click on the link given below
If you want to study the previous topic of nested if statement you can click on the link given below
If you have any doubts please comment down below.