1 00:00:00,060 --> 00:00:05,880 Welcome back in this video, we are going to look at another control flow, which is one type of a loop 2 00:00:05,880 --> 00:00:07,960 and there are multiple types of loops. 3 00:00:08,490 --> 00:00:11,070 And we're going to start with a while loop. 4 00:00:11,250 --> 00:00:17,850 So a while loop executes a block of code repeatedly as long as a given condition is true. 5 00:00:18,120 --> 00:00:20,700 So we're looking at this while keyword. 6 00:00:20,820 --> 00:00:24,660 Then we have a condition in the brackets, in the square brackets. 7 00:00:24,960 --> 00:00:30,360 And then in the curly brackets, we have the code that should be executed as long as the condition is 8 00:00:30,360 --> 00:00:30,660 true. 9 00:00:31,320 --> 00:00:36,400 So now if we look at the specific example, it could look like this. 10 00:00:36,480 --> 00:00:40,540 So we have a variable called X, and it should be one. 11 00:00:40,560 --> 00:00:42,060 So it starts with the value of one. 12 00:00:42,540 --> 00:00:49,110 And then we compare and say as long as X is less or equal to 10. 13 00:00:50,560 --> 00:00:52,850 We want to execute the following code. 14 00:00:52,870 --> 00:00:59,380 We want to say X, so should just print the value of X, nothing more than that. 15 00:01:00,370 --> 00:01:00,790 All right. 16 00:01:01,000 --> 00:01:07,630 So if we do it like this, then we get an infinite loop because this whale statement will always be 17 00:01:07,630 --> 00:01:12,940 true because one is always less than 10 or equal to 10. 18 00:01:13,510 --> 00:01:20,850 So what we need to do is we need to at one point lead to an end for this condition. 19 00:01:20,860 --> 00:01:23,530 So this condition has to end at one point. 20 00:01:23,560 --> 00:01:24,430 It's really important. 21 00:01:25,060 --> 00:01:31,780 It can either be done through whatever happens within the while loop, or it can be done from outside. 22 00:01:31,790 --> 00:01:37,750 So let's say we execute something as long as the mouse is moving. 23 00:01:37,750 --> 00:01:40,600 As soon as the mouse is not moving anymore, we don't execute it. 24 00:01:41,440 --> 00:01:46,210 This is a very basic example, and this is something that probably shouldn't happen. 25 00:01:46,420 --> 00:01:55,240 But if you look at our mouse while we're moving it, it's also changing its position and our UI. 26 00:01:55,570 --> 00:02:00,490 Even though, of course, it will not be executed via a wire loop how the mouse is displayed. 27 00:02:00,490 --> 00:02:08,470 But it's going to be something that really is calculated all the time, meaning our screen so refreshing 28 00:02:08,470 --> 00:02:12,730 the whole screen and displaying the position of the mouse at a specific point. 29 00:02:13,330 --> 00:02:13,660 All right. 30 00:02:13,660 --> 00:02:19,510 So this while loop will be executed all the time until the point where the condition is not true anymore. 31 00:02:19,540 --> 00:02:28,000 So we need to actively lead to an end of this condition, meaning that we can do something such as incrementing 32 00:02:28,000 --> 00:02:31,210 X each time that this wire loop is executed. 33 00:02:31,510 --> 00:02:36,040 So what will happen now is our X is one in the first iteration. 34 00:02:36,050 --> 00:02:40,810 Then this condition is checked, so is one less or equal ten. 35 00:02:41,260 --> 00:02:41,890 It's true. 36 00:02:41,920 --> 00:02:48,970 So execute the code block and in the block it's just going to print x whatever X is in our case, one 37 00:02:49,360 --> 00:02:55,270 and then it's going to increase X by one, which means that in the next iteration is going to be two. 38 00:02:55,630 --> 00:02:59,280 So we check is to a less or equal ten. 39 00:03:00,040 --> 00:03:00,620 It's true. 40 00:03:00,640 --> 00:03:01,060 All right. 41 00:03:01,060 --> 00:03:02,810 So please execute the code block. 42 00:03:02,920 --> 00:03:07,360 So it's going to print two and Increment X by one. 43 00:03:07,660 --> 00:03:13,500 So it's going to be three in the next iteration, and that's how it's going to go until it's at 10. 44 00:03:13,510 --> 00:03:16,120 So it's going to check 10 less equal. 45 00:03:16,120 --> 00:03:17,350 Ten is true. 46 00:03:17,770 --> 00:03:18,190 All right. 47 00:03:18,190 --> 00:03:25,030 So execute the block print 10 increase X by one, which means X is going to be 11 now. 48 00:03:25,050 --> 00:03:28,240 So we check is 11 less or equal. 49 00:03:28,240 --> 00:03:28,630 10. 50 00:03:29,080 --> 00:03:29,820 No, it's not. 51 00:03:29,890 --> 00:03:35,350 So don't execute the block anymore, but go to the next line of code, whatever that will be. 52 00:03:35,380 --> 00:03:43,810 So if we have something here while loop is done only, then this while loop is done, code will be executed. 53 00:03:45,520 --> 00:03:51,520 So let's execute this code, and we see one, two, three, four and so forth. 54 00:03:52,090 --> 00:03:53,680 Five six seven eight nine 10. 55 00:03:53,980 --> 00:03:55,210 And while loop is done. 56 00:03:56,630 --> 00:04:01,820 Now, if you want to print them next to each other and not line by line, you can, of course, instead 57 00:04:01,820 --> 00:04:08,270 of writing, print and just print it and it will print it just next to each other. 58 00:04:11,050 --> 00:04:15,040 So though we are one, two, three, four and four while Luke is done. 59 00:04:16,560 --> 00:04:22,050 Now, by the way, what if you want this while loop to be in the next line? 60 00:04:22,140 --> 00:04:28,920 So this texture while loop is done well, how you can do it is you can add an additional line break 61 00:04:28,920 --> 00:04:29,700 manually. 62 00:04:30,180 --> 00:04:30,930 How would you do that? 63 00:04:30,960 --> 00:04:39,240 Well, you do a backslash and here this backslash and is not going to be interpreted as an end and as 64 00:04:39,240 --> 00:04:39,780 a string. 65 00:04:39,780 --> 00:04:47,100 But it's just going to say or it's going to be interpreted as a code, so to speak, and it's going 66 00:04:47,100 --> 00:04:48,360 to create a line break. 67 00:04:48,750 --> 00:04:50,310 So as you can see, it's also orange. 68 00:04:50,310 --> 00:04:51,900 It's not green as the string is. 69 00:04:52,500 --> 00:04:55,650 And that's generally what the backslash does. 70 00:04:56,070 --> 00:04:58,500 It just keeps the character. 71 00:04:58,510 --> 00:04:59,700 So that's the key word here. 72 00:04:59,700 --> 00:05:06,870 It says escape, so as keeps the end, which means that it will be ignored as a string, but it will 73 00:05:06,870 --> 00:05:10,350 be used as code, so to speak. 74 00:05:11,070 --> 00:05:18,030 So if we run that again, then while loop is done, will be on the next line of code or on the next 75 00:05:18,030 --> 00:05:20,400 line of this case of our console. 76 00:05:20,850 --> 00:05:27,840 You can also see that this empty space here is also over here, so you can even get rid of this empty 77 00:05:27,840 --> 00:05:29,850 space and it will still work. 78 00:05:31,610 --> 00:05:35,930 So that we are now there is no empty space at the start of the line anymore. 79 00:05:37,730 --> 00:05:45,290 So now a little challenge for you, right a while loop, which only prints every second value descending 80 00:05:45,590 --> 00:05:53,300 from 100, so it's going to print 98, 96, 94 and so forth. 81 00:05:53,810 --> 00:05:55,460 All right, so please go ahead and try that. 82 00:05:57,110 --> 00:05:57,440 All right. 83 00:05:57,470 --> 00:05:58,220 I hope you tried it. 84 00:05:58,370 --> 00:06:01,070 So we start at one hundred and. 85 00:06:02,140 --> 00:06:04,270 We go down to zero. 86 00:06:05,190 --> 00:06:10,230 And then instead of + +, we say minus equal to. 87 00:06:11,650 --> 00:06:12,850 All right, now, it's going to print. 88 00:06:13,920 --> 00:06:19,500 The value and it's going to go down by two each time, and maybe we even start at. 89 00:06:20,250 --> 00:06:22,920 Well, this case in this case, 100 will be included. 90 00:06:23,340 --> 00:06:28,470 Otherwise, what we could do is go from ninety nine here. 91 00:06:28,710 --> 00:06:33,570 But I said going from 100, so let's just see what is going to print. 92 00:06:34,050 --> 00:06:39,960 And there we are 198, 96, 94, 92 in 1988 and so forth. 93 00:06:40,290 --> 00:06:43,260 So it goes all the way down to. 94 00:06:44,790 --> 00:06:46,080 For two and zero. 95 00:06:47,560 --> 00:06:53,890 So and these examples, it's a very basic type of code that is executed, this is really just printing 96 00:06:53,890 --> 00:06:54,970 something on the screen. 97 00:06:55,210 --> 00:07:00,130 But this can be super useful and many different applications when writing code. 98 00:07:00,250 --> 00:07:05,710 So while loops are not only useful for what you can see here, where we just print a value onto the 99 00:07:05,710 --> 00:07:10,540 screen, but they are really useful to just execute a code multiple times. 100 00:07:10,870 --> 00:07:15,310 And yeah, that's something that we're going to do quite often in the future, and we're going to use 101 00:07:15,310 --> 00:07:16,510 different loop types. 102 00:07:16,510 --> 00:07:21,850 So not just a while loop, but also the for loop or even to do while loop. 103 00:07:22,180 --> 00:07:24,610 And of course, also the for each loop. 104 00:07:25,150 --> 00:07:30,790 So I say, let's just go to the next video where we're going to look at the next loop type and we're 105 00:07:30,790 --> 00:07:34,180 going to look through all of them step by step in this course.