For syntax without the counter

Asked

Viewed 66 times

0

I am a beginner in programming and I am studying the C++ language, therefore it is also my first time using this site. But after doing a basic course on the mentioned language, I downloaded a source code to understand some of the practice.

Then reading a part of the code I came across this line of code:

for (Creature* spectator : list) { }

But when I was looking at this type of loop there were some arguments but that was defined as the following example >> For (int i = 0; i < 10; i++) <<

The question is, what does this line want to do? What is this loop structure?

1 answer

0


the for C++ can be used in the traditional way with a counter, specifying the start of the count, the end and the steps (http://en.cppreference.com/w/cpp/language/for) or used as a more readable equivalent to a loop that operates on a variety of values, like all elements in a container, as in the case you quote. In this case, it is counting all the elements of a list. You can see how to use this link: http://en.cppreference.com/w/cpp/language/range-for. This last way, it is used similar to the for_each (http://en.cppreference.com/w/cpp/algorithm/for_each). Since you are learning, I suggest starting with simpler codes. I hope I have helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.