Loop in C++

A Loop is a programming construct that allows you to repeatedly execute a block of code until a certain condition is met. It helps in automating repetitive tasks and controlling the flow of a program. Loops are fundamental in programming and are used to iterate over data structures, process large amounts of data, and implement various algorithms.

Main Components of a Loop:

  • Initialization: This step is performed before the loop starts and initializes any necessary variables.
  • Condition: The loop condition is checked before each iteration. If the condition is true, the loop body is executed. If it is false, the loop terminates, and program execution continues with the code following the loop.
  • Update : After each iteration, the loop variables are updated to ensure progress toward the loop termination condition.

Loops provide powerful mechanisms for controlling the flow of a program and performing repetitive tasks efficiently. They allow you to process large amounts of data, implement algorithms, iterate over arrays or collections, and more. Choosing the appropriate loop type depends on the specific requirements and nature of the problem you are trying to solve.

for loop

The for loop is commonly used when the number of iterations is known in advance. It consists of three components: initialization, condition, and increment/decrement.

Syntax
for (initialization; condition; increment/decrement) {
    // code to be executed in each iteration
}
Example
#include<iostream>
using namespace std;
int main() {
    for (int i = 1; i <= 5; i++) {
        cout << "Iteration: " << i << endl;
    }
    return 0;
}

In this example, the for loop will execute the code block five times. The loop variable i is initialized to 1, the condition i <= 5 is evaluated before each iteration, and i is incremented by 1 (i++) after each iteration. The output will display the iteration number from 1 to 5.

while loop

The while loop is used when the number of iterations is not known in advance. It repeatedly executes a block of code as long as the specified condition is true.

Syntax
while (condition) {
    // code to be executed in each iteration
}
Example
#include<iostream>
using namespace std;
int main() {
    int i = 1;
    while (i <= 5) {
        cout << "Iteration: " << i << endl;
        i++;
    }
    return 0;
}

In this example, the while loop will execute the code block as long as i is less than or equal to 5. The loop variable i is initialized outside the loop, and within the loop, i is incremented by 1 (i++). The output will display the iteration number from 1 to 5.

do-while loop

The do-while loop is similar to the while loop, but it executes the code block at least once, even if the condition is initially false. It then checks the condition and continues to execute the loop as long as the condition remains true.

Syntax
do {
    // code to be executed in each iteration
} while (condition);
Example
#include<iostream>
using namespace std;
int main() {
    int i = 1;
    do {
        cout << "Iteration: " << i << endl;
        i++;
    } while (i <= 5);
    return 0;
}

In this example, the do-while loop will execute the code block at least once. It will continue to execute as long as i is less than or equal to 5. The output will display the iteration number from 1 to 5.