techmore.in

C++ Loops

Loops in C++ allow you to repeat a block of code multiple times based on a condition. There are three main types of loops in C++:

  1. for loop
  2. while loop
  3. do-while loop

Each type of loop serves different purposes and is useful in different situations.


1. for Loop

The for loop is generally used when the number of iterations is known beforehand. It consists of three parts: initialization, condition, and update.

Syntax:

cpp
for (initialization; condition; update) { // Code to be executed }
  • initialization: Initializes the loop counter (executed once).
  • condition: The loop runs as long as this condition is true.
  • update: Updates the loop counter after each iteration.

Example:

cpp
#include using namespace std; int main() { // Print numbers from 1 to 5 using a for loop for (int i = 1; i <= 5; i++) { cout << i << " "; } return 0; }

Output:

1 2 3 4 5

In this example, the loop starts with i = 1 and increments i by 1 after each iteration, stopping when i becomes greater than 5.


2. while Loop

The while loop is used when the number of iterations is not known in advance. It checks the condition before executing the loop body. If the condition is true, the loop body is executed. If the condition is false, the loop terminates.

Syntax:

cpp
while (condition) { // Code to be executed }
  • The loop runs as long as the condition is true.

Example:

cpp
#include using namespace std; int main() { int i = 1; // Print numbers from 1 to 5 using a while loop while (i <= 5) { cout << i << " "; i++; } return 0; }

Output:

1 2 3 4 5

In this case, the loop will continue as long as i <= 5. After each iteration, i is incremented.


3. do-while Loop

The do-while loop is similar to the while loop, but the key difference is that the loop body is executed at least once, even if the condition is false. The condition is checked after executing the loop body.

Syntax:

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

Example:

cpp
#include using namespace std; int main() { int i = 1; // Print numbers from 1 to 5 using a do-while loop do { cout << i << " "; i++; } while (i <= 5); return 0; }

Output:

1 2 3 4 5

Here, the code inside the do block is executed at least once before the condition is checked.


Loop Control Statements

1. break statement

The break statement is used to exit the loop prematurely, regardless of the loop condition.

Example:

cpp
#include using namespace std; int main() { for (int i = 1; i <= 10; i++) { if (i == 6) { break; // Exit the loop when i is 6 } cout << i << " "; } return 0; }

Output:

1 2 3 4 5

The loop terminates when i reaches 6 due to the break statement.


2. continue statement

The continue statement skips the rest of the current iteration and jumps to the next iteration of the loop.

Example:

cpp
#include using namespace std; int main() { for (int i = 1; i <= 5; i++) { if (i == 3) { continue; // Skip the rest of the iteration when i is 3 } cout << i << " "; } return 0; }

Output:

1 2 4 5

Here, when i is 3, the continue statement skips that iteration, so 3 is not printed.


Nested Loops

You can place one loop inside another, creating a nested loop. This is commonly used for iterating over multidimensional arrays or creating patterns.

Example of Nested for Loops:

cpp
#include using namespace std; int main() { // Nested for loop to print a 3x3 grid of numbers for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { cout << i << "," << j << " "; } cout << endl; } return 0; }

Output:

1,1 1,2 1,3 2,1 2,2 2,3 3,1 3,2 3,3

Infinite Loops

An infinite loop occurs when the loop's condition is always true. In practice, infinite loops are usually unintended, but they can be created on purpose for scenarios such as continuously waiting for user input.

Example:

cpp
#include using namespace std; int main() { int i = 1; // Infinite loop (condition is always true) while (true) { cout << "This will print forever" << endl; if (i == 5) { break; // Exit the infinite loop when i reaches 5 } i++; } return 0; }

Without the break statement, the loop would run forever.


Summary

  • for loop: Best when the number of iterations is known in advance.
  • while loop: Useful when the number of iterations is not known, and the loop should continue as long as the condition is true.
  • do-while loop: Executes the loop body at least once, even if the condition is false.
  • break and continue: Control the flow of the loop. break exits the loop, and continue skips the current iteration.
  • Nested loops allow loops within loops, often used for working with multidimensional data.