The code below works correctly. It checks for the letter 'd'. Iterations stop if the word has a 'd' in it.
My question is, does this mean that behind the range-based syntax is actually a for loop? Like the range-based syntax is an alias for it? Or are there differences in the range-based loop. For instance, could I put more complicated code in the block, like function calls?
its a for loop, note the for in the front :) You can do anything in the loop body that you can normally do in any other loop body.
what it really is, is pointer/iterator masked syntax sugar. Its hiding the details in the syntax but effectively its using a pointer to each item and handing it to you (as a copy or a reference).
much like function parameters, you usually want a constant reference (for anything larger than an integer) when it does not change (in your example c does not change) and a non-const reference if it does change (eg if you said c= value; in your code). Without the reference, it makes a copy of the data, which can be rather slow if the data is an object.
Note that in the range-based loop, the end of range sentinel is evaluated only once, before the synthesised for loop.
> For instance, could I put more complicated code in the block, like function calls?
Yes, you could; as long as the code does not modify end of range.
We use the range-for-loop for simple loops over all the elements of a sequence looking at one element at a time. More complicated loops, such as looking at every third element of a vector, looking at only the second half of a vector, or comparing elements of two vectors, are usually better done using the more complicated and more general traditional for-statement
Stroustrup in 'Programming: Principles and Practice Using C++'