‫Welcome back.
‫In this video, we are going to have a look at four loops.
‫Therefore, I'm going to show you the practice of using for loops.
‫And we are going to start with an example.
‫So let's go ahead and use the for keyword.
‫Then in parentheses you have three values or three parts.
‫So the first part is the counter itself and I use an integer here that's common practice and counter
‫could be a name.
‫Usually what you see is I so int I is something that you see quite often when seeing for loops.
‫So counter then the condition here you compare your counter to a specific value.
‫So let's say as long as counter is lower than ten, I want to go ahead and run my code.
‫But now my counter needs to be initialized as well.
‫So I need to set it, for example, to zero.
‫So now the counter will run until it's nine because at ten this condition is not met anymore.
‫So what I finally need is a increment.
‫So I'm going to increment my counter by one by using the double plus.
‫So by plus plus I say, okay, increase this counter by one as soon as this iteration of the for loop
‫is done.
‫And that's going to happen every single iteration.
‫So every single time the for loop is run, it will increase by one.
‫And we can simply see if that's true by using console dot right line.
‫And here I'm just going to print out the counter.
‫And now finally we need to use the console that read in order to see something on the console.
‫So I'm going to run that real quick and then I'm going to go through what we see here once again.
‫So let's look and we see 012344529.
‫So it has run ten times, it started at zero and then it went all the way to nine.
‫So that's what this counter does here or this for loop does here.
‫So we have a start value which is of type integer and which I call the counter.
‫Then after the semicolon we have the condition, so we check if counter is lower than ten and then we
‫increase by one.
‫So what the general flow here is we set a value to zero or to any other start value.
‫Then we check if the condition is true.
‫If that's true, then this code will be executed.
‫So anything that's inside of the code body or the for loop body will be executed.
‫Anything that is between those curly brackets.
‫And in our case, it's simply to write something onto the counter.
‫And then once that is done, once this part here is executed.
‫So everything in the body, the counter is increased by an amount, and in this case it's increased
‫by one.
‫So it's incremented by one because counter plus plus increments a value by one.
‫If you want it to decrease a value by one, you would use minus minus.
‫But in our case, we want to increase it by one.
‫So that's the general approach for for loop.
‫Now let's have a look at a for loop which starts at zero, but goes to 50 and increments by five each
‫time.
‫So counter plus equals five.
‫That means that it will increase its value by five each time it executed the code.
‫So let's run it again.
‫And that we are zero five, ten, 15 and so forth.
‫So what our code does here is do the following.
‫We start at zero.
‫It checks is zero lower than 50.
‫And that's true.
‫So let's run the code so it executes and it writes zero onto our console.
‫Then it increases itself by five, so it increments by five.
‫The next time it checks, OC is five lower than 50.
‫That is true.
‫All right.
‫So the run the code, then it's increased or incremented by five once again.
‫So now it's ten.
‫The counter checks is ten.
‫Lower 50.
‫Yes, it is.
‫All right.
‫Let's run the code and so forth until we are at the point where this condition is not met anymore.
‫So let's say we were at 45 and now we increase the number by five.
‫So we add another five, we increment by five.
‫Then the condition here is not met anymore because it asks here or checks is 50 lower than 50 and because
‫50 is not lower than 50 or smaller than 50, this condition is not met anymore.
‫So the code is not executed anymore and we get outside of this loop.
‫So we get to this code here, console read.
‫So what we also could do just to see how that is true, we can simply write something like for loop
‫is done and then we will see for loop is done.
‫So after all of that is done.
‫Condition is not valid anymore and it's done.
‫We could even write something like counter plus is lower than 50.
‫So now if we run it again, it says zero is lower than 50, five is lower than 50 and so forth.
‫So this is the condition and it's met all the time and then the for loop is done.
‫And that's generally how we for loop works.
‫All right.
‫Now a little challenge for you.
‫Please go ahead and write a for loop, which only prints out the odd numbers from 0 to 20.
‫So go ahead and build that.
‫Just to counter my for loop counter, which only prints out the odd numbers.
‫All right let's go ahead for and counter is equal to one counter lower than 20 counter plus plus and
‫equal actually plus equal to.
‫So we want to increase it by two each time.
‫And now let's write that onto the console.
‫So console right line.
‫And here I write the counter and here again console, dot read and now we can go ahead.
‫All right.
‫And then we are all the old numbers.
‫One, three, five, seven, nine and so forth.
‫Great.
‫So now you have seen how to create odd numbers and how to create for loops in general.
‫And the next video, we are going to have a look at the do while loop.
‫So see you there.