Why? for ( char : string)

I am trying to understand how this loop condition works.
(char & c : str1) I have never seen this before. I have only been exposed to (init, cond, loop) in setting up a 'for' loop.
I realize it is creating a char variable and looping as many times as the string is long. What is this type of condition setup called so I can learn more about it?
Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13

	std::string str1;

	std::cout << "Enter a string: " << std::endl;
	getline(std::cin, str1);

	for (char & letter : str1)
	{
		std::cout << letter << std::endl;
	} 

	std::cin.get();
	return 0;
Its called a "for each" loop and it has been around in quite a few programming languages for a long time, and I believe it was added to c++ 11 and is now being used much more often.
C++11 calls it range-based for loop syntax.
See http://en.cppreference.com/w/cpp/language/range-for
Range-based for loops uses the keyword for but it should be thought of as a different type of loop.

C++ has four different types of loop statements:

• while loops
• do-while loops
• for loops
• range-based for loops
Topic archived. No new replies allowed.