- In this video we're going to talk about one
small detail, but often it can mean the difference
between a working program and a non-working one,
meaning the need to break out of loops.
Sometimes, either deliberately or due to
a programming error, a loop may not end,
causing an infinite cycle that drains
the computer resources in addition
to the fact that it prevents the program
from working properly.
There is a command in C called break
that lets you break out of a loop
or halt the loop regardless
of what else is happening.
Here's one example.
There is a loop, a while loop, that reads
a character one by one until the end of line
unless that character is x.
If the character read is x, the while loop stops.
So if that while loop continues to read characters
other than x, it will absolutely never stop.
We want to make sure that there is some condition
that will make this loop stop.
Let's look at another example in code
and see how this works.
In this example I created a simple function
called testBreak.
Notice line 25, while(1).
This is a typical type of while when we
are interested purposely to create an infinite loop.
While(1) is always true, which means there's
absolutely nothing to stop this loop.
Sometimes you will want to have a while(1),
an infinite loop, because you have other constructs
that will cause some events that eventually
will cause the loop to halt.
In our case, we have a loop counter
and we have a number, n,
that's being entered by the user.
The loop counter simply counts the number
of cycle the while loop executes,
and this entering of the number in line 27,
the scanf, is the only real action
that this loop is performing.
So unless otherwise told, this loops keeps
asking the user to enter a number,
the user enter a number, and so on,
and on, and on, and on, seemingly to infinity
because while(1) is always true.
However, here's a little trick to prevent you
from doing things that will drain computer resources
and help you prevent inadvertent errors.
Line 28 has two safety measures.
We could have said, let's just say,
if n equals zero then stop the loop.
This would mean if the number typed equals zero,
the loop will stop.
Well, if all we wanted is that, we could have
said while n does not equal zero, do the cycle.
However, we want to do something
a little more than that.
We want to have an infinite loop,
so we want an additional safety.
Therefore, we have the loop counter.
The loop counter is set to zero initially,
incremented at every cycle of the loop,
hence the term loop counter, but in line 28
I am testing whether this value is equal
or larger than 10.
If yes, then we break in line 29.
So because this is an infinite loop
and I want to ensure that there are some conditions
under which I can actually stop this loop,
I put those two safeties.
So in case my prescribed input of zero
for some reason doesn't happen,
I have the second safety that I am
not allowing this loop to go over 10.
Naturally, for a 10, it's not a real drain
of any kind on a computer, so I'm using 10
as an illustration because I want to create
an example that will run within the time
allocated for this video.
But imagine you have a very complex program
and you want to make sure that the program
performs the number of cycles required,
but just in case, let's say you don't want this
to go over 1,000, or over 10,000,
or whatever you decide the limit would be.
In this case, no matter what you do,
this particular while loop will stop after nine.
So let's put this to test.
First let's test the zero stop measure.
I'm going to type a number and another number
and a zero, and the loop stops.
There's nothing else for me.
I'm hitting the keyboard and nothing happen.
We see the word here terminated, which is
what the application under Eclipse tells you.
Let's run this again.
This time I'm not going to enter zeroes,
I'm just going to enter just one.
You can see the cycles keep increasing,
cycle eight, cycle nine, cycle 10, and nothing.
I'm typing and you see the note here, terminated.
So we used the break command together
with a safety lock to prevent infinite loops.
There are other reasons why you may want to break,
and you will be more experienced and be able
to make the choice as you write more code.