Range-based for loop & other variations

It is added in C++ 11, and it is as same as traditional for loop operating over a range. 


Let's see a simple example. 

  for ( for-range-declaration : expression ) statement          
#include <iostream>
using namespace std;

int main() {
  int array[5] = { 1, 2, 3, 4, 5 };
  
  /* Range-based for loop */
  for (int& x : array) {
     x *= 2;
     cout << x << endl;
     //cout << array[x] << endl; // This gives invalid output
     // So, don't use the expression to print
  }
  
  /* Traditional for loop */
  for (int i = 0; i < 5; i++) {
      cout << "arr" << "[" << i << "]" << " " << array[i] << endl;
  }
  
  return 0;
}

Output

2
4
6
8
10
arr[0] 2
arr[1] 4
arr[2] 6
arr[3] 8
arr[4] 10

Pass by reference & value 

Click here to debug.
#include <iostream>
#include <vector>
using namespace std;

int
main ()
{

  //Method 1: using auto
  vector < int >v1 = { 1, 2, 3, 4, 5 };
  cout << "auto i: ";
  for (auto i:v1) {
      ++i;
      cout << i << " ";
  }
  cout << endl << "auto i: vector modification is done" << endl;
  
  //Method 2: Changes will not reflect 
  for (auto i:v1) {
      cout << i << " ";
  }
  cout << endl;

  vector < int >v2 = { 1, 2, 3, 4, 5 };
  cout << "auto& i: ";
  for (auto & i:v2) {
      ++i;
      cout << i << " ";
  }
  cout << endl << "auto& i: vector modification" << endl;
  
  for (auto i:v2) {
      cout << i << " ";
  }
  cout << endl;

  vector < int >v3 = { 1, 2, 3, 4, 5 };
  cout << "auto iterator: ";
  for (auto it = v3.begin (); it != v3.end (); ++it) {
      ++(*it);
      cout << *it << " ";
  }
  cout << endl << "vector for loop: Modification" << endl;

  for (auto i:v3) {
      cout << i << " ";
  }
  cout << endl;

  vector < int >v4 = { 1, 2, 3, 4, 5 };
  cout << "Tradition for loop: ";
 
  for (int i = 0; i < v4.size (); ++i) {
      ++v4[i];
      cout << v4[i] << " ";
  }
  cout << endl << "traditional for loop: Modification" << endl;
  
  for (auto i:v4) {
      cout << i << " ";
  }

  return 0;
}

Output

auto i: 2 3 4 5 6 
auto i: vector modification is done
1 2 3 4 5 
auto& i: 2 3 4 5 6 
auto& i: vector modification
2 3 4 5 6 
auto iterator: 2 3 4 5 6 
vector for loop: Modification
2 3 4 5 6 
Tradition for loop: 2 3 4 5 6 
traditional for loop: Modification
2 3 4 5 6 
Passing the & ensures that changes done inside the for() {} is reflected out of the for loop as well. 

Other loop variations

#include <iostream>

using namespace std;

int main()
{
    for (int i = 1; i < 10; i++) {
        cout << "for() " << i << endl;    
    }
    
    cout << "******" << endl;
    
    for (int i:{ 1, 2, 3, 4, 5, 6, 7, 8, 9}) {
        cout << "for(int i:{}) " << i << endl;    
    }
    
    cout << "******" << endl;
    
    int i = 1;
    loop:
       cout << "loop: " << i++ << endl;    
       if (i < 10) goto loop;
    
    cout << "******" << endl;
    
    i = 1;
    while (i < 10) {
        /* Below line results 2, 3, 4, 5, 6, 7, 8, 9, 10 */ 
        //cout << "while() " << ++i << endl;  
        
        /* Below line results 1, 2, 3, 4, 5, 6, 7, 8, 9 */ 
        cout << "while() " << i++ << endl;
    }
    
    cout << "******" << endl;
    
    i = 1;
    do {
        cout << "do-while() " << i++ << endl;    
    } while (i < 10);
    
    
    return 0;
}

Output

for() 1
for() 2
for() 3
for() 4
for() 5
for() 6
for() 7
for() 8
for() 9
******
for(int i:{}) 1
for(int i:{}) 2
for(int i:{}) 3
for(int i:{}) 4
for(int i:{}) 5
for(int i:{}) 6
for(int i:{}) 7
for(int i:{}) 8
for(int i:{}) 9
******
loop: 1
loop: 2
loop: 3
loop: 4
loop: 5
loop: 6
loop: 7
loop: 8
loop: 9
******
while() 1
while() 2
while() 3
while() 4
while() 5
while() 6
while() 7
while() 8
while() 9
******
do-while() 1
do-while() 2
do-while() 3
do-while() 4
do-while() 5
do-while() 6
do-while() 7
do-while() 8
do-while() 9


Comments