Hi, everybody, today will continue discussing for loops we were going to step it up a notch and see
some more examples in the last lecture we have seen for loops combined with lists.
And today we're going to see how can we use for loops combined with statements and also with a new action
called Break.
So before we begin, let's see a quick sketch of the flow of code.
And right after, we'll see many, many examples.
So, for example, you have a list with several items inside and you want to find which of these numeric
items is bigger than five.
So just for example.
Yeah, it's just a theoretical example.
So each time will we will check if the cell is bigger than five.
All right.
And once we find the correct value, once our requirements will be satisfied, we want to quit running
iterations upon the loop.
All right.
So this is the case.
So let's see.
Let's see here and the sketch on the right.
First we initialize the module, we run the code and we tackle the loop.
And right after we extract the value out of the list in our case and we compare it, if it's bigger
than five right after we were seeing and if statement.
All right.
And inside the statement, there are two options, whether the output of the if statement is either
boolean true or boolean false.
So we want some requirement to be fulfilled in this case, X bigger than five.
And we can see here in the sketch that we have an error that goes to the left.
For example, we have the first cell equals to one.
So obviously it's not bigger than five.
So it's a false.
What are we doing next?
We continue to next iteration, meaning we're taking the next cell out of the list and running again
the loop in second iteration there.
Obviously we encountered that statement once once again.
And we're doing this again and again and again until we will see that our cell is actually true.
Our output of the if condition is true.
For example, we have the number ten, which is bigger than five.
So it's true.
And then we want the code inside if a block will run.
All right.
So many we don't want any more iterations.
We found the item that we're looking for.
We're looking for the first item, which is bigger than five.
And then we use the action key break.
And that key, that action key basically tells the loop, stop running.
I found what I want.
All right.
So from here, let's see.
Many you see here, many examples that we're going to learn in this lecture and by these examples and
by this sketch together, I think we're going to get the hang of for loops combined with if statements
are also actually what is the break.
All right.
By the way, just a quick reminder, this lecture and basically all other lectures as well, have an
attachment file, that PDF file that you can download and we view after you watch the video.
All right.
So first of all, let's see how loop is applied on a collection right on the list.
So once using a loop upon list or any other type of collection, each iteration would scan one cell
in the collection.
We already seen this in the previous lecture as well.
So and if conditions statement will be used for some actions will be used for some actions to happen.
So in example, a right here we can see a list of fruits.
We have banana, orange and cherry.
And right after we are seeing a for loop, we're seeing here for fruit item.
And if you recall from previous lecture, we discussed that whatever comes after the four is a single
item out of the whole package and the whole package comes after in.
So fruits is the list.
It has to be the same name.
And what comes after the four is the single item.
You can name it anything you want.
You can choose whatever name you want.
And in our case, each each of these items will be one cell out of the list.
And we want just to print out each iteration, one item, one cell item value once we go again and again.
And the output obviously going to be banana or and.
We want to print all of the values in example A all right, and here an explanation.
One last item will be printed on each iteration.
So each time we go in the circle again and again and again, each time one item will be printed.
All right.
Until we'll finish the whole list and then we're going to go and continue the flow of code.
So from here, let's see example be and we'll see something a bit different.
So right here, we have also the same list.
All of these examples have, by the way, they have the same list.
And here we also have four fruit like them in fruits.
And here we want to find something we we inserted here and each statement.
And we think here, if fruit item equals orange, then fruit print the fruit item.
And basically what would happen here is that it's going to run on all of these cells.
And once it gets to orange, it will print it out.
As you can see right here, it will print the orange.
But once we find orange, it's not going to stop again.
The loop is not going to stop running.
It will run all all over the cells until we finish the last cell.
And only after that it will exit the for loop because we haven't set any condition to exit the loop
once we find what we want.
So the output will be orange, this cell.
But after we pass this cell, the the for loop also will scan this cell.
All right.
And if there were more, much more many cells after it, it would scan each and every one of them.
So only once the loop will get to the to the item orange, it will print it after orange item.
The loop will keep running until the last item without printing anything.
And from here, we'll go straight ahead, for example, see and here we will use the break action,
which should be with a small letter like this.
And also this part is the same as we have seen.
And also right here, it's also the same.
All of these four lines are the same.
But here we have an additional action key.
We have the break word here.
And basically what's going to happen, the output will be the same because we only have one orange here.
All right.
But once we find once we find the orange, the loop will not continue to this cell to the cherry.
It will not continue.
It will exit the loop completely and go further with the call with whatever comes after this for loop.
All right, so only once the loop will get to the item orange, it will print, it will print it.
Once Orange will be printed, the loop will stop running because we use the break word.
All right, Prezi.
So the break word basically tells the loop.
All right.
You found what you want.
Stop.
And that happens because the break it appears under the if in Hiroki, it appears under the if so whatever
runs after the if it goes by the order from top to bottom.
So we print the orange and then we break the loop.
All right.
Let's start here, the typical example here, we have seen an example upon a collection, but now let's
see what happens if we do the same thing upon a string, upon a variable.
All right.
So we have examples here and we have fruits.
And inside fruits, we have only one item, a banana.
All right, and in this case, sorry, in this case, you will see here also in fruits.
This is the whole package.
Right.
And whatever comes after the four in our case, it's fruit character is a single item.
So if our whole package is a string, meaning a bunch of letters, so a single item would be one letter
because the whole is a word and single item out of the word is just the letter.
So you can see what happens here.
It's like a dynamic structure.
If you have many items, as you can see here, each item is once a one word.
But in this case, we have only one word here, so we will see here, we will see here that each each
item, whatever comes after four is a single character.
All right.
Let me just correct you a small mistake.
I didn't mean it to be inside a list, so I'm just going to remove the square brackets because there's
no point for this example, if it's inside a list.
So we have just a simple string variable here.
All right.
So from here, we just going to go over the call again, so for fruit, character and fruits, and we
want to print each character one by one.
So if we print each each of these letters one by one, we going to end up with banana eventually.
So each letter fruit character will be printed on each iteration of the loop.
So the first iteration will print be the second iteration will print a then in the iteration after N
and then A then and and then A until we'll get to the last item here, which is a and then the code
will exit the loop.
Pretty simple.
All right.
So in this case we have one word with which is the whole and a single item out of the hole is obviously
one character.
So each each iteration in the gold will extract one letter, one character out of this string variable,
an example.
B, we can see something a bit different.
So here we are going to use an if statement, same as we did on collections, a list in the example
right here in the examples right here in B, and we're going to use the if statement right here and
we're going to say here, if fruit character equals and then print the fruit character.
All right.
So we're going to first iteration go B would not be printed, then A also would not be printed.
And then we're going to get to N and and is what we're looking for inside the IFES, obviously, and
will be printed.
But after we print this and the for loop will continue running because we haven't said it, we haven't
said to it to quit.
So it's going to keep running to this cell, to this it's not a cell to the next character, a will
not printed then out again.
It's going to get to and here this and it will be also printed and then it's going to go to the last
character A and only after will exit the loop.
So eventually the output will be and because we're looking for an and every time will encounter and
and here it will be printed out.
All right.
So in total, two end characters will be printed.
And in the last example, we'll see exactly what we have seen right here, but we're going to use a
break action as well to stop the loop whenever we want to do it right.
We're under control here.
So, again, we're going to look for the and character and we're going to print it.
But this time, if fruit character equals and we're going to print the NT and right after we're going
to break the loop.
So it goes first iteration here.
Second iteration.
Third iteration, and once we encounter the NT character, we're going to print it and break the loop.
OK, so in other words, all of these items right here would not be scanned at all.
The code will exit the full loop once we encountered the first and.
All right.
So right here, the output is then, obviously, and only only once the loop will get to the first string
and it will print it after the first and variable, the loop will stop running.
All right.
So this is the example once we apply the for loop upon a variable and we also use the break.
So summing up what we have started right here in this lecture, we have studied how how can we use these
three items, these three topics all together?
And you can see that each of them has its own features.
And once combined, its like it gives us a lot of power of filtering cells or filtering letters however
we want, and basically gives us a lot of power once we build project development projects because it's
very useful to filter items or filter information that way.
And I think it's very, very useful in lots of various positions in the industry.
Whether you're a web developer or whether you are an information developer, it doesn't matter front
and back end.
And basically it's very, very useful and it's really important to know this.
All right.
So this will be the conclusion for this version, for this lecture.
And I will see you in the next one.
Thank you.