From the course: Complete Guide to C++ Programming Foundations
Unlock this course with a free trial
Join today to access over 24,700 courses taught by industry experts.
While loops - C++ Tutorial
From the course: Complete Guide to C++ Programming Foundations
While loops
- [Instructor] Now let's get to know loops. The simplest loop in C++ is the while loop. It has two forms. While which evaluates a condition prior to iterating and do while, which always runs the first iteration and then evaluates the condition at the end of the loop body. Let me show you how to print all the elements of a vector with a while loop. In line nine, we have the declaration of the vector named player scores, and just like a raise, vectors can be initialized at declaration with a list. Here I wrote five values in ascending order. Then in line 11, we have an iterator declaration and initialization. This iterator will be used to traverse the vector. Notice that iterators are usually defined within their classes, even generic classes. This iterator type belongs to the vector of integers class. I have initialized it at playerScores.begin, which returns an iterator pointing to the first element of the vector. I named it scorePTR as in score pointer. Now with a type name as…