Loop in C Language
A loop statement in C language allows us to execute a statement or group of statements multiple times until a specific condition satisfies. It is also known as iteration. The looping enables us to simplify the complex problems instead of writing the same code again and again. We can execute the same code for a finite number of times using a condition.
Essential components required for a loop
- Declare a counter variable with an initial value
- Specify a condition that check with the value of the counter
- Write statement(s) to be executed by iteration
- Increment/decrement the value of the counter
1. while loop - The while loop in c is used to repeat a statement or group of statements while a given condition is true. It checks the condition before executing the loop body. It is also called a pre-tested loop.
2. for loop - The for loop is used to execute a sequence of statements multiple times and abbreviates the code that manages the loop variable. This loop is also called as a pre-tested loop. It is better to use for loop if the number of iteration is known in advance.
3. do...while loop - The do-while loop is more like a while statement, except that it tests the condition at the end of the loop body and continues until a given condition satisfies. It is also called post-tested loop. The do-while loop is used when it is necessary to execute the loop at least once.
4. nested loops - The nested loops are used to execute one or more loops inside any other while, for, or do..while loop.
Flow Chart of Loop
ADVERTISEMENT