In this video, we'll do a quick overview of looping in
general and specifically in c++.
Looping is also called iteration or repetition.
It's the third basic building block of programming.
After this section of the course, you'll have all you need to begin to
solve very complex problems, using sequence, selection and iteration.
In this course, we're covering c++.
However, all programming languages must support sequence,
selection and iteration.
Once you understand these concepts completely, then it's just a matter
of picking up the syntax of another programming language and you'll
be able to implement solutions to problems in those languages as well.
Loops allow the execution of a statement or block
of statements repeatedly.
And they're made up of two sections.
First, the loop condition which determines when we
iterate and when we don't.
Second is the loop body.
That's the statement or block of statements that will be repeated.
That's it.
Conceptually, it's very simple.
Let's look at a few use cases for looping.
Here are some typical use cases for looping.
We want to execute a loop or repeat statements a specific number of times.
For example, we want to do something 10 times or we ask the user how many
times they wish to do something.
Another use case is iterating one time for each element in a collection.
So suppose we have items in an array or a vector, and we want
to process each of those items.
Other times we want to loop until a specific condition becomes true.
So we loop until we see a specific value then we stop.
Of course, we can also loop forever.
This isn't as common as the other use cases, but operating systems
are an example of programs that loop forever executing system processes.
There are many other use cases and variations of
these existing use cases.
Let's look at the c++ looping constructs now.
C++ has three main looping structures: the for loop, the
while loop and the do while loop.
The for loop has a ranged base variant, which is really great.
First, let's talk about the for loop.
The for loop is typically used when you want to iterate
a specific number of times.
For example you want to loop 10 times or end times, where n
is determined by the program.
The range-based for loop makes it really easy to iterate through
a range of items or through the elements of a collection.
With arrays vectors and other collections, this
is a great loop to use.
The while loop iterates while a specific condition remains true.
When the condition becomes false, we terminate the loop.
In the case of the while loop, the condition is checked at the
beginning of every iteration.
The do while loop is similar to the while loop, except that the condition
is checked after each iteration.
With only these basic loops, we can implement any
iteration pattern we want.
All programming languages have loop constructs.
The c++ loops are clear and concise.
Let's go through the c++ loops one at a time.
Let's start with the for loop in the next video.