Online Book Reader

Home Category

Professional C__ - Marc Gregoire [9]

By Root 1128 0
and C++11 adds one new looping mechanism.

The while Loop

The while loop lets you perform a block of code repeatedly as long as an expression evaluates to true. For example, the following completely silly code will output “This is silly.” five times.

int i = 0;

while (i < 5) {

std::cout << "This is silly." << std::endl;

i++;

}

The keyword break can be used within a loop to immediately get out of the loop and continue execution of the program. The keyword continue can be used to return to the top of the loop and reevaluate the while expression. Both are often considered poor style because they cause the execution of a program to jump around somewhat haphazardly, so they should be used sparingly. The only place where you have to use break is in the context of the switch statement as seen earlier.

The do/while Loop

C++ also has a variation on the while loop called do/while. It works similarly to the while loop, except that the code to be executed comes first, and the conditional check for whether or not to continue happens at the end. In this way, you can use a loop when you want a block of code to always be executed at least once and possibly additional times based on some condition. The example that follows will output “This is silly.” once even though the condition will end up being false.

int i = 100;

do {

std::cout << "This is silly." << std::endl;

i++;

} while (i < 5);

The for Loop

The for loop provides another syntax for looping. Any for loop can be converted to a while loop and vice versa. However, the for loop syntax is often more convenient because it looks at a loop in terms of a starting expression, an ending condition, and a statement to execute at the end of every iteration. In the following code, i is initialized to 0; the loop will continue as long as i is less than 5; and at the end of every iteration, i is incremented by 1. This code does the same thing as the while loop example, but to some programmers, it is easier to read because the starting value, ending condition, and per-iteration statement are all visible on one line.

for (int i = 0; i < 5; i++) {

std::cout << "This is silly." << std::endl;

}

The Range-Based for Loop

The range-based for loop is a fourth way of looping which has been added to the C++11 standard. It allows for easy iteration over elements of a list. This type of loop will work for C-style arrays, initializer lists (discussed in Chapter 9) and any type that has a begin() and end() function returning iterators. Since all containers in the STL have these begin() and end() functions, they will all work with this new range-based for loop. STL containers and iterators are explained in more details in Chapter 12.

The following example first defines a list of four integers. The range-based for loop will then iterate over every element in this list and increment its value by 2.

int arr[] = {1, 2, 3, 4};

for(auto& i : arr) {

i += 2;

}

Arrays

Arrays hold a series of values, all of the same type, each of which can be accessed by its position in the array. In C++, you must provide the size of the array when the array is declared. You cannot give a variable as the size — it must be a constant value. C++11 relaxes this requirement a little by allowing the size of the array to be a constant expression or constexpr, which is discussed in Chapter 9. The code that follows shows the declaration of an array of 10 integers followed by a for loop that initializes each integer to zero.

int myArray[10];

for (int i = 0; i < 10; i++) {

myArray[i] = 0;

}

The preceding example uses a for loop to initialize every element to zero. This can also be accomplished with the following one-liner.

int myArray[10] = {0};

Note that this is only possible if you want to initialize all values to zero. For example:

int myArray[10] = {2};

This line will only fill the first element in the array with the value 2 and will fill all the other elements in the array with the value 0.

The preceding example shows a one-dimensional array, which you can think of as a line of integers, each with its own

Return Main Page Previous Page Next Page

®Online Book Reader