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:02,310
So for that, we'll need code blocks.
2
00:00:02,430 --> 00:00:05,960
And first of all, let's define two variables of type in.
3
00:00:06,030 --> 00:00:07,410
Now one in two.
4
00:00:07,580 --> 00:00:10,710
So end number one and number two.
5
00:00:10,950 --> 00:00:16,620
And also, let's create a third variable that will simply store the result of the division and it will
6
00:00:16,620 --> 00:00:19,200
be something like each resolved.
7
00:00:19,620 --> 00:00:23,280
So once again, this variable result is of type end.
8
00:00:23,730 --> 00:00:29,970
And it will simply be used to store the result of the division between number one and number two.
9
00:00:30,270 --> 00:00:35,850
So now we can say that result will equal to now one over.
10
00:00:36,320 --> 00:00:38,830
Number two, assuming that number two is not zero.
11
00:00:38,850 --> 00:00:39,180
Right.
12
00:00:39,840 --> 00:00:45,570
Assuming number two is not equal to zero.
13
00:00:45,940 --> 00:00:46,470
All right.
14
00:00:46,890 --> 00:00:51,410
And then we will just use the printer message to print out the resolve to the screen.
15
00:00:51,420 --> 00:00:53,000
Will see resolved.
16
00:00:53,270 --> 00:00:53,940
Resolved.
17
00:00:54,240 --> 00:00:59,800
Equals to percentage the backslash can and then will go with resolved.
18
00:01:00,390 --> 00:01:05,040
But before we build and ran this program, we kind of forgot something.
19
00:01:05,040 --> 00:01:07,140
We forgot to initialize these variables.
20
00:01:07,150 --> 00:01:08,370
So let's put it.
21
00:01:08,760 --> 00:01:09,780
Now, one equals two.
22
00:01:09,780 --> 00:01:10,360
Five.
23
00:01:10,470 --> 00:01:12,270
And then two equals two.
24
00:01:12,270 --> 00:01:12,530
Two.
25
00:01:12,920 --> 00:01:19,520
Now, if we save and then we build and run it, we can see that the results should be gone.
26
00:01:19,540 --> 00:01:21,500
That the result is actually two.
27
00:01:21,720 --> 00:01:21,910
Right.
28
00:01:21,990 --> 00:01:25,940
We divided five by two and the result was simply too.
29
00:01:26,230 --> 00:01:27,880
So that's a K.
30
00:01:27,930 --> 00:01:36,690
Let's try to figure out what will happen if we simply to change these result from integer to a floating
31
00:01:36,690 --> 00:01:37,000
point.
32
00:01:37,020 --> 00:01:39,020
For example, it's used double.
33
00:01:39,360 --> 00:01:46,440
So now if we are to use double, will simply modify these percentages, these two percentage alef,
34
00:01:46,860 --> 00:01:49,800
and then we will use the result as we use it previously.
35
00:01:49,810 --> 00:01:50,700
So save it.
36
00:01:50,760 --> 00:01:51,720
Build and run.
37
00:01:52,020 --> 00:01:52,830
There you go.
38
00:01:52,940 --> 00:01:53,880
Still, too.
39
00:01:54,360 --> 00:01:58,290
But now it's two with a bunch of zeroes after words.
40
00:01:58,560 --> 00:01:59,940
So why is that?
41
00:02:00,150 --> 00:02:05,760
Why again, do we have the result of two when we've defined it to be of type double?
42
00:02:05,890 --> 00:02:09,210
We defined the result variable to be of type double.
43
00:02:09,660 --> 00:02:13,470
So we expected the result to be something like 2.5.
44
00:02:13,800 --> 00:02:14,130
Right.
45
00:02:14,460 --> 00:02:18,620
That division between number one and number two should give 2.5.
46
00:02:18,990 --> 00:02:21,000
But still we got just two.
47
00:02:21,300 --> 00:02:22,260
So what happened?
48
00:02:22,440 --> 00:02:23,700
Let's try to find out.
49
00:02:23,940 --> 00:02:26,500
The result variable is of type double.
50
00:02:26,610 --> 00:02:33,080
And it basically does contain numbers of the time we saw on the screen, two point zero zero zero is
51
00:02:33,120 --> 00:02:35,090
actually a floating point representation.
52
00:02:35,400 --> 00:02:40,890
And if we are to take a closer look at this line and line eight, we can see that on the left side of
53
00:02:40,890 --> 00:02:42,270
this assignment operation.
54
00:02:42,660 --> 00:02:49,220
There is this result and it will simply hold the value represented as a floating point number.
55
00:02:49,710 --> 00:02:56,140
But what interests us here is actually the right side of this assignment, number one over number two.
56
00:02:56,510 --> 00:02:59,760
And this leads us to the first rule I'm about to tell you.
57
00:02:59,940 --> 00:03:06,060
So the first rule I want you guys to know is that whenever you make any mathematical operation between
58
00:03:06,060 --> 00:03:07,990
two numbers of the same type.
59
00:03:08,040 --> 00:03:11,010
In this case, it's integer and integer.
60
00:03:11,020 --> 00:03:11,360
Right.
61
00:03:11,880 --> 00:03:17,160
So whenever you do any mathematical operation between two numbers of the same type, the result you
62
00:03:17,160 --> 00:03:17,740
will get.
63
00:03:17,760 --> 00:03:22,000
By default, you will be actually of the same particular type.
64
00:03:22,020 --> 00:03:22,380
Right.
65
00:03:22,410 --> 00:03:23,190
Which side?
66
00:03:23,460 --> 00:03:25,500
Previously in the slide itself.
67
00:03:26,070 --> 00:03:28,620
I discussed it in just a couple of moments.
68
00:03:29,130 --> 00:03:36,510
So the result of these guys or this division is going to be also an integer because both number one
69
00:03:36,540 --> 00:03:38,660
and number two are of type end.
70
00:03:38,970 --> 00:03:41,570
So the result is also going to be of type in.
71
00:03:41,790 --> 00:03:49,200
And please notice that the result itself on the right side before, before and I emphasize of these
72
00:03:49,200 --> 00:03:53,190
before it is assigned to the result itself.
73
00:03:53,700 --> 00:03:56,400
There is some mathematical operation going on here.
74
00:03:56,670 --> 00:04:02,010
And the result itself before it is being assigned is an integer number.
75
00:04:02,190 --> 00:04:09,750
So whenever you divide five by two, it is round to the lowest number A, which is in our case, is
76
00:04:09,750 --> 00:04:10,530
simply too.
77
00:04:10,800 --> 00:04:17,940
And now, once the right side has been calculated, has been found, that it's still only now we take
78
00:04:17,940 --> 00:04:21,690
these two as an integer and we assign it to a result.
79
00:04:22,170 --> 00:04:24,140
So the result will refer to this.
80
00:04:24,140 --> 00:04:26,820
S2 is two point zero zero zero.
81
00:04:26,940 --> 00:04:27,300
All right.
82
00:04:27,510 --> 00:04:29,070
So it will be something like that.
83
00:04:29,910 --> 00:04:37,110
It was two in the first place and then it became two point zero instead of being just something like
84
00:04:37,410 --> 00:04:40,970
two point five, as you would have expected.
85
00:04:40,980 --> 00:04:41,270
Right.
86
00:04:41,520 --> 00:04:44,490
And I hope this one is clear for you and you got the point.
87
00:04:44,760 --> 00:04:46,950
So let's proceed to the following question.
88
00:04:47,370 --> 00:04:49,350
How can we divide five?
89
00:04:49,800 --> 00:04:55,290
How can we divide five by two and still obtain two point five as the result?
90
00:04:55,650 --> 00:04:57,570
And this leads us to the second rule.
91
00:04:57,720 --> 00:04:59,760
And I call this rule an implicit.
92
00:04:59,990 --> 00:05:00,500
Casting.
93
00:05:00,740 --> 00:05:06,740
And the reason why it's called implicit is because this casting is being done behind the scenes.
94
00:05:07,140 --> 00:05:11,330
And we don't explicitly specify that we want to make type conversion.
95
00:05:11,630 --> 00:05:18,620
So the second rule, the implicit casting states that whenever you make mathematical operation between
96
00:05:18,680 --> 00:05:25,940
an integer and a floating point number, the result of this operation will be always treated as it would
97
00:05:25,940 --> 00:05:28,650
have been done between two floating points.
98
00:05:28,940 --> 00:05:32,290
Meaning that the result would be of a floating point-I.
99
00:05:32,660 --> 00:05:40,010
So, for example, if we take the number of three, which is an end and added 4.5, which is a free
100
00:05:40,040 --> 00:05:46,250
floating point type, then the result would be also of a floating point type, which is seven point
101
00:05:46,250 --> 00:05:46,660
five.
102
00:05:46,880 --> 00:05:48,790
And it's a floating point type, though.
103
00:05:49,400 --> 00:05:50,640
Three is I mean, the jaw.
104
00:05:50,690 --> 00:05:51,020
Right.
105
00:05:51,200 --> 00:05:58,280
So in addition to any plus and minus operations, this will also work for that, a vision.
106
00:05:58,640 --> 00:06:03,970
So if we change even one of these numbers, number one or number two, let's leave.
107
00:06:04,280 --> 00:06:05,240
One equals two.
108
00:06:05,240 --> 00:06:06,560
Five is an integer.
109
00:06:06,560 --> 00:06:09,830
And we'll just use double NUM to double num.
110
00:06:09,860 --> 00:06:13,940
Two equals to two point zero, which is pretty much the same.
111
00:06:14,190 --> 00:06:18,950
So in this case, if we are to run these code, we will see a different result.
112
00:06:18,980 --> 00:06:20,810
Let's try to build and run it.
113
00:06:20,900 --> 00:06:21,960
Build and run.
114
00:06:22,010 --> 00:06:22,490
Come on.
115
00:06:22,880 --> 00:06:25,610
So the result is actually two point five.
116
00:06:25,910 --> 00:06:28,010
And leave aside these zeroes at the end.
117
00:06:28,160 --> 00:06:30,050
And that's exactly what we expected.
118
00:06:30,080 --> 00:06:30,920
Which is great.
119
00:06:31,100 --> 00:06:37,640
So another option would be simply to use instead of now two as a double.
120
00:06:37,860 --> 00:06:40,410
It's live it as an integer.
121
00:06:40,560 --> 00:06:42,260
And number two equals two.
122
00:06:42,260 --> 00:06:42,640
Two.
123
00:06:42,980 --> 00:06:45,560
And just modifying one to be.
124
00:06:46,370 --> 00:06:47,100
To be of type.
125
00:06:47,120 --> 00:06:47,780
Double.
126
00:06:49,100 --> 00:06:49,660
Double.
127
00:06:50,610 --> 00:06:54,170
So now Y equals two five point zero.
128
00:06:55,070 --> 00:06:59,270
And if you tried to build and run it, you will see pretty much the same result.
129
00:06:59,290 --> 00:07:00,360
Two point five.
130
00:07:00,660 --> 00:07:07,180
And that happens because it's sufficient for us that even if one of these numbers that participate in
131
00:07:07,190 --> 00:07:11,630
this division operation will be of type of some floating point type.
132
00:07:12,050 --> 00:07:18,620
Then also the result before it is even assigned to this result variable, the result itself will also
133
00:07:18,620 --> 00:07:20,360
be treated as a floating point.
134
00:07:20,510 --> 00:07:25,250
And that's the reason why we don't lose any data in these type conversion.
135
00:07:25,580 --> 00:07:26,350
All right, guys.
136
00:07:26,390 --> 00:07:27,260
Good, good, good.
137
00:07:27,500 --> 00:07:34,980
But suppose that you began your program by setting up both number one and number two to be of type N,
138
00:07:35,000 --> 00:07:41,180
something like what we've done previously, number one equals two five and one two equals two two.
139
00:07:41,480 --> 00:07:44,630
And you don't want to change their tie for some reason.
140
00:07:44,630 --> 00:07:45,200
I don't know.
141
00:07:45,230 --> 00:07:50,750
Maybe the boss demands that these two variables should remain of type bar.
142
00:07:51,290 --> 00:07:54,290
You still need to calculate these results somehow.
143
00:07:54,530 --> 00:07:57,300
So for that, we will use a technique called Castellaneta.
144
00:07:57,560 --> 00:08:03,680
We know that thanks to the second rule, we said that it's enough that at least one of the participants
145
00:08:04,070 --> 00:08:08,030
in the mathematical operation would have been a floating point time.
146
00:08:08,510 --> 00:08:11,900
So then the whole result would have been as a floating point.
147
00:08:12,170 --> 00:08:17,690
And we also know that in this case, both number one and number two are of type in nature.
148
00:08:17,900 --> 00:08:23,210
So basically what we have to do is simply to take at least one of these numbers.
149
00:08:23,240 --> 00:08:30,620
Number one or number two, and cast them explicitly use casting into double, for example.
150
00:08:30,890 --> 00:08:37,490
And it will allow the computer to treat that mathematical operation as it is being done between two
151
00:08:37,490 --> 00:08:41,570
floating points number, which is exactly what we want to use.
152
00:08:41,570 --> 00:08:44,210
Casting, for example, on number two in this case.
153
00:08:44,600 --> 00:08:48,490
Let's define something like this one double.
154
00:08:49,040 --> 00:08:52,340
So we simply use the parentheses inside of this parentheses.
155
00:08:52,340 --> 00:08:57,160
We specify the type of casting that we want to make on these num do.
156
00:08:57,500 --> 00:09:00,430
So, for example, we know that name two in such a case.
157
00:09:00,470 --> 00:09:01,640
Let's remove this one.
158
00:09:02,090 --> 00:09:05,230
We know that num two in this example equals two two.
159
00:09:05,720 --> 00:09:09,140
And if we add this explicit casting before.
160
00:09:09,350 --> 00:09:09,840
All right.
161
00:09:09,890 --> 00:09:17,630
These casting will be applied on NUM two and it will simply change it from two to two point zero to
162
00:09:17,630 --> 00:09:19,370
a floating point of representation.
163
00:09:19,610 --> 00:09:26,720
So in this case, if we save it, then build and run this program, we'll see the expected 2.5 printed
164
00:09:26,720 --> 00:09:27,560
out on the screen.
165
00:09:27,900 --> 00:09:32,020
Although we used now one and num two as integers.
166
00:09:32,570 --> 00:09:35,560
So I hope that makes sense to you guys.
167
00:09:35,570 --> 00:09:36,500
And also you.
168
00:09:37,000 --> 00:09:41,650
You are not required only to use casting gone num two.
169
00:09:41,960 --> 00:09:47,390
You may also use these casting on the first num num one on the right here.
170
00:09:47,620 --> 00:09:47,990
All right.
171
00:09:48,250 --> 00:09:55,500
And if you are to run and build it, build and run into literacy, then the result is also the same.
172
00:09:55,520 --> 00:09:57,350
It's 2.5 also here.
173
00:09:58,100 --> 00:09:59,630
And all of that, thanks to the.
174
00:09:59,720 --> 00:10:06,840
Second rule where we've defined that it's sufficient for at least one one of the participants to be
175
00:10:06,840 --> 00:10:07,630
of type.
176
00:10:08,130 --> 00:10:14,140
For example, of type double, then the whole result of this operation will also be of type double.
177
00:10:14,360 --> 00:10:22,020
And then we can simply assign this result to the result variable on the left of the on this assignment
178
00:10:22,020 --> 00:10:22,470
operation.
179
00:10:22,770 --> 00:10:30,330
And also, please note there that the type of neither number one or number two after the casting operation
180
00:10:30,330 --> 00:10:31,920
will never be changed.
181
00:10:32,190 --> 00:10:35,800
Both number one and number two will still remain off type end.
182
00:10:36,690 --> 00:10:41,620
They were just converted or better say, casted to be.
183
00:10:41,670 --> 00:10:44,700
For example, here and just name one to be of type double.
184
00:10:44,970 --> 00:10:46,350
Just for this operation.
185
00:10:46,380 --> 00:10:47,430
Just for this line.
186
00:10:47,670 --> 00:10:50,910
So hopefully these casting makes sense to you guys.
187
00:10:51,360 --> 00:10:57,570
You could basically just also use here a double on both of them, both for number one and number two.
188
00:10:57,900 --> 00:11:04,560
And it will not change their result at all because it will already be treating these these separation.
189
00:11:04,860 --> 00:11:10,210
These mathematical division as an operation between two floating point numbers.
190
00:11:10,470 --> 00:11:14,760
So simply saying you can basically cast whatever you need.
191
00:11:15,150 --> 00:11:18,360
For example, you can cast a floating point to an integer.
192
00:11:18,600 --> 00:11:21,540
And this can be done by something like that.
193
00:11:22,170 --> 00:11:27,570
You specify in the parentheses the type that you want to cast a given value.
194
00:11:27,600 --> 00:11:28,990
So let's say Amed.
195
00:11:29,610 --> 00:11:36,690
And then you specify the floating point representation 5.1 and all of these expression.
196
00:11:36,720 --> 00:11:37,370
All of this.
197
00:11:38,370 --> 00:11:43,540
Everything we've written here can simply be treated as just fine.
198
00:11:43,800 --> 00:11:48,460
This will simply be an integer representation of these 5.1.
199
00:11:48,720 --> 00:11:55,590
And also you can do the same for seven point ninety five, let's say, seven point ninety five.
200
00:11:55,770 --> 00:12:00,830
So if there are these casting operation, these casting two integer representation.
201
00:12:01,260 --> 00:12:04,920
We will simply stay with the just the seven.
202
00:12:05,160 --> 00:12:07,140
So I hope that's clear for you guys.
203
00:12:07,380 --> 00:12:11,490
And to summarize everything up, casting is a very important topic.
204
00:12:11,520 --> 00:12:16,830
You must be familiar with because you will encounter with a lot in our programming career.
205
00:12:17,110 --> 00:12:25,080
And you must know how to tackle problems with casting, how to use them, when to use them, and basically
206
00:12:25,080 --> 00:12:29,580
whether it should be done implicitly or explicitly or whatever you like.
207
00:12:29,700 --> 00:12:35,880
And one of the examples is going to be shown in the next challenge where you will have to calculate
208
00:12:35,880 --> 00:12:40,140
the average grade between three given grades from the user.
209
00:12:40,350 --> 00:12:42,210
Because previously we didn't.
210
00:12:42,690 --> 00:12:46,590
When we solved this exercise of the average grade, we didn't specify.
211
00:12:46,590 --> 00:12:49,620
We didn't talk about anything related to casting.
212
00:12:49,620 --> 00:12:49,890
Right.
213
00:12:49,920 --> 00:12:57,870
Because an average grade will usually will not be in integer representation, but rather it will be
214
00:12:57,870 --> 00:13:05,310
a floating point number so that just now we know how to handle it and how to to take care of all of
215
00:13:05,310 --> 00:13:06,240
these casting.
216
00:13:06,420 --> 00:13:09,960
And of course, guys, there is much more depth to these rules.
217
00:13:09,990 --> 00:13:14,400
We've just seen and many new ones says that should be taken care of.
218
00:13:14,550 --> 00:13:21,420
But that will be for another course because these topics are much more advanced and require the understanding
219
00:13:21,420 --> 00:13:22,830
of lower levels.
220
00:13:23,040 --> 00:13:26,820
So hopefully you'll find this information valuable for you for now.
221
00:13:27,300 --> 00:13:33,750
And I will, of course, share my knowledge with you in the next advanced courses that I'm planning
222
00:13:33,750 --> 00:13:35,670
to create just for you.
223
00:13:36,060 --> 00:13:37,560
So thank you guys for watching.
224
00:13:37,890 --> 00:13:44,820
And I'll see you in the next challenge where we will solve and practice our casting skills.
225
00:13:45,150 --> 00:13:45,900
I'll see you there.
20297
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.