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 )
   {

   }
*/

 

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *