0% found this document useful (0 votes)
35 views2 pages

For (Declaration: Range) Statement Range Declaration: Jump Statements

The range-based for loop iterates over elements in a range like arrays, containers, and strings. It declares a variable to represent each element in the range. An example uses a range-based for loop to iterate over the characters in a string, outputting each character in brackets. The variable type is automatically deduced from the element type.

Uploaded by

icul1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views2 pages

For (Declaration: Range) Statement Range Declaration: Jump Statements

The range-based for loop iterates over elements in a range like arrays, containers, and strings. It declares a variable to represent each element in the range. An example uses a range-based for loop to iterate over the characters in a string, outputting each character in brackets. The variable type is automatically deduced from the element type.

Uploaded by

icul1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Range-based for loop

The for-loop has another syntax, which is used exclusively with ranges:

for ( declaration : range ) statement;

This kind of for loop iterates over all the elements in range, where declaration declares some
variable able to take the value of an element in this range. Ranges are sequences of elements,
including arrays, containers, and any other type supporting the functions begin and end; Most
of these types have not yet been introduced in this tutorial, but we are already acquainted with at
least one kind of range: strings, which are sequences of characters.

An example of range-based for loop using strings:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// range-based for loop
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string str {"Hello!"};
for (char c : str)
{
std::cout << "[" << c << "]";
}
std::cout << '\n';
}
[H][e][l][l][o][!]


Note how what precedes the colon (:) in the for loop is the declaration of a char variable (the
elements in a string are of type char). We then use this variable, c, in the statement block to
represent the value of each of the elements in the range.

This loop is automatic and does not require the explicit declaration of any counter variable.

Range based loops usually make also use of type deduction for the type of the elements
with auto. Typically, the range-based loop above can also be written as:
1
2
for (auto c : str)
std::cout << "[" << c << "]";


Here, the type of c is automatically deduced as the type of the elements in str.


Jump statements
Jump statements allow altering the flow of a program by performing jumps to specific locations.
The break statement
break leaves a loop, even if the condition for its end is not fulfilled. It can be used to end an
infinite loop, or to force it to end before its natural end. For example, let's stop the countdown
before its natural end:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// break loop example
#include <iostream>
using namespace std;

int main ()
{
for (int n=10; n>0; n--)
{
cout << n << ", ";
if (n==3)
{
cout << "countdown aborted!";
break;
}
}
}
10, 9, 8, 7, 6, 5, 4, 3, countdown aborted!

You might also like