- In the previous section, we saw how to write loops
and now we're going to see how to use the break keyword
and continue keyword
to control execution through those loops.
So if you've done other languages
and you've come across break and continue in those languages
then it's nearly exactly the same in Rust.
The only difference is when you have a nested loop,
how do you break out of a nested loop
or how do you continue with a nested loop in Rust?
So we'll have a look at that as well in this section.
So first of all, this is how you can break out of a loop.
Use the break keyword.
So I've got some hypothetical loop
and each time we run the loop
it's gonna be doing some processing
on the current element.
Okay, so that's fine.
But if we reach some condition, then break.
As soon as some condition is met
then the break keyword basically terminates the loop
and we're done.
So reasons for using the break keyword
is where you wanna loop over a collection,
maybe lines of text from a file,
you're looking for a particular item,
and once you found that item then stop looping
because you found what you were looking for, okay?
So if you found the item you were looking for, then break.
No need to loop anymore. We found it, let's stop.
Okay, right. What about the continue keyword?
Same kind of syntax, except with the continue keyword,
it doesn't abandon the whole loop completely
it just abandons the current iteration
and then it goes back up to the top again,
ready for the next iteration, okay?
So typical usage for that
would be where you're looping over some items.
So maybe you're reading lines of text from a file.
And so each time around the loop
you've got some processing that you're gonna do
on the current element.
But let's say that the next element that you read in
from the file is invalid, okay?
So in the if condition here,
you're saying something like,
if the current element is invalid
then continue the loop back at the top,
ready for the next iteration.
So basically it abandons, it doesn't perform the body
it just continues up the top, ready for the next element
in the collection.
So in a way, it's a pity that the continue element
wasn't called skip, okay?
Because that's kind of what it's doing.
But of course, because of C, basically
if you go back to 1972,
the C programming language introduced the continue keyword
and we've been stuck with it ever since.
So continue the loop body back up the top again,
ready for the next element.
So quite straightforward really
and familiar from other languages.
Okay, one thing that is different
is if you have a nested loop.
So if you have a loop within a loop,
by default the break and continue would just take you
out of the nested loop, okay?
And then you're still kind of,
you would then go back into the outer loop.
So if you've got this kind of arrangement,
if you've got an outer loop and an inner loop,
if you say break or continue in here,
it'll just break or continue the current, the nested loop.
If you want to break or continue
out of the outer loop like that,
then you have to take special measure.
So what you have to do is you have to attach a label
to the outer loop.
You give it a name, it doesn't matter what you call it
but you use this kind of syntax.
Apostrophe and then some kind of name and a colon.
Okay, so I could basically say 'outer: here.
I label the outer loop a scope basically.
And then when you say break or continue
if you want to break out of the outer loop basically,
like here, or if you wanna continue the outer loop,
like here, then you have to use that label name.
So if you wanted to break out of the outer loop,
you'd say break and then apostrophe,
and then whatever name you called it up here, okay?
So whatever, it doesn't matter what you call it
but it would be the same name here.
And I would then basically break out
of that outer loop like so.
Or alternatively, if you want to continue the outer loop
then you say continue with the label that you specified.
And that would then continue, not continue the inner loop,
it would instead continue the outer loop.
It would basically iterate around the outer loop like so.
So it's not often where you need to use nested loops
with kind of labeled loop names
but you can do it if you want to.
So let's see an example of all of this.
Same project, lesson03_flow_control
and it's the final function we're gonna look at
in this lesson, demo_break_continue.
Let's have a look. So here's my code in VS Code.
And in the main function,
I'm going to uncomment the last function
called demo_break_continue.
And let's have a look at that function down here.
Okay, so I've got several different bits of the example
and as usual, I'll run the example first
so we can actually have some output to look at.
cargo run.
Okay then, so let's have a look.
So this is my demo of using break and continue.
Here's my array.
Oh, I'm looking to see, did the student get 100 in any exam?
So loop through the elements.
If the element was 100, then say we found 100.
So break out of the loop completely.
And the break statement takes me down here.
Of course, if the value wasn't 100
then we just print it, okay?
So as we pass in 99 and 45 and 85,
those would fail the if statement
and they would just print the value 99, 45, 85.
And then when we get to 100, hooray,
that's what we're looking for.
Break, stop looping, and we're done here.
There we go.
I never, ever got 100 in any exam ever in my life.
I had 99 when I was about 14.
We had an exam on technical drawing
but my teacher took one mark off because I'd written my name
in a slightly untidy handwriting.
So my moment in the sun, snatched from my grasp.
But I'm completely over it. 44 years later.
What about this one then?
If the exam mark was less than 50, that would be a failure,
then basically skip that value and go back to the next one.
So the first few exam marks,
well, 99, I didn't skip 99, I printed 99, so there's 99.
And then the next element was 45.
So 45, basically skip that element.
I found a value less than 50. So basically skip it.
So the continue keyword basically stopped
the current iteration, didn't go any further.
It basically wound back up ready for the next element
and the next few elements were okay.
85, 100, and 82.
Very good.
And then here I've got an example of a nested loop.
So it's a nested loop if you like,
but it could have been a nested while loop
or it could have been a nested for loop.
And you can use this 'outer: mechanism for any kind of loop.
I've just used an infinite loop here.
So I have an infinite loop potentially.
Entered the outer loop, it says here.
So I have entered the outer loop
and then here I have now entered the inner loop.
Okay, so I'm now inside the inner loop.
I break to the scope of the outer loop, okay?
So it's not gonna go any further.
It's gonna basically break out of this loop here.
So that loop, the outer loop, whatever I call it.
I didn't have to call it 'outer:,
I could've called it 'wibble: and 'wibble:.
That would've also been okay.
It doesn't really matter what you call it.
So break to the loop whose scope is defined by that.
It'll break out of this loop here.
In other words, it'll break out there.
And then we get the message, exited the outer loop. The end.
Exited the out the loop. The end.
So that is the end of this lesson.
In this lesson, we've seen how to write if statements,
we've seen the match keyword, we've looked at loops,
and we've looked at how to use break and continue, okay?
So we're starting to put together algorithms
with a little bit of interest now.
Let's see what comes up in the next lesson.