What are the for Loop ???
for Loop is a count controlled loop in the sense that the program knows in advance how many times the loop is to be executes.
It allow us to specify three things about a loop in a single line,
- Setting a loop counter to an initial value.
- Testing the condition about loop counter.
- Increasing or decreasing the loop counter’s value.
Syntax,
Let’s understand the for Loop with Syntax,
for (initialization; condition; increment/decrement) {
// code block to execute
}
- for -> is a reserved word.
- initialization -> is usually an assignment expression where in a loop control variable is initialized.
- increment/decrement -> it modifies the value of the lop control variable y a certain amount.
C Language Notes
Example:
#include <stdio.h> int main() { // For loop example for (int i = 1; i <= 5; i++) { printf("Iteration %d\n", i); } return 0; }
Explanation about Example:
In this example, we used for Loop and then we initialize a “i” variable with 1. And then check the condition is i is less than or equal to 5. And with the each increment value of i will be increased.