Difference between for loop and the range based loop in C

0 votes
The distinction between a for loop and a for each loop in C++ is unclear to me.

I've studied various books and tutorials, but I haven't yet understood how for each loop differs.

Examples of arrays that I've seen appear to imply that for each loop iterates over all of the items at once.

Could it be the unique benefit a range-based loop possesses?
Jul 11, 2022 in C++ by Nicholas
• 7,760 points
671 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

The distinction between a for loop and a range-based for loop is essentially equivalent to the one between goto and a for loop. 

The first is a more broad control flow, whereas the second is more organised. 

Every range-based loop can be expressed as a for loop, and every for loop can be expressed as a goto. 

However, the opposite is not true.

Goto encapsulates the concept of venturing somewhere. 

The concept of repeating is encapsulated by the term loop (which involves jumping back). 

The concept of iterating across a range from start to finish is encapsulated in a range-based loop (which involves repetition).

If you want to iterate over a whole range, a range-based loop is usually more syntactically straightforward and easier to grasp than a for loop. 

A for loop is also easier to grasp than a goto loop. 

This is what distinguishes the structured approach. 

Consider the following examples and decide which one is easiest to understand:

for (auto it = list.begin(), end = list.end(); it != end; ++it) {
    auto& element = *it;
    // do stuff with element
}

for (auto& element : list) {
    // do stuff with element
}

A range-based loop can loop through an entire range. 

A for loop may achieve the same thing. 

A for loop, on the other hand, may do much more: it can iterate indices, iterate forever, iterate until a terminator, and so on.

answered Jul 14, 2022 by Damon
• 4,960 points

edited Mar 5

Related Questions In C++

0 votes
1 answer

What is the difference between public, private, and protected inheritance in C++?

To begin answering that question, let me characterise member accessors in my own terms.  If you already know this, proceed to the section "next:". I'm aware of three types of accessors: public, protected, and private. Let: class Base { public: ...READ MORE

answered Jul 11, 2022 in C++ by Damon
• 4,960 points
862 views
0 votes
1 answer

What is the difference between operator overloading and operator overriding in C++?

Some people use the latter word to ...READ MORE

answered Aug 2, 2022 in C++ by Damon
• 4,960 points
2,087 views
0 votes
0 answers

What is the difference between std::list<std::pair> and std::map in C++ STL?

What distinguishes std::list<std::pair> from std::map? Does the ...READ MORE

Aug 17, 2022 in C++ by Nicholas
• 7,760 points
858 views
0 votes
1 answer

C++ code file extension? What is the difference between .cc and .cpp [closed]

GNU GCC recognizes all of the following ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
1,708 views
0 votes
0 answers

What is the difference between Java and C++?

What is the difference between Java and ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
429 views
0 votes
0 answers

What is the difference between cout, cerr, clog of iostream header in c++? When to use which one?

I looked up the differences between cout, ...READ MORE

Jul 27, 2022 in C++ by Nicholas
• 7,760 points
829 views
0 votes
1 answer

What is the difference between ++i and i++?

To answer your question, the ++i will ...READ MORE

answered Feb 17, 2022 in Others by Aditya
• 7,680 points
878 views
0 votes
0 answers

Is hiding implementation detail Encapsulation or Abstraction?

I have seen some people defining abstraction ...READ MORE

May 6, 2022 in Java by narikkadan
• 63,600 points
3,530 views
0 votes
1 answer

Why would anyone use set instead of unordered_set?

Unordered sets must compensate for their O(1) ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
3,172 views
0 votes
1 answer

What is a smart pointer and when should I use one?

A smart pointer is similar to a ...READ MORE

answered Jun 2, 2022 in C++ by Damon
• 4,960 points
598 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP