Loops are used when a certain task needs to be repeated a number of times or until a certain condition is satisfied. Sometimes you need to repeat statement or group of statements many times. This repetitive task is done by loop statements.
There are mainly three types of loops in C language.
We discuss each loop in details with syntax, example, and flowchart.
Why use loops:
Suppose that we have to print tables from 2 to 10, then you have to write code for each table 10 times.
But this is not feasible. It will increase the length of the code.
Therefore, in C programming language we can use loops.
By using loop we can write 10 times of code in one time.
- Display even numbers from 1 to 100.
- Displaying table of 2 to table of 10.
- Displaying numbers from 1 to 100.
These loop can be further categorized as:
- Pre-tested loops: In this type of loop, first the condition is checked then the loop will be executed.
In this category, there are two loops.
Example: while loop and for loop.
- Post-tested loop: In this type of loop, first the loop is executed at least once and then condition is check.
Example: do-while loop.