0:00

[MUSIC]

So the next thing I want to cover is loops.

I mean, what computers are really, really good for is repetitive work, right?

So a lot of computer work is definitely repetition, over and over again.

Integrate, we do that with loops.

Loops are a form of control flow statements.

The way how that works is we have different kinds of loops.

One thing we have is a loop for any value in a certain range.

So for i, inferred here as an integer because the other part's an integer in 1

to 5, do something.

And so between the brackets is all the code which we want to execute for the i.

0:45

Al right, so this one would execute basically for 1, 2, 3, 4 5.

Or we can loop as an increment or decrement.

So that's the loop you might notice from C# or Java.

All right, so for i equals 5.

So here, you initialize the variable, so i equal 5.

And then you loop through until i is smaller, so as long as i is less than 10.

And then you increment i by 1, and then for

each of these increments, you would execute this loop.

You can also loop as long as a condition is met, so these are while loops.

Like while money is bigger than something, spend a part or the whole thing, right?

So that would be a way of saying it in words, but

I'll show you a couple of samples for that.

Or you could do something where the condition is actually execute,

or computed at the end of the execution.

You would repeat something until a certain condition is true, or

as long as a certain condition is true.

Now, the next thing which I will do, I will show you some code samples for loops.

One example which is being used on practically all over

the place when you learn to work with loops is like to calculate something like

Fibonacci numbers.

So Fibonacci sequence is something like 1, 1,

then the previous two numbers added together.

So 1 plus 1 is 3, so the next number, or 1 plus 1 is 2.

So the next number is 2.

Then 2 plus the previous 1 is 3.

2:10

Then the 3 and the 2 together is 5.

And so that's the Fibonacci sequence.

So the way how that's being calculated is I assign first,

my first Fibonacci number is 1.

2:29

value before the previous one to the value to the previous.

And then my previous value becomes my Fibonacci number, and my next Fibonacci

number is then the two numbers added together, the previous two together.

And so I do that here in the loop.

I loop here for 20 cycles, right, for i in 0 to 20.

And then I just print the values here and

the output of that is what we see here at the bottom of the screen.

So you see here that the Fibonacci number is then the next one is,

you'll see best in the third line.

It's basically the previous Fibonacci number plus the Fibonacci minus 1.

So here you have the 3 is a 2 plus a 1.

The 5 is a 3 plus a 2.

So that's how the sequence is then printed out.

Happened basically by having this one for loop.

Now you can have, you can express that so it has a little more complication.

So you can say for case let j equal, so your sign here

a j in 0 to 10 with aware.

And now, in this case here, we take the j, and we

3:43

execute a mod 3 operation, and then print out the j.

So, what happens, in this case Is for

the first one you start with a 0, and it is 0, so it will print as 0.

Now the next ones it will actually step through and iterate only for

the ones where this part of the equation actually executes the two true.

So 3 mod 3 is 0, 6 mod 3 is 0, 9 mod 6 is 0, right.

So you will see here at the bottom of the screen this is actually the output of that

kind of loop.

Right and so it's a regular for

loop was a regular for in loop but combined actually was aware statement.

4:27

The next one is more the traditional for loop.

So where we have a variable, k we assign the variable, initialize the variable k

here with a zero, and execute as long as k is smaller than 10.

And for each iteration we increment k not by 1, but k by 2.

So the outcome of this will actually be this year.

So first one is 0, k smaller than 10.

4:56

Execute print the k at 2.

Then you have the 2, 2 is smaller than 10.

Execute that line until, and then you print the 2.

And so then, when you get to 8, 8 is smaller than 10.

5:11

You print the 8 and then you execute the increment, the k plus 2.

And in that moment it's 10, and k is not smaller than 10 anymore.

So the 10 we actually don't print out anymore, all right.

So in the moment in which this condition doesn't hold true,

we don't execute anymore.

5:37

So, it's a while loop, you execute, actually,

as long as this n smaller than 100 statement is true.

So, simple ones, you would increment m always by 1, but in this case,

what I want to do, is I want to increment by a certain value.

So here we loop through the values of n as long as s of n,

as long as it's smaller than 100 and each time we increment it by 25.

So the outcome of this one here is, so

the first time n is equal to n plus 25, so we get 25.

Print the n and so there is 25.

Next time it's 50, 75.

Now why does it still print the 100?

Well if we had 75, and so 75 is smaller than the 100,

and if we then look at the 75, we add another 25 we end up

with 100 and we print the 100, right.

And now n is not smaller than 100 anymore so we stop executing this while loop.

It's only as long as this condition is true.

6:45

The repeat one is similar.

The only difference here is it will always execute what's here

while the condition is true.

If you look through this one It will execute until the 36.

All right?

So, we started with a zero, at some point we end up at like say 32.

We print the 32.

We do a 32 plus 4, that's 36.

36 is smaller than 40 so we still do it.

Next loop goes in,

we add another 4, we end up at 40 and don't print it anymore, all right?

Because the print will then happen actually in the next line.

So if I would turn this around, let's do that quickly.

7:30

Do the value before I print it, then if I scroll down,

then you see now we are printing the values from 4 to 40.

Because in this case we would still increment it and print it, and

then we see, okay now we are not smaller than 40 anymore and we would stop.

7:52

Controlled transfer state, so the first one is here.

It's a variable, like I say here for variable k equals 0, k is smaller than 20.

I print my k, and then I check if my k is bigger than 10.

And if my k is bigger than 10, I just continue, so I don't do anything.

8:19

Okay, after my k, I then print the inner condition.

So I would print that condition in addition.

So in the beginning it would actually execute this code and

once it actually reaches, so if the numbers are bigger than 10,

I just execute, I just go out of this loop and do the rest of the loop, right?

Yeah, do the rest of the loop.

And if I have a number which is bigger than 10,

and I continue, then I will never reach this statement.

So for all the values which are bigger than 10, the printing,

the inner condition part, is not executed any more.

9:02

Next one, we have a while loop, but in this while loop,

we use a break statement, right?

And so for this statement, we loop.

9:15

We declare a constant variable O is equal to 0.

While it's smaller than 20, we increment O by 1, and then we print the O.

But, if O, at any point, is reaching a number bigger than 10,

then we should break out of our while loop, right?

So, in this case, it prints here the numbers until 11, right?

So, in this case, because the O plus in the print is in that order,

so it does say 11 still.

But, as soon as we have a value which is bigger than 10,

then we leave our while loop, all right.

So the difference between the continue and

the break is that with the break we completely jump out of that while loop,

and we completely continue the code outside the while loop.

Versus, if you have a continuous statement, then we stay within our loop

but we don't execute the rest of that current loop, of that current iteration.

>> [MUSIC]