Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6835243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:12:33+00:00 2026-05-26T23:12:33+00:00

What’s the real difference between a foreach and for loop if either can get

  • 0

What’s the real difference between a foreach and for loop if either can get the same job done? I’m learning C++ and apparently there is no foreach loop for its arrays 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:12:34+00:00Added an answer on May 26, 2026 at 11:12 pm

    There is no “foreach” language construct in C++, a least not literally. C++11 introduces something that’s “as good as” a foreach loop, though.

    The traditional for loop has something to do with evaluating conditions and performing repeated operations. It’s a very general control structure. Its most popular use is to iterate over container or array contents, but that’s just a tiny fraction of what you can do with it.

    A “foreach” loop, on the other hand, is explicitly designed to iterate over container elements.

    Example:

    int arr[5] = { 1, 3, 5, 2, 4 };
    
    for (int & n : arr) { n *= 2; } // "for-each" loop, new in C++11
    
    for (size_t i = 0; i != 5; ++i) { arr[i] *= 2; } // "classic" for loop
    

    In the second for, we use a traditional for loop to increment an auxiliary variable i in order to access the container arr. The first, range-based loop does not expose any details of the iteration, but just says “do this and that to each element in the collection”.

    Since the traditional for loop is a very general control structure, it can also be used in unusual ways:

    std::vector<std::string> all_lines;
    for (std::string line; std::cin >> line; all_lines.push_back(line))
    {
      std::cout << "On line " << (all_lines.size() + 1) << " you said: " << line << std::endl;
    }
    

    You can trivially rewrite for(A; B; C) as a while loop:

    {  // scope!
      A;
      while (true && B)
      {
        {  // more scope!
          /* for loop body */
        }
        C;
      }
    }
    

    Edit: I would probably be remiss not to mention the library function template std::for_each from <algorithm>, which in conjunction with lambdas is a very nice and self-descriptive way to iterate over arbitrary ranges (not just entire containers). It has existed since Day 1, but before lambdas it was a show-stopping pain to use.


    Update: I thought of something else that might be relevant here: A “foreach” loop generally assumes that you don’t modify the container. A common type of looping that modifies the container requires the traditional for-loop; as for example in this typical erase pattern:

    for(Container::const_iterator it = v.begin(); it != v.end() /* not hoisted! */; /* no increment */ )
    {
      // do something
      if (suitable_condition)
      {
        v.erase(it++);   // or it = v.erase(it), depending on container type
      }
      else
      {
        ++it;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to loop through a bunch of documents I have to put
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.