C - For Loop

The for loop is a control flow statement that allows you to repeatedly execute a block of code for a specified number of iterations. It is a compact loop that consists of three parts: initialization, condition, and increment/decrement. 

for (initialization; condition; increment/decrement)
{
    // Code to be executed in each iteration
}
  • The initialization is executed first and is typically used to initialize a loop control variable.
  • The condition is evaluated before each iteration. If the condition is true, the code block within the for loop is executed. If the condition is false, the loop is terminated, and the program proceeds to the next statement after the for loop.
  • After executing the code block, the increment/decrement is executed, typically used to update the loop control variable.
  • The process repeats: the condition is evaluated again, and if it's true, the code block is executed. This continues until the condition becomes false.
for (int i = 1; i <= 5; i++)
{
    printf("%d\n", i);
}

In this example, the for loop is used to print the numbers from 1 to 5. The loop control variable i is initialized to 1, and the loop continues as long as i is less than or equal to 5. After each iteration, i is incremented by 1 using i++. The current value of i is printed in each iteration.