Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,560 --> 00:00:05,090
At this point, you should already be well familiar with if statements.
2
00:00:05,180 --> 00:00:08,210
And what are the main principles of control flow?
3
00:00:08,880 --> 00:00:16,100
And in this video, we are going to review the code from one of our previous examples and build our
4
00:00:16,100 --> 00:00:24,650
way up in the most detailed explanation and learn how and when we could and actually should use switch
5
00:00:24,650 --> 00:00:27,380
cases in C programming language.
6
00:00:27,590 --> 00:00:33,530
So in this example, we've tested out some conditions and for each of these options, we've printed
7
00:00:33,530 --> 00:00:35,450
out a corresponding message.
8
00:00:35,570 --> 00:00:42,740
And if you take a closer look, you will see that we checked whether a condition is true or false for
9
00:00:42,740 --> 00:00:44,390
a range of numbers.
10
00:00:44,480 --> 00:00:51,710
For example, if we take the first condition, which is if grade is greater or equals to 80, that may
11
00:00:51,710 --> 00:00:55,700
be true for all numbers, which are basically 80 or higher.
12
00:00:55,710 --> 00:00:56,110
Right.
13
00:00:56,630 --> 00:01:01,070
So it can be true for a variety of options, for a range of numbers.
14
00:01:01,280 --> 00:01:07,700
So it can be true for 80, 81, 82, 83, up to 95, 96 and so on.
15
00:01:07,750 --> 00:01:10,880
All numbers that are greater than 80.
16
00:01:11,210 --> 00:01:15,320
This condition will result in a true right.
17
00:01:15,500 --> 00:01:16,340
It will be true.
18
00:01:16,490 --> 00:01:23,190
And then we also had the ALS if statement that checks whether or grade equals or a higher event, 60,
19
00:01:23,480 --> 00:01:25,940
but it less than 80.
20
00:01:26,090 --> 00:01:33,380
And similarly, this condition will be true, will be true for all number is between 60 to 80.
21
00:01:33,380 --> 00:01:33,740
Right.
22
00:01:33,950 --> 00:01:37,370
60, 61, 62 and so on, up to 80.
23
00:01:37,520 --> 00:01:44,150
So once again, this means that these different conditions may be true for a range of numbers.
24
00:01:44,360 --> 00:01:50,750
And of course, we can take a look at the last at the last condition when we use the LS the EL statement,
25
00:01:50,750 --> 00:01:54,560
when the grade is less probably is less than 60.
26
00:01:54,890 --> 00:02:02,870
Then also here, you may have a lot of other options that this condition will be satisfied like 59,
27
00:02:02,870 --> 00:02:06,110
58, 57 and so on.
28
00:02:06,200 --> 00:02:06,500
Right.
29
00:02:06,560 --> 00:02:08,900
Everything that is below 60.
30
00:02:09,050 --> 00:02:12,660
But the thing, guys, is that these range of conditions.
31
00:02:12,670 --> 00:02:15,050
So that's not always going to be the case.
32
00:02:15,400 --> 00:02:21,770
There are times when the conditions may be true just for a single value, not for a range of values.
33
00:02:22,310 --> 00:02:28,940
And I want to give you an additional example that hopefully will be much more interesting to you before
34
00:02:28,940 --> 00:02:29,630
we move on.
35
00:02:29,750 --> 00:02:30,200
All right.
36
00:02:30,350 --> 00:02:37,310
So let's talk about a grading system that works with the letters A, B, C, D, and athletes C.
37
00:02:37,880 --> 00:02:40,250
And this grading system is pretty simple.
38
00:02:40,610 --> 00:02:44,210
Some of you may be familiar with it, but for those who are not.
39
00:02:44,270 --> 00:02:46,430
I will briefly explain it to you guys.
40
00:02:46,610 --> 00:02:49,430
So the grades here are represented as letters.
41
00:02:49,550 --> 00:02:54,880
And there is no precise grade like 96 or 97 or 72.
42
00:02:55,100 --> 00:02:58,400
Like we've seen previously in our previous examples.
43
00:02:58,520 --> 00:03:06,140
But rather, each letter in this rating system represents a range of possible grades of possible numbers
44
00:03:06,470 --> 00:03:10,430
in the numerical rating system that we've seen in previous examples.
45
00:03:10,880 --> 00:03:18,920
So a grade A represents the range of grades of numbers of grades from ninety to a hundred.
46
00:03:19,040 --> 00:03:24,090
Grade B represents the range of grades from 80 to 89.
47
00:03:24,560 --> 00:03:28,940
The results of grade C that we presents on the range of No.
48
00:03:29,090 --> 00:03:32,420
The range of grades between 70 to 79.
49
00:03:32,810 --> 00:03:33,940
There is also D.
50
00:03:34,040 --> 00:03:40,580
And lastly, lastly, we have the F grade, which represents that you've failed on your exam.
51
00:03:40,790 --> 00:03:47,480
And what we're going to do now is to write some converting program which will simply get a letter from
52
00:03:47,600 --> 00:03:54,290
the user and then print out the range of associated grades from the other grading system, just like
53
00:03:54,290 --> 00:03:56,840
we can see here on the screen on the left.
54
00:03:57,200 --> 00:04:03,650
We can see that we will get a letter either, which will be A, B, C, D or F, and then we will print
55
00:04:03,650 --> 00:04:07,430
a corresponding message with the range of numbers.
56
00:04:07,460 --> 00:04:11,570
The range of values associated with that given letter.
57
00:04:11,690 --> 00:04:16,910
So, for example, if we studied well for our exam and received an A..
58
00:04:17,240 --> 00:04:19,460
Then we should print out the message.
59
00:04:19,820 --> 00:04:22,460
Your grade is between 92 to a hundred.
60
00:04:22,640 --> 00:04:26,300
Or if we got a C on the exam, then we should print something like that.
61
00:04:26,360 --> 00:04:31,730
Your grade is between 70 to 79 simply based on this formula.
62
00:04:32,120 --> 00:04:37,520
Is that you can see here on the left, you are going to print an associated message.
63
00:04:37,610 --> 00:04:38,480
Is that clear?
64
00:04:38,510 --> 00:04:40,100
What we have to do right now.
65
00:04:40,220 --> 00:04:45,050
And before we move on, I want to stump you guys, Jim, just for a moment.
66
00:04:45,080 --> 00:04:45,590
All right.
67
00:04:46,040 --> 00:04:54,260
And let you think about how cool is that that you can actually write your own program that does something.
68
00:04:54,260 --> 00:04:55,850
You wanted it to do it right.
69
00:04:56,570 --> 00:04:59,960
You can think about an idea like the one we can see on the screen.
70
00:05:00,810 --> 00:05:07,350
And then you think about the idea and you can implement it by writing, by developing a program that
71
00:05:07,470 --> 00:05:13,710
gets a grade from one grading system and converts that grade to another grading system.
72
00:05:13,800 --> 00:05:16,950
Think about how much progress have you made so far?
73
00:05:17,370 --> 00:05:20,610
And I want to let you know that you should be proud of yourself.
74
00:05:20,700 --> 00:05:25,800
And now with that motivation being said, let's come back to our program, shall we?
75
00:05:25,920 --> 00:05:30,180
So here we can see basically the template for our code.
76
00:05:30,300 --> 00:05:34,020
I've written it in C and just embedded here in this presentation.
77
00:05:34,440 --> 00:05:41,010
We can see that, first of all, we create a variable called grade and it's of type CHA, right?
78
00:05:41,160 --> 00:05:42,650
We're using a character.
79
00:05:42,810 --> 00:05:49,670
Then we read the value of this character and then we are going just with the if else statement.
80
00:05:49,720 --> 00:05:49,890
So.
81
00:05:49,890 --> 00:05:50,310
All right.
82
00:05:50,430 --> 00:05:55,360
So we've talked about a case where we've received a we've received a letter A..
83
00:05:55,440 --> 00:05:57,660
And what should we present if that's the case?
84
00:05:57,720 --> 00:06:00,420
So if great equals to the letter A..
85
00:06:00,720 --> 00:06:04,170
We should bring that grade is between ninety to a hundred.
86
00:06:04,320 --> 00:06:07,100
Now we we've received a letter B.
87
00:06:07,500 --> 00:06:12,220
We should go through these condition and print that a grade is between 80 to 89.
88
00:06:12,630 --> 00:06:14,880
Also for a C, for a D.
89
00:06:15,210 --> 00:06:18,210
And for now I'm not putting how.
90
00:06:18,300 --> 00:06:21,540
Here in this example, I'm not putting the letter F.
91
00:06:21,960 --> 00:06:30,170
I'm just going with the LS condition that brings out that there was an error and you didn't specify
92
00:06:30,210 --> 00:06:31,580
A, B, C or a D.
93
00:06:31,770 --> 00:06:32,070
Right.
94
00:06:32,650 --> 00:06:37,530
We did not include here the F option, although it's mentioned here in the print F command.
95
00:06:38,010 --> 00:06:44,470
So we we basically can handle four cases and A, B, C and D.
96
00:06:44,670 --> 00:06:45,120
All right.
97
00:06:45,420 --> 00:06:48,840
Otherwise we are going to print an error message to the screen.
98
00:06:48,990 --> 00:06:56,550
So this program, this whole structure of it, if it will work, but we can say that it starts getting
99
00:06:56,550 --> 00:06:58,170
a little bit messy, don't you think?
100
00:06:58,500 --> 00:07:04,290
All of these Eve else, if l.c if and then there else if at some point.
101
00:07:04,380 --> 00:07:04,980
All right.
102
00:07:05,100 --> 00:07:11,400
You would like to add support also for grade F, which is currently not here.
103
00:07:11,600 --> 00:07:11,890
Right.
104
00:07:12,390 --> 00:07:16,200
You will need to find the exact place in code where you should added.
105
00:07:16,260 --> 00:07:18,210
Maybe you should edit here or here.
106
00:07:18,210 --> 00:07:19,330
Or maybe it doesn't matter.
107
00:07:19,350 --> 00:07:19,650
Right.
108
00:07:19,950 --> 00:07:21,570
But you have to think about it.
109
00:07:22,230 --> 00:07:28,620
Where you should add the condition to check if the grade equals to an F.
110
00:07:28,680 --> 00:07:32,300
So although this code works, as we just mentioned.
111
00:07:32,440 --> 00:07:32,720
Right.
112
00:07:32,730 --> 00:07:36,390
If you tried to run it on your own, then you will see that it works.
113
00:07:36,990 --> 00:07:41,220
There is a better elegant approach for solving such problems.
114
00:07:41,310 --> 00:07:43,350
And it should be done using this.
115
00:07:43,350 --> 00:07:44,540
Which cases.
116
00:07:44,670 --> 00:07:51,540
So switch case source, which statement is also a control mechanism which allows us to compare a given
117
00:07:51,540 --> 00:07:53,700
variable with multiple cases.
118
00:07:54,120 --> 00:07:57,690
And I'll just I'll I'll show you this right now.
119
00:07:57,720 --> 00:08:03,960
But I just want you to take another look at this example and understand that it's getting kind of messy
120
00:08:03,960 --> 00:08:10,360
when we make comparisons between a given variable to a is a certain value.
121
00:08:10,500 --> 00:08:10,770
Right.
122
00:08:10,800 --> 00:08:16,200
Not to a range of values, as we've seen previously, but to a certain value.
123
00:08:16,230 --> 00:08:20,020
There is a better way to handle it by using switch cases mechanism.
124
00:08:20,160 --> 00:08:27,270
So switch cases, a control mechanism that allows us to compare a given variable with multiple cases.
125
00:08:27,750 --> 00:08:34,830
And for a case that the mechanism finds a match, meaning the condition is true, then it allows associating
126
00:08:34,830 --> 00:08:38,820
a block of commands to be executed for that particular case.
127
00:08:38,940 --> 00:08:42,010
But this values and remember, this one is important.
128
00:08:42,060 --> 00:08:45,510
These values should always be discreet.
129
00:08:45,780 --> 00:08:46,110
Right.
130
00:08:46,170 --> 00:08:51,240
Not a range of numbers, but a single unique value, something discrete.
131
00:08:51,330 --> 00:08:57,930
And now I want us to take a look at how the template, how the structure of this which statement looks
132
00:08:57,930 --> 00:09:00,390
like in C programming language.
133
00:09:00,450 --> 00:09:02,640
And then explain it in more details.
134
00:09:02,820 --> 00:09:07,050
So first of all, we just create the variable grade as previously.
135
00:09:07,170 --> 00:09:09,810
Then we write the word switch.
136
00:09:10,110 --> 00:09:10,560
All right.
137
00:09:10,580 --> 00:09:12,760
We'll write the words switch enough.
138
00:09:12,770 --> 00:09:16,860
The words we provide in the parentheses, the variable.
139
00:09:16,890 --> 00:09:20,190
You would like to check the variable itself.
140
00:09:20,220 --> 00:09:20,730
Great.
141
00:09:20,730 --> 00:09:21,540
In this case.
142
00:09:21,690 --> 00:09:25,590
And we know that grade should hold only discrete values.
143
00:09:25,590 --> 00:09:25,920
Right.
144
00:09:25,950 --> 00:09:28,590
Either way, it's A, B, C or D.
145
00:09:28,940 --> 00:09:33,480
We are not talking here about testing any ranges for a grade.
146
00:09:33,630 --> 00:09:39,750
We simply want to ask if it equals to one of these given character as A, B, C already.
147
00:09:39,900 --> 00:09:40,440
That's it.
148
00:09:40,560 --> 00:09:42,630
So the syntax goes like this.
149
00:09:42,690 --> 00:09:46,110
We first of all specify the curly brackets.
150
00:09:46,140 --> 00:09:53,060
The block of commands that are associated with the switch case are mechanism in C.
151
00:09:53,250 --> 00:09:59,790
And now we will specify all the cases, all the cases of that grade variable may be.
152
00:10:00,300 --> 00:10:05,310
And they associate it commands that should be executed for each of these case.
153
00:10:05,460 --> 00:10:09,930
So the first case refers to one, the grade equals to a..
154
00:10:09,990 --> 00:10:12,600
So we just use case.
155
00:10:12,690 --> 00:10:16,170
And then we specify the value a character.
156
00:10:16,330 --> 00:10:22,200
A, it's almost the same as you would have written if rade equals equals to a letter A..
157
00:10:22,650 --> 00:10:24,960
And you can see that right after the case.
158
00:10:24,990 --> 00:10:26,520
We use here a column.
159
00:10:26,850 --> 00:10:32,880
And it simply lets you associate a bunch of commands related to that particular case.
160
00:10:33,300 --> 00:10:37,770
Meaning if that case seems to be seems to be happening to be true.
161
00:10:37,890 --> 00:10:38,350
All right.
162
00:10:38,370 --> 00:10:43,560
If that case happens to be true, then we will execute the associated commands.
163
00:10:43,830 --> 00:10:45,990
So we will print on the screen the grade.
164
00:10:46,470 --> 00:10:50,760
The degrade is between nineteen ninety to a hundred.
165
00:10:50,860 --> 00:10:52,350
So here you go.
166
00:10:52,350 --> 00:10:58,530
You associate a bunch of commands with a given case, just like we did previously with the IEF else,
167
00:10:58,830 --> 00:11:00,390
just by using cases.
168
00:11:00,510 --> 00:11:07,050
So for now, maybe these break line, these break and the semicolon at the end, maybe it's bothering
169
00:11:07,050 --> 00:11:09,840
you a little bit, but just leave it aside for now.
170
00:11:09,840 --> 00:11:11,430
We'll come back to it later on.
171
00:11:11,550 --> 00:11:14,010
So now we can add another case, right.
172
00:11:14,040 --> 00:11:21,150
Because we are over we are done with the case where the grade equals to a we can move on to another
173
00:11:21,150 --> 00:11:28,260
case and specify the second case, which is case B, and now we can specify the associated commands
174
00:11:28,500 --> 00:11:30,750
with this particular case, its goal.
175
00:11:30,930 --> 00:11:37,170
It goes like this print f print F grade is between eighty to eighty nine.
176
00:11:37,320 --> 00:11:43,080
And also it goes the same for a case C and case D, pretty much the same.
177
00:11:43,860 --> 00:11:51,240
You just specifies the case and the value for the variable grade that you are running this which case
178
00:11:51,300 --> 00:11:51,630
on.
179
00:11:52,170 --> 00:11:58,140
And you just specify the associated commands for each given case.
180
00:11:58,710 --> 00:12:04,230
So once again, you first of all specify the word case, then the value of that case.
181
00:12:04,650 --> 00:12:12,450
And if this which variable value is the same as specified in the case A, B, C or the in this example,
182
00:12:12,930 --> 00:12:18,150
you should execute the associated block of commands for this particular case.
183
00:12:18,450 --> 00:12:20,430
So I hope that makes sense to you guys.
184
00:12:20,490 --> 00:12:21,750
Nothing complicated here.
185
00:12:21,870 --> 00:12:28,440
And this simply allows us to organize the code in a little bit more structured way.
186
00:12:28,980 --> 00:12:36,990
And it also gives us flexibility with modifying and even adding new cases as your program being continuously
187
00:12:36,990 --> 00:12:37,750
developed.
188
00:12:37,810 --> 00:12:38,190
All right.
189
00:12:38,490 --> 00:12:41,190
These are some terms that you will learn in the future.
190
00:12:41,520 --> 00:12:48,480
But it's important for me to tell you these terms of continuous development and how you modify your
191
00:12:48,480 --> 00:12:48,840
code.
192
00:12:49,230 --> 00:12:58,710
It should always be as easy as possible and as long as understandable as you can make this program.
193
00:12:58,830 --> 00:13:05,430
So, for example, if you would like to add support for F right for F grade, then you should just add
194
00:13:05,430 --> 00:13:10,590
an associated case for that without changing anything in the structure of the code just like that.
195
00:13:10,890 --> 00:13:14,850
Simply adding additional case and its associated commands.
196
00:13:15,000 --> 00:13:15,720
All right.
197
00:13:15,750 --> 00:13:16,500
We're good.
198
00:13:16,500 --> 00:13:17,190
We're good.
199
00:13:17,220 --> 00:13:18,970
We're moving forward.
200
00:13:19,020 --> 00:13:21,120
And so we're almost done.
201
00:13:21,150 --> 00:13:25,550
Just last thing we should talk about in this.
202
00:13:25,560 --> 00:13:27,750
Which statement is the default line?
203
00:13:27,780 --> 00:13:28,620
Default line.
204
00:13:28,740 --> 00:13:32,940
So this line is optional and you don't actually have to add it.
205
00:13:32,970 --> 00:13:40,380
But still, you're very likely to use that because it is a special case which refers to what you should
206
00:13:40,380 --> 00:13:49,140
do if the variable was not matched with any of the other cases, the T. that war specified in this switch
207
00:13:49,140 --> 00:13:51,180
case structure.
208
00:13:51,240 --> 00:13:56,040
So to put it simple, these special default case is being executed.
209
00:13:56,520 --> 00:14:04,370
If there was no match between A, B, C or D, this example just executing the default, the default
210
00:14:04,380 --> 00:14:04,860
case.
211
00:14:05,010 --> 00:14:06,680
There was no match between these cases.
212
00:14:06,690 --> 00:14:09,090
We're going to execute the default case.
213
00:14:09,240 --> 00:14:11,430
So we know, for example, what we should.
214
00:14:11,430 --> 00:14:14,400
What do you think we should execute in the default section?
215
00:14:14,520 --> 00:14:22,380
We are probably going to execute some error message that will represent the fact that no case was matched.
216
00:14:22,470 --> 00:14:22,980
All right.
217
00:14:23,700 --> 00:14:31,680
So maybe by mistake, a user inserts, I don't know, a grade J, for example, then you should definitely
218
00:14:31,680 --> 00:14:33,360
print an error message.
219
00:14:33,480 --> 00:14:35,370
So it will look just like this.
220
00:14:35,400 --> 00:14:37,140
You just print F error.
221
00:14:37,170 --> 00:14:41,940
Try again to let the user know that he has done something wrong.
222
00:14:42,060 --> 00:14:42,570
Great.
223
00:14:42,720 --> 00:14:48,750
And the last thing to note here is that brake line, the brake line you can see in every case.
224
00:14:49,020 --> 00:14:51,660
It's very important to understand what does it mean?
225
00:14:52,200 --> 00:14:55,710
So this command is added at the end of every case.
226
00:14:56,160 --> 00:14:59,220
And all all it simply says is.
227
00:14:59,540 --> 00:15:07,620
That if you've executed a given case, there is no need to continue searching and executing other cases,
228
00:15:07,920 --> 00:15:11,310
just break from this switch statement and move on.
229
00:15:11,430 --> 00:15:18,510
So if, for example, we have a great day and great a was used, so we are getting inside of this case.
230
00:15:18,910 --> 00:15:23,340
You're simply going to execute all of its associated commands.
231
00:15:23,400 --> 00:15:23,940
All right.
232
00:15:24,390 --> 00:15:29,940
And then you're just going to break from this which statement and end up living it.
233
00:15:30,120 --> 00:15:30,510
All right.
234
00:15:30,520 --> 00:15:31,800
You are going to live it.
235
00:15:32,450 --> 00:15:34,110
And why is that so useful?
236
00:15:34,290 --> 00:15:37,930
Well, because if you don't use a break, for example, it case A..
237
00:15:38,100 --> 00:15:43,280
Then if you run this program, you will get inside case A. You may get inside Gacy.
238
00:15:43,680 --> 00:15:46,650
You will execute all of its associated commands.
239
00:15:46,860 --> 00:15:49,860
There may be more than just the print of command here.
240
00:15:50,160 --> 00:15:54,630
Maybe you are modifying grade or I don't know what you are doing here.
241
00:15:55,290 --> 00:15:57,810
And once you are done, you will not believe this.
242
00:15:57,810 --> 00:15:59,470
Which statement is hall?
243
00:15:59,490 --> 00:16:05,660
You will not leave it, but rather you will keep searching and executing maybe other cases, right?
244
00:16:06,120 --> 00:16:11,670
You will simply execute commands until you reach some brake line or you're finished with this which
245
00:16:11,670 --> 00:16:13,860
case where you are getting to the default.
246
00:16:14,010 --> 00:16:19,780
So basically you will also execute the big case if maybe the grade was modified in there.
247
00:16:19,860 --> 00:16:25,860
In this particular case, maybe grade was modified and there was no brake line.
248
00:16:25,980 --> 00:16:35,910
So you may be by mistake getting side of this case because now grade will be equal to B and all of that
249
00:16:35,910 --> 00:16:41,760
will happen just because you didn't use these brake lines to specify that if you got inside of a case,
250
00:16:41,880 --> 00:16:46,920
you as it gets its commands and then you get out of this which case right away.
251
00:16:47,130 --> 00:16:47,550
All right.
252
00:16:47,670 --> 00:16:52,320
So don't leave any places for mistakes in this kind of structure.
253
00:16:52,410 --> 00:16:59,880
So if you will run this program and I hope you are going to write this code on your own and try to execute
254
00:16:59,880 --> 00:17:03,410
it, if we are going to run it, we are going to get a message.
255
00:17:03,450 --> 00:17:05,560
Enter your grade A to F..
256
00:17:05,630 --> 00:17:08,790
We are going to insert A and we are going together.
257
00:17:08,790 --> 00:17:12,690
Result of grade is between 90 to a hundred.
24187
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.