C++ For Loop Examples

Welcome to The Coding College! In this post, we’ll explore C++ for loop examples to demonstrate its versatility in handling repetitive tasks efficiently. If you’re new to loops, the for loop is a great place to start due to its structured syntax and ease of use.

What is a For Loop?

A for loop in C++ is used to execute a block of code multiple times with a specific condition. It has three parts:

  1. Initialization: Sets the starting point of the loop.
  2. Condition: Determines when the loop should stop.
  3. Increment/Decrement: Updates the loop control variable after each iteration.

Syntax

for (initialization; condition; increment/decrement) {
    // Code to execute
}

Example 1: Print Numbers from 1 to 10

#include <iostream>
using namespace std;

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

Output:

1 2 3 4 5 6 7 8 9 10

Example 2: Sum of First N Natural Numbers

#include <iostream>
using namespace std;

int main() {
    int n, sum = 0;

    cout << "Enter a number: ";
    cin >> n;

    for (int i = 1; i <= n; i++) {
        sum += i;
    }

    cout << "Sum of first " << n << " natural numbers is " << sum << endl;

    return 0;
}

Output (if n = 5):

Sum of first 5 natural numbers is 15

Example 3: Multiplication Table

#include <iostream>
using namespace std;

int main() {
    int number;

    cout << "Enter a number to print its multiplication table: ";
    cin >> number;

    for (int i = 1; i <= 10; i++) {
        cout << number << " x " << i << " = " << number * i << endl;
    }

    return 0;
}

Output (if number = 5):

5 x 1 = 5  
5 x 2 = 10  
...  
5 x 10 = 50

Example 4: Reverse Counting

#include <iostream>
using namespace std;

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

Output:

10 9 8 7 6 5 4 3 2 1

Example 5: Factorial of a Number

#include <iostream>
using namespace std;

int main() {
    int n;
    unsigned long long factorial = 1;

    cout << "Enter a number: ";
    cin >> n;

    for (int i = 1; i <= n; i++) {
        factorial *= i;
    }

    cout << "Factorial of " << n << " is " << factorial << endl;

    return 0;
}

Output (if n = 5):

Factorial of 5 is 120

Example 6: Printing an ASCII Table

#include <iostream>
using namespace std;

int main() {
    for (int i = 32; i <= 126; i++) {
        cout << i << " = " << char(i) << endl;
    }
    return 0;
}

Output (partial):

32 =  
33 = !  
34 = "  
...  
126 = ~

Example 7: Sum of Even Numbers

#include <iostream>
using namespace std;

int main() {
    int n, sum = 0;

    cout << "Enter a number: ";
    cin >> n;

    for (int i = 2; i <= n; i += 2) {
        sum += i;
    }

    cout << "Sum of even numbers up to " << n << " is " << sum << endl;

    return 0;
}

Output (if n = 10):

Sum of even numbers up to 10 is 30

Example 8: Nested Loops to Print a Pattern

Print a pyramid pattern of stars:

*
* *
* * *
* * * *
* * * * *
#include <iostream>
using namespace std;

int main() {
    int rows;

    cout << "Enter the number of rows: ";
    cin >> rows;

    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= i; j++) {
            cout << "* ";
        }
        cout << endl;
    }

    return 0;
}

Output (if rows = 5):

*
* *
* * *
* * * *
* * * * *

Example 9: Printing Prime Numbers in a Range

#include <iostream>
using namespace std;

int main() {
    int start, end;

    cout << "Enter the start of the range: ";
    cin >> start;
    cout << "Enter the end of the range: ";
    cin >> end;

    for (int i = start; i <= end; i++) {
        bool isPrime = true;
        if (i <= 1) {
            continue;  // Skip 0 and 1 as they are not prime
        }

        for (int j = 2; j <= i / 2; j++) {
            if (i % j == 0) {
                isPrime = false;
                break;
            }
        }

        if (isPrime) {
            cout << i << " ";
        }
    }

    return 0;
}

Output (for range 10 to 20):

11 13 17 19

Best Practices for Using For Loops

  1. Use Descriptive Variable Names: Replace generic names like i or j with more meaningful ones if the context is complex.
  2. Avoid Infinite Loops: Ensure the condition will eventually become false.
  3. Minimize Nested Loops: Excessive nesting can reduce readability and performance.
  4. Use break and continue Wisely: Exit or skip iterations when necessary to improve efficiency.

Explore More at The Coding College

Visit The Coding College for more practical C++ tutorials, from beginner to advanced concepts.

What’s Next?

  • Learn about nested loops for more advanced iterations.
  • Explore range-based for loops for simpler iteration in C++11 and beyond.
  • Dive into recursion as an alternative to iterative solutions.

Leave a Comment