For loop in C++

Program is not usually limited to a linear or sequence or instructions.

During it process it may bifurcate, repeat code or take decition.

That is why C++ also provided a control structures so that tells our program what has to be done and when or in which circumstances.

Loops have as purpose to repeat a block of code in certain number of times or while a condition is fulfilled.

If you know how many times you want to repeat a block of code then use forloops and if you don't have idear how many times the block of code will runs use while loops.

Syntax:

for (initialization; condition; increment/decrement) {
  // code block to be executed
}

Syntax:

  1. The initialization is where the variable is set and it only executed once.
  2. The condition part is where we checked. If it is true the loop continues, otherwise the loop ends and statement is skipped (not executed).
  3. The statement is what will be executed inside block of code.
  4. The increase/decrease field is where we set the keep updating our initial varibale.

Let's take an example, where we will print numbers from 0 to 4.

// For Loop
#include <iostream>
using namespace std;

int main(){
  
  for (int i = 0; i < 5; i++) {
    cout << i << "\n";
  }
  return 0;
}

Example explained

We set the value of the variable at the initialization befor loop start (int i = 0).

Then in the condition part we set a rules for the loop to run where (i is to be less than 5), if its true, then the loop will keep repeat itself. if it is false, then the loop will end.

The increment/decrement is doing it is job whenever the block of code executed. Means to increase (i++) or decrease (i--).

Nested Loops

Placing a loop inside another loop is called Nested loop

In programmers convention we called the loop that placed inside Inner loop and the one that takes it Outer loop.

The "inner loop" will executed one time for each iteration of the "outer loop":

// For Loop
#include <iostream>
using namespace std;

int main(){
  // Outer loop
  for (int i = 1; i <= 2; ++i) {
    cout << "Outer: " << i << "\n"; // Executes 2 times

    // Inner loop
    for (int j = 1; j <= 3; ++j) {
      cout << " Inner: " << j << "\n"; // Executes 6 times (2 * 3)
    }
  }
  return 0;
}

The foreach Loop

The "for-each"loop is another type of loops which is used to iterate over an Array elements or other data structures.

The main differences between "for-each" loop and normal for loop is that it will not provide you the index of the elements.

Syntax:

for (type variableName : arrayName) {
  // code block to be executed
}

Let's take an example, where we will print the list of an a Array elements using "for-each loop".

// For-each Loop
#include <iostream>
using namespace std;

int main(){
  string myNames[5] = {"Aisha", "Muhammad", "Bilal", "John", "Musa"};
  for (string name : myNames) {
    cout << name << "\n";
  }
  return 0;
}

Real World Example

The below example, is a countdown program

// For Loop
#include <iostream>
using namespace std;

int main(){

  for (int n=10; n>0; n--) { 
    cout << n << ", "; 
  } 
  cout << "FIRE!\n";
  return 0;
}

The below example, will print only Even numbers.

// For Loop
#include <iostream>
using namespace std;

int main(){
  for (int i = 0; i <= 10; i = i + 2) {
    cout << i << "\n "; 
  } 
  return 0;
}

The below example, will print only Odd numbers.

// For Loop
#include <iostream>
using namespace std;

int main(){
  for (int i = 1; i <= 10; i = i + 2) {
    cout << i << "\n "; 
  } 
  return 0;
}

The below example, is a program to print multiplication table for a specific numbers.

// For Loop
#include <iostream>
using namespace std;

int main(){
  int number = 3;
  int i;

  // Print the multiplication table for the number 2
  for (i = 1; i <= 10; i++) {
    cout << number << " x " << i << " = " << number * i << "\n";
  }
  return 0;
}