1 00:00:00,420 --> 00:00:03,480 ‫Welcome to the introduction to Loops and the next chapter. 2 00:00:03,480 --> 00:00:08,160 ‫We are going to cover the different loop types and you're going to learn how to use them in practice. 3 00:00:08,160 --> 00:00:13,410 ‫But first of all, let's have a look at theory and we're going to start off with the different loop 4 00:00:13,410 --> 00:00:15,060 ‫types and the advantages. 5 00:00:15,060 --> 00:00:16,950 ‫So let's check the advantages first. 6 00:00:16,950 --> 00:00:18,510 ‫What are the advantages of loops? 7 00:00:18,510 --> 00:00:20,400 ‫Well, first of all, they save time. 8 00:00:20,400 --> 00:00:27,570 ‫So you don't have to create loads of code, but you can often simplify it and put it into a loop and 9 00:00:27,570 --> 00:00:29,760 ‫they can help you to save time. 10 00:00:29,760 --> 00:00:37,320 ‫Then they are quick and easy repetition module, so they allow you to repeat your code easily multiple 11 00:00:37,320 --> 00:00:39,510 ‫times based on conditions that you set up. 12 00:00:39,510 --> 00:00:44,130 ‫So if you say, okay, I want to run this code ten times, you can simply do that with a loop. 13 00:00:44,820 --> 00:00:50,100 ‫They allow you to work with huge amounts of data so you don't have to check the data manually, but 14 00:00:50,100 --> 00:00:51,960 ‫you can do that with a loop as well. 15 00:00:51,960 --> 00:00:58,500 ‫So that's a huge advantage and we will see when it comes in handy, then it allows you to loop through 16 00:00:58,500 --> 00:01:03,480 ‫a race and that's something that doesn't add too much value yet because we haven't covered the race 17 00:01:03,480 --> 00:01:06,750 ‫yet, but we will do so in one of the following chapters. 18 00:01:06,750 --> 00:01:07,770 ‫So no worries. 19 00:01:07,770 --> 00:01:13,530 ‫We'll get there and then you'll see what the advantages of loops are when we work with the race. 20 00:01:13,770 --> 00:01:18,390 ‫So next let's look at the loop types and there are multiple types. 21 00:01:18,390 --> 00:01:24,120 ‫First of all, the for loop, then there is the while loop which checks and then it goes. 22 00:01:24,120 --> 00:01:30,120 ‫Then there is the do while loop which does first and then it checks and then we have the for each loop 23 00:01:30,120 --> 00:01:33,780 ‫which runs through an array or a list. 24 00:01:33,780 --> 00:01:38,010 ‫And that's what I'm going to show you as soon as we get to your race and lists. 25 00:01:38,460 --> 00:01:44,670 ‫So first of all, the for loop, the for loop has a start value in brackets, then separated by a semicolon, 26 00:01:44,670 --> 00:01:48,210 ‫the condition and the increment again separated. 27 00:01:48,210 --> 00:01:50,580 ‫And then in curly brackets you have the code body. 28 00:01:50,580 --> 00:01:52,920 ‫So that's code that should be repeated. 29 00:01:52,950 --> 00:01:58,350 ‫How often it should be repeated is based on the start value and the condition and of course the increment. 30 00:01:58,350 --> 00:02:05,970 ‫So whatever you set up in those three lines will determine how many times this for loop will be run. 31 00:02:06,240 --> 00:02:09,090 ‫And that's perfectly great for counters. 32 00:02:09,090 --> 00:02:12,240 ‫So for loops are amazing in order to run counters. 33 00:02:12,960 --> 00:02:16,500 ‫Next the while loop you start off with the counter variable. 34 00:02:16,530 --> 00:02:18,600 ‫You set it, for example, to zero. 35 00:02:18,720 --> 00:02:20,070 ‫It could be a different number. 36 00:02:20,100 --> 00:02:24,450 ‫Then you have the while keyword and in brackets you have the condition. 37 00:02:24,450 --> 00:02:30,990 ‫So for example, as long as the count variable is lower than ten, please go ahead and run that while 38 00:02:30,990 --> 00:02:31,590 ‫loop. 39 00:02:31,590 --> 00:02:38,580 ‫Then you have the code that should be executed once the condition is met and it will be repeated as 40 00:02:38,580 --> 00:02:40,680 ‫often as long as the condition is met. 41 00:02:40,680 --> 00:02:45,720 ‫One very important aspect here is to have this counter variable increment. 42 00:02:45,720 --> 00:02:49,200 ‫So you need to increment the counter variable within the while loop. 43 00:02:49,200 --> 00:02:55,620 ‫Otherwise you get an an infinite loop and that's something that you don't want because that will crash 44 00:02:55,620 --> 00:02:56,370 ‫your program. 45 00:02:56,370 --> 00:03:01,710 ‫So that's the general structure of a while loop you can use while loops, not only with integers or 46 00:03:01,710 --> 00:03:07,890 ‫numbers, you can also use them with booleans so you could check if something is true. 47 00:03:07,890 --> 00:03:11,070 ‫So for example, you could check what did the user enter? 48 00:03:11,070 --> 00:03:16,770 ‫As long as the user didn't enter zero, for example, you're just going to add the numbers that he enters. 49 00:03:17,190 --> 00:03:18,960 ‫So that's something that you could check here. 50 00:03:18,960 --> 00:03:25,890 ‫So while a specific condition is not true, so the user didn't enter zero, so it means the bool is 51 00:03:25,890 --> 00:03:26,520 ‫false. 52 00:03:26,580 --> 00:03:31,980 ‫Please go ahead and run the code and it executes the code as long as the condition is met. 53 00:03:31,980 --> 00:03:37,830 ‫And that means that it doesn't only have to be as long as this counter variable, for example, is, 54 00:03:37,830 --> 00:03:40,080 ‫but also based on booleans. 55 00:03:40,080 --> 00:03:42,900 ‫And that will make a lot more sense as soon as we go into Boolean. 56 00:03:42,900 --> 00:03:45,840 ‫So we will have examples of booleans as well. 57 00:03:46,410 --> 00:03:46,770 ‫All right. 58 00:03:46,770 --> 00:03:53,310 ‫So next the do while loop and that one is slightly different to the while loop, even though it has 59 00:03:53,310 --> 00:03:59,940 ‫the wild term inside of its name, it's starting with a counter variable as well outside of the loop 60 00:03:59,940 --> 00:04:00,570 ‫itself. 61 00:04:00,570 --> 00:04:06,750 ‫Then you have the do keyword, then the curly brackets, and in there you have the code that you want 62 00:04:06,750 --> 00:04:10,530 ‫to execute multiple times or at least once. 63 00:04:10,530 --> 00:04:13,350 ‫And that's the difference between a do while and a while loop. 64 00:04:13,350 --> 00:04:19,260 ‫The do while loop does first and then it checks at the bottom the condition. 65 00:04:19,260 --> 00:04:24,420 ‫So it will repeat as long as the condition is true, but it will run at least once. 66 00:04:24,810 --> 00:04:30,690 ‫And by the way, you have the counter variable increment here as well, then it executes the code as 67 00:04:30,690 --> 00:04:32,130 ‫long as the condition is met as well. 68 00:04:32,130 --> 00:04:33,960 ‫So the same as the while loop. 69 00:04:33,990 --> 00:04:35,220 ‫Only the difference is here. 70 00:04:35,220 --> 00:04:41,460 ‫As I said, it runs once first and that can be very useful if you, for example, don't know if the 71 00:04:41,460 --> 00:04:46,920 ‫condition is met, but you at least want to run the code because it initializes something or it prepares 72 00:04:46,920 --> 00:04:49,950 ‫something, then you would use a dual loop. 73 00:04:50,220 --> 00:04:55,170 ‫And then finally we have the for each loop and it runs through an array or a list. 74 00:04:55,470 --> 00:04:57,990 ‫It's only as long as there is content. 75 00:04:57,990 --> 00:04:59,250 ‫And that's great because. 76 00:04:59,500 --> 00:05:05,800 ‫If you run a four loop, for example, through an array, what you can get is an array index out of 77 00:05:05,800 --> 00:05:07,360 ‫bounds exception. 78 00:05:07,360 --> 00:05:10,390 ‫And that's something that you don't get with a for each loop. 79 00:05:10,390 --> 00:05:14,230 ‫So that's pretty much an error that you can avoid as long as you use it for each loop. 80 00:05:14,260 --> 00:05:17,650 ‫And finally, more about this in the Arrays chapter. 81 00:05:17,650 --> 00:05:22,240 ‫So we're not going to cover for each loop in this chapter, but we're going to use it as soon as we 82 00:05:22,240 --> 00:05:24,880 ‫get to the array section. 83 00:05:24,880 --> 00:05:29,620 ‫And I just wanted to put it in here so you know that there is a different type of loop as well. 84 00:05:29,620 --> 00:05:31,720 ‫So the for each loop. 85 00:05:31,930 --> 00:05:32,320 ‫Great. 86 00:05:32,320 --> 00:05:37,510 ‫So let's go into the demo and let's get started with some examples. 87 00:05:37,510 --> 00:05:38,500 ‫So see you there.