Loop in c
Topics
- what is loop in c?
- Categories of loop
- entry controlled loop
- exit controlled loop
- types of loop
- for loop
- while loop
- do-while-loop
Loop in c
In c a loop is control structure that allows you to repeat a block of code repeatedly based on certain condition. the loop runs as long as certain condition remains true. if the condition become false then loop stops running. loops can be classified into two categories entry controlled loop and exit controlled loop.
two main categories of loops are:
1. entry controlled loop:
in entry controlled loop the condition is checked before the execution, if the condition is false initially then the loop body may not execute. entry controlled loop are for loop and while loop.
2. exit controlled loop:
the entry controlled loop is quite opposite of entry controlled loop because in exit controlled loop the condition is checked after the execution of loop body, in this type of loop the loop body is executed atleast once regardless of the condition, the main type of exit controlled loop is do-while-loop.
1. for loop : in c for loop is a entry controlled loop that means it checks the condition first before execution for loop is a control structure that repeatedly iterates the loop as long as condition remains true. if the condition become false then loops stops iterating.
syntax of for loop:
for (initialization; condition; increment/decrement) {// statement}
initialization: it is the place where you initialize a variable.
condition: this is a condition that is checked before each iteration of loop. if the condition is true then loop continues if it is false then loop stops.
increment/decrement: this part is executed after each iteration of the loop. it increments or decrement the value of variable.
Example :
#include<stdio.h>int main(){for (int i = 1; i <=5; i++ ) {printf("%d\n",i);}return 0;}
In the above example,
• i is initialized to 1
• the loop will iterate as long as the condition (i<=10) i is less then or equal to 10
• after each iteration i will get incremented by 1
Dry Run :
i=1; i<=5 iinitialization condition print i++
i=1 1<=5 true 1 2i=2 2<=5 true 2 3i=3 3<=5 true 3 4i=4 4<=5 true 4 5i=5 5<=5 true 5 6i=6 6<=5 false
Output will be :
1
2
3
4
5
2. while loop : While loop is a entry control loop that means it checks the condition first before the execution of program it repeats the block of code until the condition become false it consist of just a condition.
syntax for while loop:
while (condition) {// code to be executed}
example:
#include<stdio.h>int main(){int i=1;while (i<=5) {printf("%d\n",i);i++;}return 0;}
• In the above example i is initialised to 1
• the loop continues as long as i is less than or equal to 5 inside the loop value of i will get printed after each iteration
• after is each iteration i will get incremented by 1.
Dry run
i<=5 iint i = 1 condition print i++
i=1 1<=5 true 1 2i=2 2<=5 true 2 3i=3 3<=5 true 3 4i=4 4<=5 true 4 5i=5 5<=5 true 5 6i=6 6<=5 false
Output will be :
1
2
3
4
5
do {// code to be executed} while (condition);
Example:
#include <stdio.h>int main(){
int i = 1;
do {printf("%d\n", i);i++;} while (i <= 5);return 0;}
• In the above example: i is initialized to 1.
• The loop body is executed first then it printing the value of i and then incrementing the value of i.
• The condition (i <= 5) is checked after the loop body is executed.
Dry Run
int i = 1 print condition
i=1 1 1<=5 truei=2 2 2<=5 truei=3 3 3<=5 truei=4 4 4<=5 truei=5 5 5<=5 truei=6 6<=5 false
Output will be :
1
2
3
4
5
C program to print multiplication table using for loop
#include<stdio.h>int main(){int n,t;printf("enter any number: ");scanf("%d",&n);for(int i=1; i<=10; i++ ){printf("%d * %d = %d \n",n,i,n*i);}return 0;}