Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,300 --> 00:00:01,440
What is going on, guys?
2
00:00:01,470 --> 00:00:02,400
My name is Vlad.
3
00:00:02,490 --> 00:00:09,000
And in this video, we are going to take a look at for loops in C programming language at this point.
4
00:00:09,030 --> 00:00:14,760
We understand the concept and the main principles of for loops as we've discussed it in the previous
5
00:00:14,760 --> 00:00:15,300
video.
6
00:00:15,420 --> 00:00:17,780
And now it's time for some Hands-On.
7
00:00:17,850 --> 00:00:24,720
So supposedly you have to write a program that reads a number from the user and then prints all the
8
00:00:24,720 --> 00:00:26,620
numbers from one to you.
9
00:00:26,700 --> 00:00:28,340
That particular number.
10
00:00:28,400 --> 00:00:33,090
So first of all, we just create declaring non variable.
11
00:00:33,150 --> 00:00:35,650
Then we read it from the user and through a number.
12
00:00:36,000 --> 00:00:42,210
And based on the number of that the user inserts, we are going to print all the numbers, all the numbers
13
00:00:42,210 --> 00:00:44,540
from one to these particular numbers.
14
00:00:44,570 --> 00:00:51,300
So while if, for example, he entries in NUM equals to five, we are going to print one, two, three,
15
00:00:51,300 --> 00:00:52,110
four, five.
16
00:00:52,200 --> 00:00:53,040
Is that clear?
17
00:00:53,130 --> 00:00:59,060
And if the user inserts ten, then we are going to print all the numbers from one to ten and so on.
18
00:00:59,070 --> 00:01:00,270
Basically you got the idea.
19
00:01:00,330 --> 00:01:04,620
So one of the ways to do so would be by using the four loops.
20
00:01:04,770 --> 00:01:07,800
So basically the structure would be something like that.
21
00:01:07,860 --> 00:01:10,560
So first of all, you specify the four statement.
22
00:01:10,620 --> 00:01:11,730
You use the four.
23
00:01:12,120 --> 00:01:18,660
And then you are going to have the parentheses to hold the three states or condition was specified in
24
00:01:18,660 --> 00:01:19,680
the previous video.
25
00:01:20,010 --> 00:01:26,850
So first of all, we are going to have the initialization phase phase and initialization phase, which
26
00:01:26,850 --> 00:01:31,140
basically you specify that I will be equal to one.
27
00:01:31,530 --> 00:01:39,420
And you only run this command only for the first time that you happen to see these for a loop.
28
00:01:39,540 --> 00:01:40,800
So there you go.
29
00:01:40,800 --> 00:01:45,790
For example, you print this line for the screen, then you read the value from the user.
30
00:01:46,050 --> 00:01:50,820
And the first time that you come to this fourth statement to the force structure, only now you're going
31
00:01:50,820 --> 00:01:57,150
to set this value to be equal to one and then you move on to the loop condition.
32
00:01:57,270 --> 00:02:02,730
And in this condition, you actually check whether AI is less than or equal to NARM.
33
00:02:02,760 --> 00:02:08,280
And if the result of the condition happens to be true or better as say, as long as this condition happens
34
00:02:08,280 --> 00:02:11,640
to be true, you are going to execute the loop body.
35
00:02:11,760 --> 00:02:18,400
So on every duration, once again, on every duration that you are going to execute the slope, you're
36
00:02:18,450 --> 00:02:20,670
going to check if this condition is satisfied.
37
00:02:20,910 --> 00:02:21,930
If it's satisfied.
38
00:02:21,930 --> 00:02:23,700
If it's true, you are going to execute.
39
00:02:23,940 --> 00:02:26,730
If it's false, you are going to be kicked out.
40
00:02:26,760 --> 00:02:28,920
Let's say this way to be kicked out.
41
00:02:29,010 --> 00:02:32,160
Out of this for a loop end to proceed on with your program.
42
00:02:32,400 --> 00:02:38,970
So let's say that you've, Ryan, of these for the first time, you've ran the first iteration, you
43
00:02:38,970 --> 00:02:39,540
checked out.
44
00:02:39,850 --> 00:02:41,720
This condition happens to be true.
45
00:02:41,730 --> 00:02:43,050
One is less than five.
46
00:02:43,080 --> 00:02:44,460
Let's say that name is five.
47
00:02:44,550 --> 00:02:48,150
So you printed out one to the screen, right?
48
00:02:48,180 --> 00:02:50,610
Because the value of AI is just one.
49
00:02:50,610 --> 00:02:53,280
You printed out one to the screen on the first iteration.
50
00:02:53,580 --> 00:02:55,920
Then you come to the end of this loop body.
51
00:02:56,160 --> 00:02:57,240
And what happens now?
52
00:02:57,420 --> 00:03:04,200
Now, what happens is these Abdeen statement is being executed, meaning that AI is getting increased
53
00:03:04,500 --> 00:03:05,310
by one.
54
00:03:05,480 --> 00:03:05,830
Right.
55
00:03:05,970 --> 00:03:12,120
And from this point on, before we start the second iteration, I will be equal to two.
56
00:03:12,420 --> 00:03:15,960
So this happens after any duration is over.
57
00:03:16,350 --> 00:03:18,210
And we print it out on the screen.
58
00:03:18,300 --> 00:03:19,620
Then we incremented.
59
00:03:19,680 --> 00:03:21,330
We updated this statement.
60
00:03:21,390 --> 00:03:23,130
We incremented I by one.
61
00:03:23,280 --> 00:03:27,190
And then to enter the loop body to run the loop body once again.
62
00:03:27,210 --> 00:03:30,330
We check out if this condition is true or false.
63
00:03:30,630 --> 00:03:34,620
And in this case, two is less than or equals two to five.
64
00:03:34,670 --> 00:03:34,980
Right.
65
00:03:35,040 --> 00:03:36,000
This condition is true.
66
00:03:36,300 --> 00:03:39,660
Then we are going to execute the loop body once again.
67
00:03:39,700 --> 00:03:40,110
All right.
68
00:03:40,140 --> 00:03:43,020
So this is the new body we are going to executed once again.
69
00:03:43,230 --> 00:03:46,020
And we are going to print to to the screen.
70
00:03:46,160 --> 00:03:50,030
So also, we do it the same way for the 30 duration.
71
00:03:50,040 --> 00:03:52,150
We increment I by one.
72
00:03:52,200 --> 00:03:53,740
I will be equal to three.
73
00:03:53,870 --> 00:03:55,560
Three is less than five.
74
00:03:55,590 --> 00:03:57,930
We're going to print three to the screen.
75
00:03:58,260 --> 00:04:05,360
And so on until we reach some condition that I will be or it will be greater than five.
76
00:04:05,370 --> 00:04:05,690
Right.
77
00:04:05,790 --> 00:04:09,220
If it's five, five is less than or equal to NARM.
78
00:04:09,270 --> 00:04:10,950
So five will will be printed.
79
00:04:11,250 --> 00:04:16,200
And we will see all the numbers from one till four to five on the screen.
80
00:04:16,680 --> 00:04:22,280
And then we run this print f commented once again we increment I by one end.
81
00:04:22,540 --> 00:04:25,710
And now I will be equal to six.
82
00:04:25,830 --> 00:04:33,000
And six is not less than or equals to five than the result of this condition will be false.
83
00:04:33,120 --> 00:04:38,340
And that means that you are not going to execute this loop body and you're going to step out of the
84
00:04:38,340 --> 00:04:38,910
for a loop.
85
00:04:38,910 --> 00:04:44,250
And just to proceed with your programming in this case, just return zero and get it over with.
86
00:04:44,370 --> 00:04:44,850
All right.
87
00:04:44,880 --> 00:04:46,170
So I hope you've got the point.
88
00:04:46,170 --> 00:04:53,820
And just to make sure that you understand all the flow of of this code correctly, let's see another
89
00:04:53,820 --> 00:04:59,530
example where we go step by step in the most detailed explanation, because a lot of student.
90
00:04:59,710 --> 00:05:05,310
Are struggling at this point, and I don't want you guys to be one of these students that struggle at
91
00:05:05,310 --> 00:05:06,080
for loops.
92
00:05:06,270 --> 00:05:08,910
So here we come for the first time, we come from this line.
93
00:05:09,060 --> 00:05:16,800
And what we are doing is we set of the value of I to be equal to one than we are.
94
00:05:17,040 --> 00:05:17,340
All right.
95
00:05:17,370 --> 00:05:23,640
We have here the variable I we set it to be equal to one and then we are moving on the little body.
96
00:05:23,760 --> 00:05:27,000
So the first command that will be executed is the printf command.
97
00:05:27,000 --> 00:05:30,060
And we can see, let's say that this is the screen of your computer.
98
00:05:30,120 --> 00:05:31,560
This is the console application.
99
00:05:31,890 --> 00:05:35,490
We are going to see that one has been printed out to the screen.
100
00:05:35,640 --> 00:05:39,180
And now we come back to the end of the look of the loop, buddy.
101
00:05:39,840 --> 00:05:42,150
And what we should do from now on.
102
00:05:42,210 --> 00:05:46,230
From now on, we should move here and increment I by one.
103
00:05:46,260 --> 00:05:53,370
So now the variable I we created a variable I in the first steps of our program, this variable will
104
00:05:53,370 --> 00:05:54,730
be incremented by one.
105
00:05:54,780 --> 00:05:56,790
So I will be equal to two.
106
00:05:56,820 --> 00:06:00,530
From this point on before we start the second the duration.
107
00:06:00,630 --> 00:06:06,970
So now we are going to come here only only if this condition is satisfied.
108
00:06:06,980 --> 00:06:09,570
And to do we can see that I equals to two.
109
00:06:10,150 --> 00:06:12,090
That two is less than five.
110
00:06:12,120 --> 00:06:13,710
Nahm in our case was five.
111
00:06:13,830 --> 00:06:17,000
So now we are going to run this command once again.
112
00:06:17,010 --> 00:06:19,320
So print f the value of I.
113
00:06:19,410 --> 00:06:21,290
And now the value of Feis to us.
114
00:06:21,300 --> 00:06:23,490
So we are going to print to, to the screen.
115
00:06:24,060 --> 00:06:26,590
And now we are going to repeat this process.
116
00:06:26,600 --> 00:06:31,290
So we are going to increment I by one and I'm going to be equal to three.
117
00:06:31,560 --> 00:06:35,600
Then we come again for the loop body based on the result of this condition.
118
00:06:35,610 --> 00:06:36,540
We checked it on.
119
00:06:36,810 --> 00:06:38,010
The result is true.
120
00:06:38,220 --> 00:06:40,800
So now we execute this line one once again.
121
00:06:41,190 --> 00:06:44,360
And we can see that all the numbers will be printed as a screen.
122
00:06:44,370 --> 00:06:44,860
My bet.
123
00:06:45,770 --> 00:06:52,270
The user didn't enter a name equals to five, but rather he entered nom equals to ten.
124
00:06:52,340 --> 00:06:54,630
I forgot exactly what he enters.
125
00:06:54,630 --> 00:06:54,900
Right.
126
00:06:55,340 --> 00:06:56,670
It's late hours right now.
127
00:06:57,060 --> 00:06:58,650
So please forgive me, guys.
128
00:06:58,790 --> 00:07:05,880
And I basically hope that you got the idea of how this code works and you just run this little body
129
00:07:05,910 --> 00:07:08,670
over and over again until this condition.
130
00:07:08,940 --> 00:07:11,540
Feel this condition is will be false.
131
00:07:11,550 --> 00:07:13,860
And while it's not false, let's say.
132
00:07:14,250 --> 00:07:22,140
And on the end of every duration you use of this command to increment I by one in this example.
133
00:07:22,460 --> 00:07:23,820
It's it's just by one.
134
00:07:23,820 --> 00:07:26,280
It may be in other value.
135
00:07:26,280 --> 00:07:29,250
It may be another command, whatever you you choose.
136
00:07:29,770 --> 00:07:36,030
And also just remember that this line, that this command is executed just once when you come for the
137
00:07:36,030 --> 00:07:38,140
first loop for the first time.
138
00:07:38,350 --> 00:07:38,900
All right.
139
00:07:38,910 --> 00:07:43,770
So now let's take look at another example where we have to print just odd numbers.
140
00:07:43,860 --> 00:07:44,250
All right.
141
00:07:44,250 --> 00:07:47,700
From one app to a given number that the user provides.
142
00:07:47,980 --> 00:07:50,790
It's pretty similar to what we've done in the previous example.
143
00:07:51,030 --> 00:07:57,420
But there just now, we are going to print all the numbers from one of the old numbers sorry, all the
144
00:07:57,420 --> 00:07:58,810
old numbers from one field.
145
00:07:58,830 --> 00:07:59,860
These given no.
146
00:08:00,090 --> 00:08:01,800
So the steps are very similar.
147
00:08:01,830 --> 00:08:05,220
First of all, we will create AI to be equal to one.
148
00:08:05,760 --> 00:08:06,600
So there you go.
149
00:08:06,600 --> 00:08:08,070
You, first of all, created AI.
150
00:08:08,100 --> 00:08:08,520
All right.
151
00:08:08,550 --> 00:08:14,430
This is this line we we presented with this box and then we use said this value to be equal to one.
152
00:08:14,940 --> 00:08:20,610
And then we are going to execute the loop body and print one to the screen.
153
00:08:20,640 --> 00:08:22,500
So one will be printed here.
154
00:08:22,620 --> 00:08:25,350
Now we can see that the little buddy is over.
155
00:08:25,350 --> 00:08:26,670
And what we should do now.
156
00:08:26,910 --> 00:08:29,340
Should we increment just by one as previously?
157
00:08:29,340 --> 00:08:32,940
And then we will print one, two, three, four or five, six, seven and so on.
158
00:08:32,970 --> 00:08:35,010
But that's not exactly what we want to do.
159
00:08:35,280 --> 00:08:37,260
We want to bring just the odd numbers.
160
00:08:37,350 --> 00:08:43,410
So on every direct iteration, on the end of at the end of every iteration, we are going to increment
161
00:08:43,410 --> 00:08:45,210
the value of AI by two.
162
00:08:45,300 --> 00:08:49,920
So in this case, we run these command AI equals to the previous AI plus two.
163
00:08:49,920 --> 00:08:52,410
So the value of AI becomes three.
164
00:08:52,950 --> 00:08:59,220
And now once you come back to the Labadee, you're going to print three two discrete to the screen and
165
00:08:59,220 --> 00:09:01,740
then you increment the value by or by two.
166
00:09:02,010 --> 00:09:06,070
You are going to have here five and you're going to print five and so on.
167
00:09:06,120 --> 00:09:14,350
So for us, simply printing out all the odd numbers from one to these given narm that the user specifies.
168
00:09:14,880 --> 00:09:17,100
And that would be it for these video guys.
169
00:09:17,250 --> 00:09:25,740
And I think and I hope you understand now much better how the four loops work and see how this syntax
170
00:09:25,740 --> 00:09:26,400
looks like.
171
00:09:26,790 --> 00:09:30,680
What are all these commands to what they relayed.
172
00:09:31,110 --> 00:09:32,110
Which commands.
173
00:09:32,140 --> 00:09:32,610
Ron.
174
00:09:32,610 --> 00:09:33,990
Before the iteration.
175
00:09:34,020 --> 00:09:35,130
After the iteration.
176
00:09:35,130 --> 00:09:36,840
What is the condition that is checked.
177
00:09:37,680 --> 00:09:40,530
If the loop body should be executed or not.
178
00:09:40,650 --> 00:09:45,330
And we've seen full examples demonstrated line by line, command by command.
179
00:09:45,600 --> 00:09:47,920
What commands are executed and when?
180
00:09:48,060 --> 00:09:50,850
So I think you can say that, you know, for a loops.
181
00:09:50,880 --> 00:09:56,220
And now I would say that it's time to give it a little bit of practice on your own.
182
00:09:56,370 --> 00:09:59,100
So I'll see you in the next.
183
00:09:59,520 --> 00:10:00,390
Challenge.
184
00:10:00,540 --> 00:10:01,170
Good bye, guys.
16216
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.