Site icon DataFlair

Range-Based for Loop in C++

Master C++ with Real-time Projects and Kickstart Your Career Start Now!!

Program 1

// Ranged based for loop
#include<iostream>
#include <vector>
#include <map>
using namespace std;
int main()
{
   system("cls");


int nums[] = {1, 2, 3};  

for (int &x : nums) 
{
       x =x*2;
}
for(int m:nums)
{
    cout<<m<<endl;
}



//     vector<string> names = {"Niral", "Aarna", "Ankita","Amit","AAkash"};
    
//     for (string name : names) 
//     {
//        cout << name << "\n";
//    }

// map<int, string> students = {{1, "Vikas"}, {2, "Amit"}};

// for (const auto &pair : students) 
// {
//     cout << pair.first << " => " << pair.second << "\n";
// }




    // int ar[]={10,20,30,40,50};
    // int sum=0;
    // for(int m:ar)
    // {
    //    cout<<m<<endl;
    //   sum=sum+m;
    // }
    // cout<<"Sum of elements: "<<sum;
 return 0;    
}
/*
   for(int i=1;i<=10;i++)
   {}
    
   for( data type variable_name : collection )
   {

   }
*/

 

Exit mobile version