Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,180 --> 00:00:06,660
So now let's see how we can use and when we should use while loops in our C programming language.
2
00:00:06,810 --> 00:00:09,120
That's saying that we would like to print an asterisk.
3
00:00:09,540 --> 00:00:15,240
And from now on, I will use instead of asterisk, because it's kind of hard to say.
4
00:00:15,350 --> 00:00:15,580
Right.
5
00:00:15,870 --> 00:00:17,260
I will use just the star.
6
00:00:17,310 --> 00:00:17,550
OK.
7
00:00:17,610 --> 00:00:20,480
So an asterisk equals two staff.
8
00:00:20,610 --> 00:00:21,330
From now on.
9
00:00:21,360 --> 00:00:21,580
OK.
10
00:00:22,140 --> 00:00:25,500
So let's say that we would like to print just one star.
11
00:00:26,040 --> 00:00:32,100
And to do so, first of all, we will include the standard input output and creates our main function.
12
00:00:32,340 --> 00:00:37,050
Now, if we want to print these star, we just use a single print of command.
13
00:00:37,060 --> 00:00:37,400
Right?
14
00:00:37,560 --> 00:00:38,820
That's pretty simple.
15
00:00:38,880 --> 00:00:41,130
And what about printing two stars?
16
00:00:41,340 --> 00:00:43,440
Simply add another print, a function.
17
00:00:43,700 --> 00:00:49,770
And in the same way, if you would like to print three, four, five and more stars, just use the copy
18
00:00:49,770 --> 00:00:50,610
and based technique.
19
00:00:50,730 --> 00:00:57,060
But the real question arises when you are asked to print, for example, 10 stars, what will you do
20
00:00:57,060 --> 00:00:57,540
then?
21
00:00:57,840 --> 00:00:59,980
Will you use then print off lines?
22
00:01:00,120 --> 00:01:03,510
Because I think it's starting to get kind of messy, right?
23
00:01:03,930 --> 00:01:09,300
We can definitely see that these print off-line print of star line.
24
00:01:09,330 --> 00:01:12,240
Basically, it's it's kind of a ability of command.
25
00:01:12,450 --> 00:01:15,420
We repeat the same command over and over again.
26
00:01:15,750 --> 00:01:20,310
We can see it clearly here in this example that we do it for 10 times.
27
00:01:21,000 --> 00:01:27,270
And one of the things that we've learned in the previous video is that whenever we have repeating commands,
28
00:01:27,600 --> 00:01:29,910
we should think of using loops.
29
00:01:30,060 --> 00:01:32,310
So let's see the following lines of code.
30
00:01:32,460 --> 00:01:35,940
So first of all, we create a variable called count.
31
00:01:36,390 --> 00:01:41,670
And this variable will specify the number of times we've printed a star.
32
00:01:41,940 --> 00:01:49,050
So it will simply be some sort of indicator or as specified for us to know how many times a star was
33
00:01:49,050 --> 00:01:49,590
printed.
34
00:01:50,190 --> 00:01:52,110
And then we will use a while loop.
35
00:01:52,140 --> 00:01:55,660
So you, first of all, specify a wild statement, right?
36
00:01:56,070 --> 00:01:58,530
Just like we've discussed in the general introduction.
37
00:01:59,040 --> 00:02:05,730
And now in the parentheses, you can see that we specify the condition, which is count less than 10.
38
00:02:06,240 --> 00:02:11,440
Basically, what these lines of code mean is that wild count is less than 10.
39
00:02:11,760 --> 00:02:17,820
While this condition is true, we are going to execute the loop body, which is specified in this section
40
00:02:17,820 --> 00:02:20,400
between these nice curly brackets.
41
00:02:20,550 --> 00:02:24,840
And what we are going to do here is simply to print a star to the screen.
42
00:02:24,930 --> 00:02:29,310
So a star will be printed on every iteration of these loop.
43
00:02:29,370 --> 00:02:33,510
So if all around this code, you can try that also on yourself.
44
00:02:33,540 --> 00:02:35,730
But just do it carefully, OK?
45
00:02:36,300 --> 00:02:41,070
We can see that the printing of the stars to the screen never stops.
46
00:02:41,460 --> 00:02:42,750
And why is that?
47
00:02:42,870 --> 00:02:47,910
Can it be that what we have here right now is kind of an endless loop?
48
00:02:48,420 --> 00:02:50,700
Let's try to figure out what's going on here.
49
00:02:50,940 --> 00:02:56,250
So we have the wild statement and then in the parentheses, we have our condition.
50
00:02:56,250 --> 00:02:56,470
Right.
51
00:02:56,520 --> 00:03:01,670
We can see that count is less than 10 is our condition in this wild statement.
52
00:03:01,800 --> 00:03:08,370
And this is very similar to the conditions we've seen in the previous chapter where we've used our different
53
00:03:08,460 --> 00:03:09,420
F statements.
54
00:03:09,450 --> 00:03:10,680
You remember that, right?
55
00:03:11,190 --> 00:03:14,940
So the same goes here in the parentheses of the wild statement.
56
00:03:15,120 --> 00:03:16,950
We have our condition.
57
00:03:17,340 --> 00:03:21,090
And the result of that condition may be either true or false.
58
00:03:21,510 --> 00:03:27,180
If it's true, then all the commands under with these curly brackets are going to be executed.
59
00:03:27,300 --> 00:03:32,340
Which in our case is just one command to print a star to the screen.
60
00:03:32,460 --> 00:03:36,860
And it's located under the section that we said it is called of a little body.
61
00:03:36,860 --> 00:03:37,060
Okay.
62
00:03:37,140 --> 00:03:44,430
So the commands of the lobe are in these nice curly brackets and it's called the loop body, all the
63
00:03:44,430 --> 00:03:48,330
commands that are under these while statement.
64
00:03:48,420 --> 00:03:54,210
Okay, so the little body is going to be executed every time that the loop condition happens to be true.
65
00:03:54,270 --> 00:03:56,410
His longest count is less than 10.
66
00:03:56,640 --> 00:04:00,780
But if and once the result of the condition happens to be false.
67
00:04:01,080 --> 00:04:03,990
We do not execute any lion in the wild block.
68
00:04:04,290 --> 00:04:10,890
And we just move on to whatever comes afterward after the while loop, which in our case is just the
69
00:04:10,890 --> 00:04:12,300
return zero.
70
00:04:12,390 --> 00:04:19,170
So once again, we have our wild statement, our condition and the Lewy body that is executed based
71
00:04:19,170 --> 00:04:20,580
on the result of the condition.
72
00:04:20,670 --> 00:04:25,170
So we know, for example, we can see that the condition is count less than zero.
73
00:04:25,500 --> 00:04:29,700
And at the beginning of the program, we've set count to be equal to one.
74
00:04:30,270 --> 00:04:34,270
So for the first time that we see the wild block, this is the first time we've seen it.
75
00:04:34,440 --> 00:04:38,370
And we check out the condition, which is one less than 10.
76
00:04:38,460 --> 00:04:41,370
And the end result of this condition happens to be true.
77
00:04:41,370 --> 00:04:42,390
Happens to be true.
78
00:04:42,570 --> 00:04:48,780
Then in this case, we will bring these stories to the screen and go up again to check out if the condition
79
00:04:48,780 --> 00:04:51,750
has changed or maybe it still remains true.
80
00:04:51,780 --> 00:04:52,110
Right.
81
00:04:52,300 --> 00:04:54,660
It may be either either one of them.
82
00:04:54,810 --> 00:04:59,760
And if it's still true and count is actually less than 10, it's still less than.
83
00:05:00,030 --> 00:05:00,360
Right.
84
00:05:00,750 --> 00:05:03,210
It is still is it is just one.
85
00:05:03,360 --> 00:05:04,410
Nothing's changed that.
86
00:05:04,780 --> 00:05:06,810
Nothing's changed our account variable.
87
00:05:06,960 --> 00:05:10,380
So in this case, we are going to round the commands in the wild block.
88
00:05:10,410 --> 00:05:16,260
Once again, we are going to print another story to the screen, and then we are going to do it again
89
00:05:16,260 --> 00:05:18,680
and again and again and again and so on.
90
00:05:18,690 --> 00:05:19,200
You get it?
91
00:05:19,340 --> 00:05:20,130
You got the point.
92
00:05:20,280 --> 00:05:26,130
And as you probably noticed, the variable count, these variable will never change.
93
00:05:26,310 --> 00:05:27,540
It will never change.
94
00:05:27,570 --> 00:05:33,210
There were nothing that changes the inside of these while block inside of this little body and nothing
95
00:05:33,210 --> 00:05:34,340
change changes.
96
00:05:34,560 --> 00:05:35,790
These variable.
97
00:05:36,060 --> 00:05:42,600
So it's always these condition is always going to remain true, which means that we have an infinite
98
00:05:42,600 --> 00:05:42,810
loop.
99
00:05:42,870 --> 00:05:48,990
We are going to print stories to the screen over and over again and nothing will stop that.
100
00:05:49,140 --> 00:05:51,210
And that's basically not what we wanted.
101
00:05:51,220 --> 00:05:51,480
Right.
102
00:05:51,540 --> 00:05:57,600
We barely can stop this monster program that runs an infinite printing command.
103
00:05:57,900 --> 00:05:59,580
So don't try this one at home.
104
00:05:59,940 --> 00:06:02,040
And what what do you want us to do?
105
00:06:02,040 --> 00:06:06,120
Think about is that basically that's not exactly what we wanted.
106
00:06:06,120 --> 00:06:06,360
Right?
107
00:06:06,390 --> 00:06:07,500
We said it previously.
108
00:06:07,800 --> 00:06:15,920
So let's take another a look and try to to understand what's going on and how we should fix our house.
109
00:06:16,200 --> 00:06:20,820
How should we fix this problem that we have right now of using the infinite loop?
110
00:06:21,480 --> 00:06:23,310
So that's kind of simple.
111
00:06:23,400 --> 00:06:25,740
And we should just increase the value.
112
00:06:25,770 --> 00:06:29,160
We should just increase the value of count by one right.
113
00:06:29,190 --> 00:06:30,290
On every duration.
114
00:06:30,300 --> 00:06:35,820
We said that running from these curly brackets to these curly brackets, it's it's considered to be
115
00:06:35,940 --> 00:06:38,940
running a whole iteration of the loop body.
116
00:06:39,300 --> 00:06:46,320
So on every duration when we print a star of the screen, we are going to increment count by one.
117
00:06:46,470 --> 00:06:51,210
So that will look something like this count equals to the previous count plus one.
118
00:06:51,360 --> 00:06:56,280
So at the end of every duration, count is going to be incremented and then we're going to check out
119
00:06:56,280 --> 00:06:58,110
the condition once again.
120
00:06:58,410 --> 00:07:01,950
And now let's take a look at how the flow of the code looks like.
121
00:07:02,430 --> 00:07:05,940
So basically, we're getting into the main section.
122
00:07:05,970 --> 00:07:08,730
We initially the variable count to be equal to one.
123
00:07:09,030 --> 00:07:11,670
We can see it right here that it's kind of fee.
124
00:07:12,360 --> 00:07:13,350
It's a variable.
125
00:07:13,350 --> 00:07:17,160
It's kind of a box with the name count and the value of one inside of it.
126
00:07:17,490 --> 00:07:21,480
And now we come to the wire loop for the first time when count equals to one.
127
00:07:21,810 --> 00:07:26,640
And we check out the condition if one is less than 10 and the result is obviously true.
128
00:07:27,180 --> 00:07:30,390
So we will execute the commands in this lower body.
129
00:07:30,440 --> 00:07:30,630
Right.
130
00:07:30,690 --> 00:07:32,040
We said this is the loop body.
131
00:07:32,640 --> 00:07:38,490
So we will print one star on the screen and we can see it right here and we will increment count by
132
00:07:38,580 --> 00:07:39,100
one.
133
00:07:39,240 --> 00:07:42,600
And we can see that count currently equals two two.
134
00:07:42,720 --> 00:07:46,170
So that's the value of count after the first iteration.
135
00:07:46,200 --> 00:07:49,380
And you can see once star printed on the screen.
136
00:07:49,860 --> 00:07:51,120
This is our console.
137
00:07:51,120 --> 00:07:51,410
Right?
138
00:07:51,480 --> 00:07:53,700
This is just the screenshot of our console.
139
00:07:54,150 --> 00:07:56,380
And then we are going to eat the rate once again.
140
00:07:56,400 --> 00:07:59,280
We are going to check out if two is less than 10.
141
00:07:59,520 --> 00:08:06,510
We are going to enter these while now while you buddy to print another estado, the screen increment
142
00:08:06,510 --> 00:08:07,650
count by one.
143
00:08:07,690 --> 00:08:10,140
Now, which equals two, three and so on.
144
00:08:10,140 --> 00:08:11,010
You get the point.
145
00:08:11,040 --> 00:08:13,170
You just get inside of the loop, buddy.
146
00:08:13,470 --> 00:08:15,240
You executed, you increment.
147
00:08:15,270 --> 00:08:18,450
What you have to do in this case is just the count itself.
148
00:08:18,600 --> 00:08:26,730
And then you are going to basically come to a point where these count will not be less than 10.
149
00:08:26,970 --> 00:08:33,960
So, for example, if we are going like this and we have all of the stories printed for the console,
150
00:08:33,960 --> 00:08:36,990
uplink to the console and we have nine.
151
00:08:37,020 --> 00:08:43,670
So we print the last Starwind Grimm and count by one and now count equals to 10.
152
00:08:44,340 --> 00:08:49,920
So at this point, we can definitely say that if we come back to check out this condition once again,
153
00:08:50,190 --> 00:08:52,470
the result of this condition, if.
154
00:08:52,930 --> 00:08:55,830
If 10 is less than 10 will be false.
155
00:08:56,220 --> 00:08:59,930
And that's why we are not going to execute the loop body anymore.
156
00:09:00,150 --> 00:09:08,190
We are just going to go onto the next command, which is return zero and the end of these end mean program.
157
00:09:08,370 --> 00:09:13,830
And actually, if we take a look, another look, here are the printed stories on the console.
158
00:09:13,840 --> 00:09:17,460
We can see that there is only nine stories printed out.
159
00:09:18,450 --> 00:09:21,450
Which, first of all, we wanted to print 10 of them.
160
00:09:21,450 --> 00:09:21,780
Right.
161
00:09:21,900 --> 00:09:24,570
So where did we mess up the last?
162
00:09:24,580 --> 00:09:28,920
Then we checked the condition count was 10 and the condition.
163
00:09:29,010 --> 00:09:31,830
If Dan is less than then happens to be false.
164
00:09:32,160 --> 00:09:35,820
So that's why we didn't bring the last star on the screen.
165
00:09:35,850 --> 00:09:37,050
The tenth star.
166
00:09:37,470 --> 00:09:43,770
So one of the ways to fix that is to modify the condition a little bit, to modify this condition by
167
00:09:43,770 --> 00:09:46,030
adding also the equality signs.
168
00:09:46,070 --> 00:09:51,670
So this will be as long as count is less than or equal to 10.
169
00:09:51,750 --> 00:09:56,880
As long as this condition happens to be true, we are going to print another star here, the 10th,
170
00:09:57,300 --> 00:09:58,830
which will satisfy.
171
00:09:59,830 --> 00:10:04,880
Actually, what we wanted it to do, so I hope that makes sense to you, how you should use the while
172
00:10:04,880 --> 00:10:07,100
Lupino received programming language.
173
00:10:07,610 --> 00:10:12,500
And I hope you got all the flow of how the loop's structure works.
174
00:10:12,800 --> 00:10:18,620
Basically, first of all, you start the loop statement, which is while in this case include the loop
175
00:10:18,620 --> 00:10:24,470
condition and the loop body and you executed until the condition is false.
176
00:10:24,560 --> 00:10:26,340
So that's a good practice, guys.
177
00:10:26,480 --> 00:10:28,670
And I hope you enjoyed this video.
178
00:10:28,760 --> 00:10:35,330
And I'll see in the next video where we are probably going to practice a little bit these wild loops.
179
00:10:35,600 --> 00:10:36,940
So I'll see you there.
16516
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.