Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

C++ For Loops Explained: Syntax, Workflow, and Practical Examples

What Is a For Loop?

A for loop is a control structure that repeats a block of code a predetermined number of times. It evaluates a condition before each iteration; if the condition is true, the loop body executes, otherwise the loop ends immediately.

In this tutorial you’ll discover:

How a For Loop Executes

The execution flow is best visualized in the diagram below.

C++ For Loops Explained: Syntax, Workflow, and Practical Examples

Step‑by‑step:

  1. The compiler evaluates the initialization once when the loop starts.
  2. The condition is checked.
  3. If true, the loop body runs; then the increment expression updates the loop variable.
  4. Control jumps back to step 2. The loop continues until the condition becomes false.
  5. When false is encountered, the loop exits and execution continues after the loop body.

Note: The condition is re‑evaluated after every iteration, allowing the loop variable to change each cycle.

When to Use a For Loop?

Use a for loop when:

For Loop Syntax

for (initialization; condition; increment) {
    // statements
}

Each part plays a specific role:

Example 1: Basic Counting

#include <iostream>
using namespace std;

int main() {
    for (int x = 0; x < 5; x = x + 1) {
        cout << "X is: " << x << endl;
    }
    return 0;
}

Output:

C++ For Loops Explained: Syntax, Workflow, and Practical Examples

Code walkthrough:

  1. Include <iostream> to access console I/O.
  2. Use the std namespace for convenience.
  3. Define main() as the entry point.
  4. Initialize x to 0; loop while x < 5; increment x each cycle.
  5. Print the current value of x.
  6. Return 0 to indicate successful execution.

Example 2: Calculating Factorial

#include <iostream>
using namespace std;

int main() {
    int x, num, factorial = 1;
    cout << "Type positive number: ";
    cin >> num;
    for (x = 1; x <= num; ++x) {
        factorial *= x;
    }
    cout << "Factorial of " << num << " = " << factorial;
    return 0;
}

Output:

C++ For Loops Explained: Syntax, Workflow, and Practical Examples

Key points in this program:

  1. Prompt the user for a positive integer.
  2. Initialize factorial to 1.
  3. Loop from 1 to num, multiplying factorial by the loop counter each iteration.
  4. Display the result.

Summary

C Language

  1. Mastering C# For Loops: Syntax, Flow, and Practical Examples
  2. Master C++ For Loops: A Step-by-Step Guide
  3. C++ do‑while Loop: Syntax, Practical Examples, and Nested Loops
  4. Mastering C++ Switch‑Case: Syntax, Usage, and Practical Examples
  5. Master C++ Dynamic Array Allocation: A Practical Guide with Code Examples
  6. Mastering std::stack in C++: A Comprehensive Guide with Practical Examples
  7. C++ Structs Explained with a Practical Example
  8. C++ Classes & Objects: A Practical Guide with Code Examples
  9. C++ Polymorphism Explained: Practical Examples & Key Concepts
  10. Mastering std::list in C++: Syntax, Functions & Practical Examples