Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,480 --> 00:00:03,900
So what are the do while loops?
2
00:00:04,080 --> 00:00:11,430
Let's say that we would like to make a simple program that will help Sellar seen supermarkets to sum
3
00:00:11,430 --> 00:00:15,420
up the price of all products in a given or other.
4
00:00:15,650 --> 00:00:21,390
So the seller is probably going to some the prices for each product, something like one plus two plus
5
00:00:21,390 --> 00:00:23,580
one plus five and so on.
6
00:00:23,580 --> 00:00:23,990
Right.
7
00:00:24,120 --> 00:00:25,710
Let's live it as in the jurors.
8
00:00:25,710 --> 00:00:32,310
For this example, though, in the real life, it will probably be of a floating point type.
9
00:00:32,730 --> 00:00:36,690
But that's something that we can modify when whenever we like.
10
00:00:36,750 --> 00:00:42,870
So we want to develop a program that all it will do is simply this operation.
11
00:00:43,110 --> 00:00:46,800
It will sum up all the prices until a given price.
12
00:00:47,040 --> 00:00:50,010
Let's say zero will be inserted.
13
00:00:50,100 --> 00:00:50,670
All right.
14
00:00:50,820 --> 00:01:00,090
I mean, it makes sense to use a zero price as a stepping condition because it simply wants change the
15
00:01:00,090 --> 00:01:03,060
total price and nothing in our code.
16
00:01:03,210 --> 00:01:10,470
So basically, to summarize, the seller will insert prices of different products one after the other.
17
00:01:10,830 --> 00:01:17,790
And once he will insert a zero, this will mean that the order is completed and that we have to print
18
00:01:18,030 --> 00:01:22,410
the total price to let the customer pay what he has to pay.
19
00:01:22,530 --> 00:01:29,280
So let's take a closer look to see how it looks like in our program in see, I've just added it here
20
00:01:29,280 --> 00:01:32,910
for you guys that we will be more convenient for us to take a look.
21
00:01:33,030 --> 00:01:39,810
So first of all, we create two variables a price variable and a total price variable, which will hold
22
00:01:40,680 --> 00:01:47,910
the total amount of all the prices for all the products we've inserted so far and the price that will
23
00:01:48,420 --> 00:01:55,950
simply read a given price at every time and every duration because we are going to use Loop's here.
24
00:01:56,040 --> 00:02:02,210
So we first of all, like in the previous milestone, we print a nice message, please enter a price.
25
00:02:02,490 --> 00:02:06,540
Then we read the value of the price and story and say the price variable.
26
00:02:06,660 --> 00:02:12,840
And then we test how the condition wild price does not is not equal is to zero.
27
00:02:13,080 --> 00:02:15,960
We are going to run the loop body and in the loop body.
28
00:02:15,960 --> 00:02:23,790
We are going to add the given price to the total price and read the next price one after the other and
29
00:02:23,790 --> 00:02:24,360
so on.
30
00:02:24,390 --> 00:02:32,400
Until we reach a price of zero and once we reach the price of zero, we simply are going to get out
31
00:02:32,400 --> 00:02:40,530
of the slope and to print the total or their price, which will be the total price, the value in inside
32
00:02:40,530 --> 00:02:41,460
of this variable.
33
00:02:41,570 --> 00:02:47,670
But if we are to take a closer look at this program, we can clearly see that there are two repeating
34
00:02:47,670 --> 00:02:48,480
lines here.
35
00:02:48,600 --> 00:02:55,620
These print F and SCANA flying these two lines and these two lines that they are basically identical.
36
00:02:55,710 --> 00:02:59,130
So we can see that we have some sort of code duplication.
37
00:02:59,460 --> 00:03:02,100
And it's basically just unnecessary.
38
00:03:02,220 --> 00:03:07,050
We should try to avoid code duplications whenever we can.
39
00:03:07,470 --> 00:03:11,070
So what basically can we do about eating this example?
40
00:03:11,100 --> 00:03:15,380
Can we do something differently or should we just leave it as is?
41
00:03:15,540 --> 00:03:17,790
These code duplicating two places.
42
00:03:17,940 --> 00:03:25,140
And as the title suggests, we are going to use here another structure of loops, not the while loop,
43
00:03:25,140 --> 00:03:29,220
as we've seen here and exactly as we've done in the previous milestone.
44
00:03:29,490 --> 00:03:35,140
We are going to use do Wyles in do while loops in C.
45
00:03:35,250 --> 00:03:37,110
So how A do while looks like.
46
00:03:37,560 --> 00:03:38,160
Let's see.
47
00:03:38,250 --> 00:03:40,710
So it's very similar to a while loop.
48
00:03:40,800 --> 00:03:41,290
All right.
49
00:03:41,340 --> 00:03:42,480
It's very similar.
50
00:03:42,570 --> 00:03:44,460
It's do while and there is while.
51
00:03:44,460 --> 00:03:45,450
There is a do while.
52
00:03:46,020 --> 00:03:47,510
And it looks like the following.
53
00:03:47,520 --> 00:03:54,800
First of all you specify first counts the do statement and then you have the curly brackets that specify
54
00:03:54,840 --> 00:04:01,170
the block of commands that that there is going to be on there, which is actually the loop body.
55
00:04:01,320 --> 00:04:06,060
And the commands here are going to be pretty much the same as they were in the while loop.
56
00:04:06,090 --> 00:04:15,840
So you just preens print out a corresponding message to the user to enter a price and then you read
57
00:04:16,070 --> 00:04:17,910
a value from the console.
58
00:04:18,270 --> 00:04:24,840
Then you add it to the thall a total price and then you specify that it's all of these curly bracket
59
00:04:25,220 --> 00:04:32,040
is the loop body and it's going to run while the price is not equals to zero.
60
00:04:32,130 --> 00:04:35,880
So basically it's the same as the previous while loop.
61
00:04:36,330 --> 00:04:42,750
The only difference is that the first time we are going to hit that do while loop, the first time we're
62
00:04:42,750 --> 00:04:49,890
going to come here, we will run all of the loop body commands, regardless of the condition, regardless
63
00:04:49,890 --> 00:04:51,750
if the price is zero or not.
64
00:04:52,050 --> 00:04:56,220
We are, first of all, going to execute the loop body for the first time.
65
00:04:56,550 --> 00:04:58,710
Then we are going to check the condition.
66
00:04:58,980 --> 00:04:59,880
And if the condition.
67
00:05:00,160 --> 00:05:00,690
True.
68
00:05:01,040 --> 00:05:02,900
We are going to run it once again.
69
00:05:03,230 --> 00:05:08,930
And if it falls, we are going to step out of the slope and to proceed with this line.
70
00:05:09,020 --> 00:05:10,070
So pretty simple.
71
00:05:10,110 --> 00:05:17,080
We run the commands regardless of the condition, and then we check the condition and run the durations
72
00:05:17,150 --> 00:05:22,670
over and over again, just like using the standard while loop that we've seen previously.
73
00:05:23,060 --> 00:05:24,680
So that's pretty simple, guys.
74
00:05:24,710 --> 00:05:26,540
The logic is pretty straightforward.
75
00:05:26,900 --> 00:05:33,380
All you have to do is probably just get used this syntax in C language.
76
00:05:33,500 --> 00:05:35,780
And just to make it more clear to you.
77
00:05:36,680 --> 00:05:44,690
Let's make a comparison, a full comparison with the while loop, how the code maps from one one type
78
00:05:44,690 --> 00:05:48,800
of form of writing the code to another type of writing.
79
00:05:48,840 --> 00:05:50,120
These the same code.
80
00:05:51,530 --> 00:05:53,150
One option we use the while loop.
81
00:05:53,420 --> 00:05:55,790
And the second option we the do while loops.
82
00:05:55,910 --> 00:06:00,620
So the first two lines that we've seen previously, that print F and the scanner.
83
00:06:01,700 --> 00:06:07,700
That was before we entered the wire loop is actually mapped.
84
00:06:07,770 --> 00:06:08,840
There was this guy.
85
00:06:08,960 --> 00:06:10,940
We can see that in both cases.
86
00:06:11,240 --> 00:06:17,540
These two lines are going to be executed regardless of their result of these conditions here or these
87
00:06:17,540 --> 00:06:18,350
conditions here.
88
00:06:18,470 --> 00:06:24,470
And we can see that we have also on both sides the wild statement, the wild statement and the condition,
89
00:06:24,740 --> 00:06:26,590
which is just the same.
90
00:06:26,600 --> 00:06:26,900
Right.
91
00:06:27,110 --> 00:06:37,520
It specifies the loop condition and it will be whenever the price will be, it will equal to zero.
92
00:06:37,870 --> 00:06:40,070
All we will step out of this loop.
93
00:06:40,190 --> 00:06:42,230
So it's the same in both cases.
94
00:06:42,380 --> 00:06:45,770
And also, if we take a look at the loop body in the wild.
95
00:06:46,070 --> 00:06:48,770
In the wild structure, it is the same.
96
00:06:48,950 --> 00:06:49,400
All right.
97
00:06:49,430 --> 00:06:55,400
If you take a look at these the same, just that the total price here is a third line while here, it's
98
00:06:55,700 --> 00:06:56,630
in the first line.
99
00:06:56,870 --> 00:07:01,970
So it's mapped pretty much the same way as we've done it in the wild love.
100
00:07:02,030 --> 00:07:05,540
So the same with the do while the body is pretty much the same.
101
00:07:05,840 --> 00:07:07,730
Just organized a little bit different.
102
00:07:08,000 --> 00:07:16,880
So what we can learn here is that in this case, where in this such cases, let's say when we know that
103
00:07:16,880 --> 00:07:24,560
the same commands command one command, a couple of comments should be executed before before the little
104
00:07:24,560 --> 00:07:25,100
body.
105
00:07:25,370 --> 00:07:29,220
And also the same commands are going to be executed inside of the loop, buddy.
106
00:07:29,690 --> 00:07:36,320
In these cases, we are going to prefer to use a do wild structure because we want to eliminate the
107
00:07:36,320 --> 00:07:37,940
usage of code duplication.
108
00:07:38,180 --> 00:07:44,180
When whenever we we actually can, because code applications are very hard to maintain.
109
00:07:44,180 --> 00:07:50,510
And for example, if on another day you will find out that here in the Preened Def Command, you want
110
00:07:50,510 --> 00:07:53,630
to just change the message, print it out through the screen.
111
00:07:53,660 --> 00:08:00,620
Then you have to remember that you must change all all of the messages that are duplicated inside of
112
00:08:00,620 --> 00:08:01,230
this code.
113
00:08:01,250 --> 00:08:02,780
And to tell you the truth.
114
00:08:03,560 --> 00:08:06,200
That's not something that happens.
115
00:08:06,230 --> 00:08:08,750
You will, if you will, use code duplications.
116
00:08:09,180 --> 00:08:17,240
You are actually going to probably experience a lot of bugs in your program that are not so easy to
117
00:08:17,420 --> 00:08:17,840
detect.
11396
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.