WikiGalaxy

Personalize

Introduction to C++ For Loop

Basic Structure of For Loop:

A for loop in C++ is used to execute a block of code repeatedly for a specified number of times. The basic syntax includes initialization, condition, and increment/decrement.


int main() {
    for (int i = 0; i < 5; i++) {
        cout << i << " ";
    }
    return 0;
}
    

Console Output:

0 1 2 3 4

Nested For Loop

Understanding Nested Loops:

Nested loops are loops within loops. The inner loop will be executed one time for each iteration of the outer loop.


int main() {
    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= 3; j++) {
            cout << i << "," << j << " ";
        }
        cout << endl;
    }
    return 0;
}
    

Console Output:

1,1 1,2 1,3 2,1 2,2 2,3 3,1 3,2 3,3

For Loop with Arrays

Iterating Over Arrays:

For loops are commonly used to iterate over arrays, allowing you to perform operations on each element.


int main() {
    int arr[] = {10, 20, 30, 40, 50};
    for (int i = 0; i < 5; i++) {
        cout << arr[i] << " ";
    }
    return 0;
}
    

Console Output:

10 20 30 40 50

Reverse Counting

Counting Backwards:

For loops can also be used to count backwards by using the decrement operator.


int main() {
    for (int i = 5; i > 0; i--) {
        cout << i << " ";
    }
    return 0;
}
    

Console Output:

5 4 3 2 1

Skipping Iterations

Using Continue Statement:

The continue statement can be used within a for loop to skip the current iteration and move to the next one.


int main() {
    for (int i = 0; i < 5; i++) {
        if (i == 2) continue;
        cout << i << " ";
    }
    return 0;
}
    

Console Output:

0 1 3 4

Breaking Out of a Loop

Using Break Statement:

The break statement is used to exit a loop prematurely before the loop condition is false.


int main() {
    for (int i = 0; i < 5; i++) {
        if (i == 3) break;
        cout << i << " ";
    }
    return 0;
}
    

Console Output:

0 1 2

For Loop with Step Value

Incrementing by Step:

You can control the increment of the loop variable by specifying a step value in the increment section.


int main() {
    for (int i = 0; i < 10; i += 2) {
        cout << i << " ";
    }
    return 0;
}
    

Console Output:

0 2 4 6 8

For Loop with Multiple Variables

Using Multiple Variables:

C++ allows you to initialize and use multiple variables in a for loop, separated by commas.


int main() {
    for (int i = 0, j = 10; i < j; i++, j--) {
        cout << i << "," << j << " ";
    }
    return 0;
}
    

Console Output:

0,10 1,9 2,8 3,7 4,6

Infinite For Loop

Creating Infinite Loops:

An infinite loop runs indefinitely, usually due to a missing condition or a condition that never evaluates to false.


int main() {
    for (;;) {
        cout << "Infinite Loop";
    }
    return 0;
}
    

Console Output:

Infinite Loop Infinite Loop Infinite Loop...

For Loop with Conditional Statements

Combining Conditional Logic:

You can use conditional statements within a for loop to execute different blocks of code based on certain conditions.


int main() {
    for (int i = 0; i < 10; i++) {
        if (i % 2 == 0) {
            cout << i << " is even ";
        } else {
            cout << i << " is odd ";
        }
    }
    return 0;
}
    

Console Output:

0 is even 1 is odd 2 is even 3 is odd 4 is even 5 is odd 6 is even 7 is odd 8 is even 9 is odd

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025