C For Loop

Used to iterate a specific number of times.

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

  1. Initialization - It initializes the variable for the loop to process.
  2. Condition - Checks the condition before iteration. If false it exits the loop.
  3. Update - It increments or decrements the variable used to initilize the loop.
Example
#include <stdio.h>

int main() {
    // Print numbers from 1 to 5 using a for loop
    for (int i = 1; i <= 5; i++) {
        printf("%d ", i);
    }

    return 0;
}

See how the loop works

  1. int i = 1 initializes the variable i to 1.
  2. i <= 5 is the condition that checks if i <= 5.
  3. i++ increments the value of i after each iteration

Using Arrays

Example
#include <stdio.h>

int main() {
    // Iterating through an array
    int numbers[] = {10, 20, 30, 40, 50};
    
    for (int i = 0; i < 5; i++) {
        printf("%d ", numbers[i]);
    }

    return 0;
}

Learn more about C arrays


Quick Recap - Topics Covered

C For Loop

Practice With Examples in Compilers

The Concepts and codes you leart practice in Compilers till you are confident of doing on your own. A Various methods of examples, concepts, codes availble in our websites. Don't know where to start Down some code examples are given for this page topic use the code and compiler.


Example 1
Example 1 Example 2 Example 3 Example 4 Example 5


Quiz


FEEDBACK