For Loop in C

🔄 The For Loop in C Programming

Master the art of repetitive execution

The for loop in C is a control flow statement that allows you to repeatedly execute a block of code a specified number of times. It's commonly used when the number of iterations is known beforehand.

📝 Basic Syntax

for (initialization; condition; update)
{
    // Code to be executed in each iteration
}

🚀 Initialization

This part is executed only once at the beginning of the loop. It initializes the loop control variable.

🔍 Condition

This is the test condition. The loop continues to execute as long as this condition is true.

⬆️ Update

This part is executed after each iteration of the loop. It's typically used to update the loop control variable.

💡 Practical Example

#include <stdio.h>
int main() {

    for (int i = 1; i <= 5; i++)
    {
        printf("%d ", i); // Print the value of i
    }

    return 0;
}

📤 Output:

1 2 3 4 5

🔍 How It Works

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 (int i = 1)
  • The loop continues as long as i is less than or equal to 5 (i <= 5)
  • The printf statement inside the loop prints the value of i in each iteration
  • After each iteration, the loop control variable i is incremented by 1 (i++)
  • The loop repeats until the condition becomes false, and then the program continues with the next statement after the for loop

Post a Comment

0 Comments