C++ loops


Loops in C++ are used to repeatedly execute a block of code as long as a specified condition is true. They are fundamental for scenarios where you need to perform repetitive tasks, such as iterating over arrays, running calculations multiple times, or processing user input until a certain condition is met. There are several types of loops in C++:

1. for Loop

The for loop is used when you know in advance how many times you want to execute a block of code. It is often used for iteration over arrays or repeating tasks a specific number of times.

Syntax:

for (initialization; condition; update) { // Code to be executed }
  • initialization: Initializes the loop control variable.
  • condition: Evaluated before each iteration. If true, the loop body executes.
  • update: Modifies the loop control variable after each iteration.

Example:

for (int i = 0; i < 5; i++) { std::cout << "Iteration " << i << std::endl; } // Output: // Iteration 0 // Iteration 1 // Iteration 2 // Iteration 3 // Iteration 4

2. while Loop

The while loop is used when you do not know in advance how many times the loop will run, but you want to execute a block of code as long as a condition remains true.

Syntax:

while (condition) { // Code to be executed }
  • The condition is evaluated before each iteration. If it is true, the loop body is executed; if it is false, the loop ends.

Example:

int count = 0; while (count < 3) { std::cout << "Count is " << count << std::endl; count++; } // Output: // Count is 0 // Count is 1 // Count is 2

3. do-while Loop

The do-while loop is similar to the while loop, but it ensures that the loop body is executed at least once, even if the condition is false at the beginning.

Syntax:

do { // Code to be executed } while (condition);
  • The code block executes first, and then the condition is evaluated. If the condition is true, the loop repeats.

Example:

int count = 0; do { std::cout << "Count is " << count << std::endl; count++; } while (count < 3); // Output: // Count is 0 // Count is 1 // Count is 2

Key Differences Between while and do-while

  • while loop: Condition is checked before executing the loop body. If the condition is false initially, the loop body may not run at all.
  • do-while loop: Executes the loop body first, then checks the condition. It always runs at least once.

4. Range-based for Loop (C++11 and Later)

The range-based for loop provides a simpler syntax to iterate over all elements in a collection, such as an array or vector.

Syntax:

for (data_type variable : collection) { // Code to be executed }

Example:

int numbers[] = {1, 2, 3, 4, 5}; for (int num : numbers) { std::cout << num << " "; } // Output: 1 2 3 4 5

Control Statements in Loops

C++ provides special control statements to alter the flow of loops:

  • break: Terminates the loop immediately.
  • continue: Skips the rest of the current iteration and continues with the next iteration.

Example using break and continue:

for (int i = 0; i < 10; i++) { if (i == 5) { break; // Stop the loop if i is 5 } if (i % 2 == 0) { continue; // Skip even numbers } std::cout << i << " "; } // Output: 1 3

Infinite Loops

An infinite loop keeps running without end. This can happen if the condition never becomes false:

Example of an Infinite Loop:

while (true) { std::cout << "This will run forever" << std::endl; }

Infinite loops are sometimes useful, such as in applications waiting for user input or responding to events, but should be used with caution.

Summary

  • for Loop: Used for situations where the number of iterations is known beforehand.
  • while Loop: Used when the number of iterations is not known, and the condition is evaluated before the loop body.
  • do-while Loop: Similar to the while loop but guarantees that the loop body is executed at least once.
  • Range-based for Loop: Introduced in C++11 to simplify iteration over collections.

Loops are fundamental to programming, allowing repetitive tasks to be handled efficiently, which reduces redundancy and improves code readability.