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