properties of loop in c++ and how it works

Properties of Loop in C++ and How It Works

Repetition: A loop allows a set of statements to be executed repeatedly based on a condition.

Initialization: A loop initializes the loop control variable before starting the execution of the loop.

Condition: The loop condition is evaluated before each iteration. If the condition is true, the loop body is executed; otherwise, the loop is terminated.

Iteration: After the loop body is executed, the loop control variable is updated, and the condition is re-evaluated for the next iteration.

Termination: The loop terminates when the condition becomes false, and the program continues with the next statement after the loop.

Working Example:

#include <iostream>
using namespace std;

int main() {
    int count = 0;

    while (count < 5) {
        cout << "Count: " << count << endl;
        count++;
    }

    return 0;
}

In the example above, the loop initializes the variable count to 0. Then, the condition count < 5 is checked. If true, the loop body is executed, and the count is incremented. This process continues until the condition becomes false.