The `for` loop executes a block of code until the specified condition is false. Use `while` loops when the number of iterations are not known or variable; otherwise, use `for` loops. A common use of `for` loops is array iterations.
It is also known as an 'entry-controlled loop' since the condition is checked before the next iteration. Another example of an 'entry-controlled loop' is a `while` loop.
The block of code around which the <b>for</b> loop iterates is packed inside the curly braces. A <b>for</b> loop is also acceptable without curly braces. The compiler assumes only the 1st statement to be under the <b>imaginary curly braces</b>.
A variable declared inside the curly braces of a <b>for</b> loop is only valid inside that particular for loop. We cannot use a variable declared inside a <b>for</b> loop outside it.
The initialization statement is executed only once. Then, the condition is evaluated. If the condition is false (0), the `for` loop is terminated. But if the condition is true (nonzero), code inside the block of the `for` loop is executed. Finally, the update expression is executed. This process repeats until the condition is false.
The `for` loop checks the value of `i` based on the conditions. If `i` is smaller than `5`, the text will be printed. After printing, the value of `i` is increased by 1. This function will continue until `i` is greater than 4, at which point the loop will stop and exit.
Generally the part which is inside the curly braces is considered as the body of the `for` loop. In certain circumstances we can ignore the curly braces:
- When there is only one line of code inside the loop
- When there is only one `if` statement inside the loop
- When there is only one loop inside the current loop
An infinite loop occurs when the condition will never be met, due to some inherent characteristic of the loop. An infinite loop also called an endless loop, and it is a piece of coding that lacks a functional exit so that it repeats indefinitely.
When "break" statement is executed, the loop stops executing all other statements in it and immediately comes out of the loop.
```C
#include <stdio.h>
int main(void) {
int n = 5,i;
for(i=0;i<n;i++)
{
printf("%d\n",i);
if(n == 3)
break;
}
return 0;
}
```
output:
```output
1
2
```
When "continue" statement is executed, the loop execution is skipped for that time and goes for incrementation/decrementation of the variable, validates the condition and continues the execution of the loop.