‫Welcome to the introduction to Loops and the next chapter.
‫We are going to cover the different loop types and you're going to learn how to use them in practice.
‫But first of all, let's have a look at theory and we're going to start off with the different loop
‫types and the advantages.
‫So let's check the advantages first.
‫What are the advantages of loops?
‫Well, first of all, they save time.
‫So you don't have to create loads of code, but you can often simplify it and put it into a loop and
‫they can help you to save time.
‫Then they are quick and easy repetition module, so they allow you to repeat your code easily multiple
‫times based on conditions that you set up.
‫So if you say, okay, I want to run this code ten times, you can simply do that with a loop.
‫They allow you to work with huge amounts of data so you don't have to check the data manually, but
‫you can do that with a loop as well.
‫So that's a huge advantage and we will see when it comes in handy, then it allows you to loop through
‫a race and that's something that doesn't add too much value yet because we haven't covered the race
‫yet, but we will do so in one of the following chapters.
‫So no worries.
‫We'll get there and then you'll see what the advantages of loops are when we work with the race.
‫So next let's look at the loop types and there are multiple types.
‫First of all, the for loop, then there is the while loop which checks and then it goes.
‫Then there is the do while loop which does first and then it checks and then we have the for each loop
‫which runs through an array or a list.
‫And that's what I'm going to show you as soon as we get to your race and lists.
‫So first of all, the for loop, the for loop has a start value in brackets, then separated by a semicolon,
‫the condition and the increment again separated.
‫And then in curly brackets you have the code body.
‫So that's code that should be repeated.
‫How often it should be repeated is based on the start value and the condition and of course the increment.
‫So whatever you set up in those three lines will determine how many times this for loop will be run.
‫And that's perfectly great for counters.
‫So for loops are amazing in order to run counters.
‫Next the while loop you start off with the counter variable.
‫You set it, for example, to zero.
‫It could be a different number.
‫Then you have the while keyword and in brackets you have the condition.
‫So for example, as long as the count variable is lower than ten, please go ahead and run that while
‫loop.
‫Then you have the code that should be executed once the condition is met and it will be repeated as
‫often as long as the condition is met.
‫One very important aspect here is to have this counter variable increment.
‫So you need to increment the counter variable within the while loop.
‫Otherwise you get an an infinite loop and that's something that you don't want because that will crash
‫your program.
‫So that's the general structure of a while loop you can use while loops, not only with integers or
‫numbers, you can also use them with booleans so you could check if something is true.
‫So for example, you could check what did the user enter?
‫As long as the user didn't enter zero, for example, you're just going to add the numbers that he enters.
‫So that's something that you could check here.
‫So while a specific condition is not true, so the user didn't enter zero, so it means the bool is
‫false.
‫Please go ahead and run the code and it executes the code as long as the condition is met.
‫And that means that it doesn't only have to be as long as this counter variable, for example, is,
‫but also based on booleans.
‫And that will make a lot more sense as soon as we go into Boolean.
‫So we will have examples of booleans as well.
‫All right.
‫So next the do while loop and that one is slightly different to the while loop, even though it has
‫the wild term inside of its name, it's starting with a counter variable as well outside of the loop
‫itself.
‫Then you have the do keyword, then the curly brackets, and in there you have the code that you want
‫to execute multiple times or at least once.
‫And that's the difference between a do while and a while loop.
‫The do while loop does first and then it checks at the bottom the condition.
‫So it will repeat as long as the condition is true, but it will run at least once.
‫And by the way, you have the counter variable increment here as well, then it executes the code as
‫long as the condition is met as well.
‫So the same as the while loop.
‫Only the difference is here.
‫As I said, it runs once first and that can be very useful if you, for example, don't know if the
‫condition is met, but you at least want to run the code because it initializes something or it prepares
‫something, then you would use a dual loop.
‫And then finally we have the for each loop and it runs through an array or a list.
‫It's only as long as there is content.
‫And that's great because.
‫If you run a four loop, for example, through an array, what you can get is an array index out of
‫bounds exception.
‫And that's something that you don't get with a for each loop.
‫So that's pretty much an error that you can avoid as long as you use it for each loop.
‫And finally, more about this in the Arrays chapter.
‫So we're not going to cover for each loop in this chapter, but we're going to use it as soon as we
‫get to the array section.
‫And I just wanted to put it in here so you know that there is a different type of loop as well.
‫So the for each loop.
‫Great.
‫So let's go into the demo and let's get started with some examples.
‫So see you there.