In this video, we'll learn about infinite loops.
An infinite loop is often called an endless loop.
An infinite loop is a loop whose control expression always evaluates
to true, so it iterates over and over.
Usually, infinite loops are mistakes by the programmer.
If you have an infinite loop, it's pretty easy to know since
the program does something or does nothing over and over again.
So we find and fix the problem.
However, sometimes infinite loops are written as infinite loops on
purpose, and programmers use break statements as strategic places to
eventually break out of the loop.
There are cases where this makes sense and could be justified.
However, writing an infinite loop and using break statements to
terminate the loop is usually bad practice and should be rewritten
so the loop condition expression is descriptive of what the loop is doing.
Here are a couple of examples of infinite loops that are
appropriate and in fact necessary.
Infinite event loops are commonly used in event-driven programs
such as those you find on mobile devices and embedded systems.
In these programs, the program loops forever listening for mouse
clicks, mouse movements, touches and so forth and reacting to them.
This continues as long as the program is running.
Another example of an endless loop is an operating system.
An operating system is constantly looping handling input/output,
handling resources and so forth.
It only shuts down when you shut down your computer.
Let's see a few examples of infinite loops in c++.
First, let's look at an infinite for loop.
Remember that all three expressions in the for loop are optional.
So if you omit all three, you get just two semicolons.
By definition in c++ that's an infinite loop.
The output statement will be displayed as long as the program is running.
Let's look at a while loop.
Here's an example of an infinite while loop.
It's probably exactly what you were thinking of.
While true will always be true.
And the output statement will execute over and over again.
Of course, we don't have to use the true keyword.
We can use an expression that's always true.
So if we say while 10 is less than 12, since 10 is always less than 12,
it'll loop forever, you get the idea.
Finally, let's look at an infinite do while loop.
Again, the loop condition will always be true.
So the output statement repeats forever.
Okay, so what.
Why would i ever do this?
Well, you might write an event loop like this but more often you see code
that looks more like the following.
Note that in this example, we have an infinite loop since
while true will always be true.
However, we have a strategically placed break statement
that terminates the loop.
Let's walk through the example.
Inside the loop, we declare a character variable named again.
We ask the user if they want a loop again and read the response as a
character into the variable again.
If again is uppercase n or lowercase n, then we break
and terminate the loop.
If they enter anything else, we loop again.
This is an example of an endless loop that might be okay.
However, best practice suggests that the loop condition be more informative
of when the loop iterates if.
I was doing the code review of this code, I would
refactor it to do just that.
However, you do see code like this out there, and now you know how it works.
That completes this video.
In the next video, we'll see how we can nest loops one within the other.