Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Icelandic
Igbo
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
At this point, you should already be well familiar with if statements.
And what are the main principles of control flow?
And in this video, we are going to review the code from one of our previous examples and build our
way up in the most detailed explanation and learn how and when we could and actually should use switch
cases in C programming language.
So in this example, we've tested out some conditions and for each of these options, we've printed
out a corresponding message.
And if you take a closer look, you will see that we checked whether a condition is true or false for
a range of numbers.
For example, if we take the first condition, which is if grade is greater or equals to 80, that may
be true for all numbers, which are basically 80 or higher.
Right.
So it can be true for a variety of options, for a range of numbers.
So it can be true for 80, 81, 82, 83, up to 95, 96 and so on.
All numbers that are greater than 80.
This condition will result in a true right.
It will be true.
And then we also had the ALS if statement that checks whether or grade equals or a higher event, 60,
but it less than 80.
And similarly, this condition will be true, will be true for all number is between 60 to 80.
Right.
60, 61, 62 and so on, up to 80.
So once again, this means that these different conditions may be true for a range of numbers.
And of course, we can take a look at the last at the last condition when we use the LS the EL statement,
when the grade is less probably is less than 60.
Then also here, you may have a lot of other options that this condition will be satisfied like 59,
58, 57 and so on.
Right.
Everything that is below 60.
But the thing, guys, is that these range of conditions.
So that's not always going to be the case.
There are times when the conditions may be true just for a single value, not for a range of values.
And I want to give you an additional example that hopefully will be much more interesting to you before
we move on.
All right.
So let's talk about a grading system that works with the letters A, B, C, D, and athletes C.
And this grading system is pretty simple.
Some of you may be familiar with it, but for those who are not.
I will briefly explain it to you guys.
So the grades here are represented as letters.
And there is no precise grade like 96 or 97 or 72.
Like we've seen previously in our previous examples.
But rather, each letter in this rating system represents a range of possible grades of possible numbers
in the numerical rating system that we've seen in previous examples.
So a grade A represents the range of grades of numbers of grades from ninety to a hundred.
Grade B represents the range of grades from 80 to 89.
The results of grade C that we presents on the range of No.
The range of grades between 70 to 79.
There is also D.
And lastly, lastly, we have the F grade, which represents that you've failed on your exam.
And what we're going to do now is to write some converting program which will simply get a letter from
the user and then print out the range of associated grades from the other grading system, just like
we can see here on the screen on the left.
We can see that we will get a letter either, which will be A, B, C, D or F, and then we will print
a corresponding message with the range of numbers.
The range of values associated with that given letter.
So, for example, if we studied well for our exam and received an A..
Then we should print out the message.
Your grade is between 92 to a hundred.
Or if we got a C on the exam, then we should print something like that.
Your grade is between 70 to 79 simply based on this formula.
Is that you can see here on the left, you are going to print an associated message.
Is that clear?
What we have to do right now.
And before we move on, I want to stump you guys, Jim, just for a moment.
All right.
And let you think about how cool is that that you can actually write your own program that does something.
You wanted it to do it right.
You can think about an idea like the one we can see on the screen.
And then you think about the idea and you can implement it by writing, by developing a program that
gets a grade from one grading system and converts that grade to another grading system.
Think about how much progress have you made so far?
And I want to let you know that you should be proud of yourself.
And now with that motivation being said, let's come back to our program, shall we?
So here we can see basically the template for our code.
I've written it in C and just embedded here in this presentation.
We can see that, first of all, we create a variable called grade and it's of type CHA, right?
We're using a character.
Then we read the value of this character and then we are going just with the if else statement.
So.
All right.
So we've talked about a case where we've received a we've received a letter A..
And what should we present if that's the case?
So if great equals to the letter A..
We should bring that grade is between ninety to a hundred.
Now we we've received a letter B.
We should go through these condition and print that a grade is between 80 to 89.
Also for a C, for a D.
And for now I'm not putting how.
Here in this example, I'm not putting the letter F.
I'm just going with the LS condition that brings out that there was an error and you didn't specify
A, B, C or a D.
Right.
We did not include here the F option, although it's mentioned here in the print F command.
So we we basically can handle four cases and A, B, C and D.
All right.
Otherwise we are going to print an error message to the screen.
So this program, this whole structure of it, if it will work, but we can say that it starts getting
a little bit messy, don't you think?
All of these Eve else, if l.c if and then there else if at some point.
All right.
You would like to add support also for grade F, which is currently not here.
Right.
You will need to find the exact place in code where you should added.
Maybe you should edit here or here.
Or maybe it doesn't matter.
Right.
But you have to think about it.
Where you should add the condition to check if the grade equals to an F.
So although this code works, as we just mentioned.
Right.
If you tried to run it on your own, then you will see that it works.
There is a better elegant approach for solving such problems.
And it should be done using this.
Which cases.
So switch case source, which statement is also a control mechanism which allows us to compare a given
variable with multiple cases.
And I'll just I'll I'll show you this right now.
But I just want you to take another look at this example and understand that it's getting kind of messy
when we make comparisons between a given variable to a is a certain value.
Right.
Not to a range of values, as we've seen previously, but to a certain value.
There is a better way to handle it by using switch cases mechanism.
So switch cases, a control mechanism that allows us to compare a given variable with multiple cases.
And for a case that the mechanism finds a match, meaning the condition is true, then it allows associating
a block of commands to be executed for that particular case.
But this values and remember, this one is important.
These values should always be discreet.
Right.
Not a range of numbers, but a single unique value, something discrete.
And now I want us to take a look at how the template, how the structure of this which statement looks
like in C programming language.
And then explain it in more details.
So first of all, we just create the variable grade as previously.
Then we write the word switch.
All right.
We'll write the words switch enough.
The words we provide in the parentheses, the variable.
You would like to check the variable itself.
Great.
In this case.
And we know that grade should hold only discrete values.
Right.
Either way, it's A, B, C or D.
We are not talking here about testing any ranges for a grade.
We simply want to ask if it equals to one of these given character as A, B, C already.
That's it.
So the syntax goes like this.
We first of all specify the curly brackets.
The block of commands that are associated with the switch case are mechanism in C.
And now we will specify all the cases, all the cases of that grade variable may be.
And they associate it commands that should be executed for each of these case.
So the first case refers to one, the grade equals to a..
So we just use case.
And then we specify the value a character.
A, it's almost the same as you would have written if rade equals equals to a letter A..
And you can see that right after the case.
We use here a column.
And it simply lets you associate a bunch of commands related to that particular case.
Meaning if that case seems to be seems to be happening to be true.
All right.
If that case happens to be true, then we will execute the associated commands.
So we will print on the screen the grade.
The degrade is between nineteen ninety to a hundred.
So here you go.
You associate a bunch of commands with a given case, just like we did previously with the IEF else,
just by using cases.
So for now, maybe these break line, these break and the semicolon at the end, maybe it's bothering
you a little bit, but just leave it aside for now.
We'll come back to it later on.
So now we can add another case, right.
Because we are over we are done with the case where the grade equals to a we can move on to another
case and specify the second case, which is case B, and now we can specify the associated commands
with this particular case, its goal.
It goes like this print f print F grade is between eighty to eighty nine.
And also it goes the same for a case C and case D, pretty much the same.
You just specifies the case and the value for the variable grade that you are running this which case
on.
And you just specify the associated commands for each given case.
So once again, you first of all specify the word case, then the value of that case.
And if this which variable value is the same as specified in the case A, B, C or the in this example,
you should execute the associated block of commands for this particular case.
So I hope that makes sense to you guys.
Nothing complicated here.
And this simply allows us to organize the code in a little bit more structured way.
And it also gives us flexibility with modifying and even adding new cases as your program being continuously
developed.
All right.
These are some terms that you will learn in the future.
But it's important for me to tell you these terms of continuous development and how you modify your
code.
It should always be as easy as possible and as long as understandable as you can make this program.
So, for example, if you would like to add support for F right for F grade, then you should just add
an associated case for that without changing anything in the structure of the code just like that.
Simply adding additional case and its associated commands.
All right.
We're good.
We're good.
We're moving forward.
And so we're almost done.
Just last thing we should talk about in this.
Which statement is the default line?
Default line.
So this line is optional and you don't actually have to add it.
But still, you're very likely to use that because it is a special case which refers to what you should
do if the variable was not matched with any of the other cases, the T. that war specified in this switch
case structure.
So to put it simple, these special default case is being executed.
If there was no match between A, B, C or D, this example just executing the default, the default
case.
There was no match between these cases.
We're going to execute the default case.
So we know, for example, what we should.
What do you think we should execute in the default section?
We are probably going to execute some error message that will represent the fact that no case was matched.
All right.
So maybe by mistake, a user inserts, I don't know, a grade J, for example, then you should definitely
print an error message.
So it will look just like this.
You just print F error.
Try again to let the user know that he has done something wrong.
Great.
And the last thing to note here is that brake line, the brake line you can see in every case.
It's very important to understand what does it mean?
So this command is added at the end of every case.
And all all it simply says is.
That if you've executed a given case, there is no need to continue searching and executing other cases,
just break from this switch statement and move on.
So if, for example, we have a great day and great a was used, so we are getting inside of this case.
You're simply going to execute all of its associated commands.
All right.
And then you're just going to break from this which statement and end up living it.
All right.
You are going to live it.
And why is that so useful?
Well, because if you don't use a break, for example, it case A..
Then if you run this program, you will get inside case A. You may get inside Gacy.
You will execute all of its associated commands.
There may be more than just the print of command here.
Maybe you are modifying grade or I don't know what you are doing here.
And once you are done, you will not believe this.
Which statement is hall?
You will not leave it, but rather you will keep searching and executing maybe other cases, right?
You will simply execute commands until you reach some brake line or you're finished with this which
case where you are getting to the default.
So basically you will also execute the big case if maybe the grade was modified in there.
In this particular case, maybe grade was modified and there was no brake line.
So you may be by mistake getting side of this case because now grade will be equal to B and all of that
will happen just because you didn't use these brake lines to specify that if you got inside of a case,
you as it gets its commands and then you get out of this which case right away.
All right.
So don't leave any places for mistakes in this kind of structure.
So if you will run this program and I hope you are going to write this code on your own and try to execute
it, if we are going to run it, we are going to get a message.
Enter your grade A to F..
We are going to insert A and we are going together.
Result of grade is between 90 to a hundred.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.