C Programming Language Tutorial

Variables and Data Types

Input/Output

Looping and Selection Structures

Array

Functions

Preprocessing Command

Pointer

Structure

File Operations

Important Knowledge

for Loop (for Statement) in C Programming Language

The for loop in the C programming language is a control structure that allows you to repeat a block of code a specific number of times. It consists of an initialization expression, a loop condition, and an update expression. This tutorial will explain how to use the for loop in C, including its syntax, a simple example, and common use cases.

  • For loop syntax

The for loop in C has the following syntax:

for (initialization; condition; update) {
    // code to be executed repeatedly
}
  • initialization: This is an expression that initializes the loop variable(s). It is executed only once when the loop starts.
  • condition: This is a test condition that is checked before each iteration of the loop. If the condition is true, the loop body will be executed. If it is false, the loop will exit.
  • update: This expression is executed after each iteration of the loop body. It typically updates the loop variable(s) to prepare for the next iteration.
  • Simple for loop example

Here is a simple example of a for loop that prints the numbers from 1 to 10:

#include <stdio.h>

int main() {
    for (int i = 1; i <= 10; i++) {
        printf("%d\n", i);
    }

    return 0;
}

In this example, the loop variable i is initialized to 1, the condition checks whether i is less than or equal to 10, and the update expression increments i by 1 after each iteration. The loop body simply prints the value of i.

  • Common use cases for the for loop

The for loop is often used for:

  • Iterating over arrays or other data structures
  • Performing a specific number of iterations
  • Repeating a block of code until a condition is met

Here's an example of a for loop iterating over an array:

#include <stdio.h>

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int length = sizeof(arr) / sizeof(arr[0]);

    for (int i = 0; i < length; i++) {
        printf("arr[%d] = %d\n", i, arr[i]);
    }

    return 0;
}

In this example, the for loop iterates over each element of the array arr and prints its index and value.

In summary, the for loop in C is a control structure that allows you to repeat a block of code a specific number of times. It consists of an initialization expression, a loop condition, and an update expression. The for loop is commonly used for iterating over arrays or other data structures, performing a specific number of iterations, and repeating a block of code until a condition is met.

  1. Using For Loops for Iteration in C Language:

    #include <stdio.h>
    
    int main() {
        // Using for loop for iteration
        for (int i = 1; i <= 5; ++i) {
            printf("Iteration %d\n", i);
        }
    
        return 0;
    }
    
  2. C Code Examples with For Loop:

    #include <stdio.h>
    
    int main() {
        // For loop example
        for (int i = 0; i < 5; ++i) {
            printf("%d ", i);
        }
    
        return 0;
    }
    
  3. Nested For Loops in C Programming:

    #include <stdio.h>
    
    int main() {
        // Nested for loops example
        for (int i = 1; i <= 3; ++i) {
            for (int j = 1; j <= 3; ++j) {
                printf("(%d, %d) ", i, j);
            }
            printf("\n");
        }
    
        return 0;
    }
    
  4. Controlling Loop Execution with Break and Continue in C:

    #include <stdio.h>
    
    int main() {
        // Using break and continue in for loop
        for (int i = 1; i <= 5; ++i) {
            if (i == 3) {
                break;  // Exit the loop when i is 3
            }
    
            if (i == 2) {
                continue;  // Skip the rest of the loop and go to the next iteration when i is 2
            }
    
            printf("Iteration %d\n", i);
        }
    
        return 0;
    }
    
  5. Looping Through Arrays with For in C:

    #include <stdio.h>
    
    int main() {
        // Looping through an array with for loop
        int numbers[] = {1, 2, 3, 4, 5};
    
        for (int i = 0; i < 5; ++i) {
            printf("%d ", numbers[i]);
        }
    
        return 0;
    }
    
  6. Common Mistakes and Pitfalls with the For Loop in C:

    #include <stdio.h>
    
    int main() {
        // Common mistake: forgetting to increment the loop variable
        for (int i = 0; i < 5; /* missing increment */) {
            printf("%d ", i);
        }
    
        return 0;
    }