Let's now talk about the break statement because it's a very,
very powerful statement and we're going to be working with it in
this course. But before
I do that,
I just want to show you a different way of actually printing out
the names in this particular example.
Right now we're saying good morning and then plus and then XX, of course,
being the variable
representing each name in our list.
Another way of writing this would be to do something like this. First of all,
I'm going to remove the uh plus X
OK?
But now I'm gonna come right here at the very beginning,
I'm going to type in F
OK?
And now right here,
I'm going to add my curly braces, not the usual braces, but the curly ones.
And now here
I will simply type in X
OK.
I can run the program and you can see right now we have the exact same results.
This right here is a different and also a more efficient way
of actually printing out
the values of the variables in a list or an array
in a for
or while loop.
All right.
And we're going to be working like this moving forwards.
Now,
the break statement, what exactly is the brake statement
it's used when we want to terminate a loop early.
For example, in this folder that we've seen right now, what it does is that it simply,
it's simply gonna go through
all the names in our list and then print out
uh good morning and then the name of the person, right?
But what if for one reason or the other,
we wanted to terminate the loop as soon as James has been reached,
we don't want to print out a good morning Violetta because we don't like Violetta,
right?
This is where we could use the break statement
to terminate the loop early
and to demonstrate this, I'm gonna write a completely different program. OK? So
let's do this. All right, I'm gonna type in, let's say numbers again.
I'm gonna create a new list right here. Numbers equals
and
I'm gonna put in my
brackets in here
and let's go with a series of numbers. I'm gonna go with 13. Let's go with uh 78, 10
and let's go with 11 and let's add one more
even number 14.
OK.
So we have a list of numbers in here.
And what we want to do right now is we want to create a loop
that will run through all the numbers.
However, once the first even number has been reached,
we want to terminate the loop, meaning that once the number, which is eight,
we're going to end the loop. So it should run four times
the first loop for one, the second loop for three, third loop for seven,
fourth loop for eight.
So think about this, right,
we're gonna create the for loop first and then we're going to introduce the if
statement. So the if statement will check. OK.
Is this number actually even, or is it odd if it's even then break the loop?
Because we found the first even number elses,
if it's not an even number, if it's an odd number,
continue
with the loop. So
how are we going to do this
gonna tap and enter
the
follow up for,
let's say num
OK, no, would represent the, the numbers.
And by the way, I'm not sure if I've mentioned this before, but I think it's,
it's time I do.
So
see, I've only typed in for number.
You can see right now on my screen, Python is already suggesting in numbers
because Python is actually quite smart, right?
So in
this kind of situation right now,
if you're happy with the suggestion that the program has given you,
you can see,
press the tab
a key on your keyboard
and
you will get the exact same result, right? So I've typed, I
do forgive me. I can't talk,
I've typed the tab number
on the tab key on my keyboard. And now I have for num in numbers, I'm gonna add my colon.
Ok. Now
I'm going to add the if statement. And would you look at that?
I promise you. I've actually not typed this program before,
but I think Python is so intelligent.
It already has an idea of what I might be looking for.
Yes, I want if none
and then percentage two equals zero. So what exactly is this, what is going on here?
What this means is that we're going to check if the number that's been pulled out,
if it's divisible by two and the remainder is equal to zero,
then we know for sure that it is an even number.
If you divide seven by two,
you will get 3.5, 3.5 there's always going to be that remainder may have been half.
But when you divide eight by two, the answer is straight up four.
It's not 4.1 it's not 4.2 it's not 4.5, it is four.
So this is how we can check if the number is actually even.
So
if the number is, in fact, even what do we do,
let's say print,
OK.
And now I'm going to add the F string.
OK? And now the actual string itself
found the first even
number.
OK, let's add my comma. And now I want to display the actual number, curly braces.
And what is it gonna be? It is going to be numb.
OK.
Awesome.
So
we've added what
the program should do if the number is, in fact,
an even number Prince says, OK, we found the first number and now break the loop,
we don't need the loop to want anymore.
Break it. It's over. We found the first even number we're getting out of here.
Else.
Make sure your tabs. Remember the tabulation is extremely important.
If else must be on the same tab right there, the same, the same indentation, right?
So
simply print out, let's just say print,
um,
let me add my brackets and then my coats
still looking
for that even
number.
And
there it is
OK.
Let's go ahead right now,
run the program and there it is
still looking for that even number,
still looking for that even numbers looking for that even number.
Oh, look, we found the first even number and the number is eight.
This is the use of the break statement.
Now let's use it in a while loop now to do this, I'm gonna come all the way down here
and let's create a very simple program that will ask the user to type in something.
And as long as or type in a particular letter and as long as the letter isn't equal to Q,
then just keep asking
the user for a new letter.
OK?
So
that's basically the condition that needs to be satisfied.
So while the user is typing in AB CD, ef as long as it's not Q,
keep asking the user to input a new letter. So
I'm going to say while true
what this does right now, this is a very simple function
or loop in Python
that automatically has created a condition that is always true.
Unless something else happens. Remember that with the wild loops,
there needs to be a particular condition
that must be continually satisfied in order for the loop to actually run
and then it must be terminated once a very specific condition has been met. So
we're going to assume right now that
the condition is always true.
And now I'm going to type in user input,
this is going to be the variable
that will represent
the letter
that the user will actually type in. So
I'm gonna say user input equals and now
input,
remember the function input, which will now ask the user to type in something. So
I'm gonna say
uh
enter
a letter
and then in brackets,
I'm gonna say all
type a Q,
OK?
Type Q to exit the program.
OK? So we're giving the users here a choice. OK? You can either just type in a letter
or if you want to exit the program, just type in Q.
OK?
So
I'm going to press enter. So now let's type in
the condition
that most that
we need to check whether or not the input was actually QM
gonna come in right now, add
the if statement.
So we're gonna check if the user input is equal to Q,
what should we do? Well,
I'm going to say, print,
uh, in brackets
and I'm gonna say
you have,
you
have chosen to exit
the program,
uh, just exit the program,
press enter
and there it is.
And now since they've chosen to, oh, let me add the closing
code here. Since they've chosen to exit the program, we are now going to break
the loop.
Otherwise I'm gonna come in here right now
and then say print
and thou
in quotes
you
typed.
And now remember in the curly brackets,
I'm going to say user underscore input
and
there it is,
I
gonna go ahead right now,
run the program.
OK? So now it's saying enter a letter,
I'm gonna say Y
press enter, OK? Now it says, OK, you typed Y
enter another letter. I'm gonna say a
once again it's running until I now type in Q
and now
it has terminated the program.
And this right here is us combining the
our while statement,
the wild loop brother with the break statement.
So let me run through this over again. I know you might have a question.
Why didn't we type in the LS?
Because we have the if statement? OK. So where is the LS? Well,
we could actually add the LS.
It's going to be right here
and I'll just say LS
and then
the Tate
print,
you typed this. So I can go ahead right now, run the exact same program again.
OK. Come in here right now, type in G,
it works well,
type in U, it works well typ in Q enter and now it works the exact same way.
So the one that you need to understand here is that
the reason why the LS statement is actually optional here is because
we're working with the wild
true
uh loop, right, the condition.
So it's going to automatically assume that the condition will always be true.
And now we're using the break statement here to intentionally control
when the loop would actually end.
So there's really no need for the else statement here.
The loop will always run
because we have said while true
and will only stop
via the break statement
once the user has typed in the letter Q.
So really the else statement here is not necessary,
you can still type it in the program will still work perfectly fine.
But whenever you're working with the while true and then the break statements,
you don't need to add the LS condition uh any more.
So we've done quite a lot in this particular video. Hopefully
you've been able to follow and hopefully you now understand how the break statement
is used.
Thank you so much for watching the video. I will see you in the next class.