Hello everyone, my name is (name) and we are continuing with this C programming course. (8 sec gap)
In the previous video we had discussed ‘decision making statements’ which means the part one of decision making, in that, we had discussed if and if else statements, which helps us in decision making in our program.
We saw that based on the condition our compiler decides that the statements under ‘if’ will be executed, they will run.
The statements inside the else part will run.
So, we had seen these things in the last video, in our decision-making part one video and this video is decision making part two.
And in this nested if, after that switch, switch nested, we are going to see all these things.
How we can use these switch statements one inside the other and increase our decision-making capability.
Apart from that, we will see one more statement, which is called the switch statement, how the switch statement is different from if and where it can be used.
All these things we are going to see in part two.
So, we will start without any delay and first of all we will see what is this nested if statements and how are they used? So, a nested if statement is simply an if statement embedded with another if statement.
Now what does this mean? We had earlier made one if statement.
In that we had written one statement.
If you consider the previous example, in that we had written a printf statement where we were saying ‘you are eligible for car driving’, in this way we had written one printf statement, what I will do is with that statement, which means first inside if we wrote one statement.
After that statement if we make one more if in it, which means if inside one more if, you will see how we will make it.
If we can complete the part of if inside if, inside if in our program then that we call as nested if.
Any statement which we are writing one inside the other.
We will call them nested and since they are if statements, that’s why we call them nested if statements.
We will quickly see its syntax; its syntax is in this way.
You can see that first we have written the if statement and in its round brackets we have written the first condition, okay.
We will also understand this with an example.
For now, you understand the syntax, we have written the first if and inside it we wrote the first condition, if the first condition is satisfied, which means if the first condition becomes true, what will our compiler do, the statements inside that if, it will come inside to execute it.
If the condition one is true, it is successful, our compiler has come inside if and it wants that the statements that are inside, they also should run.
So, the first statement would be somewhat like this, like the printf statements we wrote.
So, done with all those statements.
Now, what is happening, inside the if, we have put another if.
We can do this, in the one if statement we can write another if statement, it's very simple.
So, we have put the second if as well and we have put some other condition for it.
so we added another if, and applied a different condition.
So, the earlier condition was one, after satisfying it, the compiler came inside so it got the condition two.
Now, if condition two is also getting satisfied.
We will come into the second if and the statement inside that will get executed.
But if this doesn't happen, if for some reason the condition two doesn't get executed and it doesn’t get true successfully.
In that case the compiler will not go into the second if.
And it will directly come outside, even from the first if.
In this way the structure of if, where we are writing one inside the other, we call this a nested if statement.
We will understand it with an example.
This is an old example; we will just tweak it.
What was the example with us, if your age is more than 18 then you are eligible to drive a car okay.
If it is less, you are not eligible.
But if you are eligible and your age is more than 18 and you are eligible to drive still there is one more question that arises, do you have a driving licence.
Okay, you are eligible but do you have a driving licence so that you can show and state that yes, I've got the driving licence and I have passed the test.
And because of that only I can drive the car on the road.
If you even have the driver's licence then both your conditions will be satisfied, first condition of age and second condition of the driving licence.
If both of these conditions are satisfied, what does this mean, that you will be able to drive the car? But suppose your age is more than 18 but you don’t have the driving licence.
Which means you have not been issued a driving licence.
What will happen then, the first condition has become true but in the second condition you have not satisfied them.
So, you came out of it.
What will be the statement then? The statement will be that you are eligible but you don't have the driving licence that is the reason why you cannot drive the car.
So, you can print these kinds of statements.
So, these type of same examples we are making in our program as well and we will see how easily we can define this entire system and we will see how the program will work ahead.
So, we will directly go to our Visual Studio code and see how our programme works.
Our text editor screen is visible, which is our Visual Studio code, in which I have made one nested if dot c named file in which I have written the if code.
Now we will understand this code by each line, the first line is always of the preprocessor commands because few files are important to provide the input and output.
So, since we are making use of printf here, we will surely import the s-t-d-i-o.h file.
So, we have put the preprocessor command and we have imported the s-t-d-i-o.h file.
We will come on to the second line, the second line is the main one.
So, we know that with the main function our actual program starts, there is our main part of the code.
So, we have also come inside the main function, now comes the age part, which means what we did, we made one variable named ‘a’ and in that we are assigning one age, we are storing it.
For example, in this case we made ‘a’ variable named a and in that the age that we have put is 19.
This is for example, we have put 19 in the age, age of the person.
Now if you come down below there is one more variable, which you can see is L.
So, what is this l showing? L is basically showing that you have a driving licence or not.
I have basically converted it to true or false form.
In the way our value comes, that is one, so we say that yes, it is true.
If the value comes as 0, we say that the value is false.
So, in this way I have followed the system here and what I have done.
If L which means the driving licence.
So, if the value of the licence is one, in this case we will understand that you have the licence.
And if the L’s value becomes 0, we will say that you don’t have the licence.
So, in the way true or false used to work, in the same way 1 and 0 will work here.
If it is one there is a licence, if it is 0, there is no licence.
We will go ahead.
Now here the if statement is starting.
Now we will see what the first condition says, is your age more than 18.
In the previous program we had seen that here the19 example has been taken.
19 is more than 18, right? So, our compiler will come inside if.
And now it will start running the statements inside if.
Now the 1st statement comes as
What does this printf statement say? Congratulations, you are eligible to drive.
Your age is more than 18 that means you can drive, which means you're eligible to drive.
Until now it is not proved that you can drive, you are eligible to drive.
So, this is the first part, we have completed the first statement of if and then if we see below, there is one more if is made inside that if.
Here if we see, this was the first if and its condition was ‘a’ should be greater than 18, we came inside it and completed the printf statement.
If we come ahead, we can see that there is one more if statement which is telling the condition that the L’s value should be equal to equal to 1, which means L should be equal to 1.
So, we know that equal to equal is one relational operator, which tells whether both the sides are equal or not.
In the same way we will see that the value of L which is stored in it, is it equal to 1 or not? What does that mean? Equal to one means whether you've got a licence or not? If you have the licence, what will happen then, in this second if also, your compiler will come and it will also execute the statements inside it.
The statement inside it is saying that “print it, great! Now you can drive safely”, meaning on the first condition we got to know that we are eligible to drive.
After testing the second condition we got to know that we can drive the car safely because we have the licence.
The system is somewhat of this type.
If I do something like this, I will make the L’s value 0 instead of 1.
What will happen then?
I have made it 0 means I don’t have the licence.
So, we will see that inside the second if, our compiler will not go.
Because the condition will not be satisfied here.
It will say that the value that is there inside L that is not equal to 1, so I am not going inside.
So, it will not come inside the if.
Corresponding to if, means just next to if, the ‘else’ which is there, it will come in that.
So, we had seen how if and else works.
Where the if ends, just after that the else starts.
If the top if condition is not satisfied, then the compiler comes to the below ‘else’.
So, first if condition was satisfied, the second one was not satisfied because l is 0 then it will come to the second one’s else.
What is the second if’s else saying? It is saying “you are eligible, but you do not possess a licence.”
Means you are capable of driving but you don’t have the licence.
So, this kind of a sentence will be printed and if we assume that the first condition itself is not satisfied, if our age is not above 18.
Then what will we say? We can't say that we are eligible, then what will we say, the 1st if of the
corresponding else, we will come to that and what will our compiler print, oh
you are not eligible.
So, we have made this kind of a system, it is a simple program of nested if.
With which we can understand how we can use two ifs one inside the other in any condition where there is its requirement.
Now we will execute it and see.
We have saved it with Ctrl+S, we will right click and click on the run code.
As soon as we click on the run code, what happens is, let's see, this says congratulations, you are eligible to drive.
It says that you are eligible to drive, then it says, you're eligible, but you do not possess a licence.
What does this mean? Since the L’s value was zero, it is telling us that we don't have the licence, that is the reason why we cannot drive, we are eligible but we don't have the licence that's why we are not able to drive.
So, we will make some changes in the values.
I will make this one but this will be logically incorrect as our age is 17 but we do have a licence.
This is logically incorrect.
But we will try it here and see.
How will we try? We have put the age as 17.
We will check whether our first condition will be printed, in which it says that you are eligible.
So, we have made it 17, now we will run it and see.
This says, oh, you are not eligible.
So, it has got to know directly that you're 17, so you're not eligible.
So, there is no point in asking for a licence.
So, the system is somewhat in this way.
It is not logically correct.
So, its value should have been 0 itself.
As it is there won't be any difference in the statement because the compiler is not reaching there.
So, in this way there are few nested if programs are there.
Slowly and gradually as and when we will practice it, we will very easily get to know these, okay.
If we understood nested if, processed it and we have utilised it and seen.
Now, we will directly come on to the next statement which is called a switch statement.
What are switch statements? Switch case statements are a substitute for long if statements that compare a variable to several integer values.
Now what is it that they are talking about? This is a definition; we will understand it in layman's terms.
What is basically a switch statement? If you want that, for example, we will take a case.
What we said is that we have to make a grade system in which what we will do is if any student has Grade A, we will tell him, wow, congratulations, awesome.
If he has got a B grade, we will tell him, well done but you can score A also.
So, we have said this to B.
If he has got a C grade, we will say, you have to work hard.
If he gets a C grade, we will ask him to work hard.
And if D comes, we will tell him, sorry, you failed in the examination.
So, these kinds of comments we want to give based on their grades.
So, what we will do is, will we do something where we will use if statement continuously, first if will check if the grade’s value is ‘a ‘or not and then it will go inside it.
If it is not if it will again come in second if and check whether it is B, it will come in the 3rd if and check
whether it is C.
So, in this way we will make lots of ifs.
So, that will take a lot of time and the number of lines will also increase.
So, to eliminate this process switch case statements were invented.
This way we can execute very easily, we will understand its syntax once, so, it will be very easy for us.
So, what we will do, this ‘n’ that you are able to see, this ‘n’ is basically a variable.
Since we're talking about the grades here, what we'll do is we will store grades in the variable and then we will check it one by one.
So, here in the switch there are curly brackets.
This is the inner part of the switch.
In round brackets, basically we have to give that value, based on which we have to check which case will come here, there are different types like case one, case two, case three or case four, in the same way there are different types of cases in switch.
So, if the n’s value is one, like here case one is written.
If n’s value is one, in case one whatever statements we will write, that will be executed.
And if n’s value is two, so in case two whichever value is written, that will be executed.
But if n’s value is neither one nor two, then we will say that the value written in default or the statement written in it, will be executed.
Here you can see that we have written by commenting that “case one is executed when n is equal to one.
Case two is executed when n is equal to two”.
And when will the default case be executed? When n doesn't match any case.
So, these are the types of switch statements, from here we might not be able to understand much.
So, we will directly go to our Visual Studio code and we will take an example of switch case statements and we will quickly execute it and understand how it works.
So that we can understand this easily.
Our 1st line is our preprocessor commands.
This is known very well.
After that comes the main function, we also have to come in the main function, so, we came inside the main function.
Now our variable is getting declared, along with that it is getting defined, how? We have told its data type and we have even stored the value in it.
So, you will see that the value that we have stored in ‘i’, i is one variable, in that we have said to assign two.
The integer value two is assigned in i.
Now here the switch case statement starts getting executed from here.
Now we will see, like we use to write if, in the same way we will write switch here.
And where we used to give condition in if.
There we will keep any particular value.
Or we can keep the condition.
It depends on our customization.
Here for example, we have written the variable directly.
What does it mean we have written the variable? It will basically check the value stores in it.
So, we wrote i, which means what has come here, which means here came 2.
Since in the i there was 2, so 2 came here.
So, we will come in switch.
Inside the switch we will basically make different cases.
Whichever values we have to check.
Whatever is the value, 1, 2, 3 or 4.
In this way we will make the case.
So, here whichever value is provided.
In front of it the value that is written after the space.
This switch statement checks basically with that.
After going in i the value is two.
It will get that and check whether it is equal to one, it is not equal to one.
The statement inside it the print one will not be executed.
So, leaving it, it will come down and check the value in i which is two is it equal to this two? Yes, two and two are equal.
So, case two will be executed and inside that the print statement that is there which says case two, that will get executed.
What will it do? Slowly it will start checking each case and if any case is not successfully checked value that we have given in default, basically that value we will bring here.
Doing this the switch keeps on checking.
We will quickly check, two is checked here.
So, we don't have to take 3, 4 and default.
The print statement will be executed.
Now we will see what is this break that is written about.
This break means whenever you successfully come inside in any case, you want it to test the rest of the values whether they are equal or not? No, we want that once the value is checked by someone and it turns out to be true, we don’t want it to check the other values as well.
If we get equal in case two, with the help of this brake line, this break statement, we want to tell the compiler that this case two has successfully been completed and has been checked.
So, from here we should break.
Which means from this particular switch statement you have to come out.
So, this is the work of the break, wherever the compiler has reached, successfully has come after checking the true value.
After that whenever he finds a break, with the break it will come directly out of the switch, which means it will come outside of the curling brackets here.
After coming here, the return 0 statement is there, as we have written the main function.
So, in this way our switch case statement works, we will right click it and run code and see what gets printed.
You can see that here it is printed case two.
And why is it printed? Even that we know because the value inside i was equal to two, so the statement under case two, the printf one, that got executed.
And we saw on the screen case two.
If in printf we would have written any statement that would have got executed.
In the same way we can use the switch case statement when we don't have to write a lot of if’s one below the other.
In this way we can use the switch very easily.
And we can understand the grades example here.
What you have to do here is write a grade.
I will quickly change it.
And instead of grade here comes Grade A, we will make it capital.
What will happen here? I will say A.
For now, we will make one change only and we will be right here Congratulations! right? Your grade is awesome.
We have written this statement and we have said here that the char value that we have defined, the grade that we have defined.
We have to check it by going in each case what it is matching with.
And the statements inside it will be executed.
So, I have done Ctrl+S and saved it.
Now I would click on the run code.
So, you can see that here I have got congratulations! Your grade is awesome.
In this way we'll define the other cases as well, a, b, c, d, whichever are the grades similarly.
And in this way, we can add comments as well.
So, this was our switch case statement.
When we define these switch statements one side the other, we call them nested switch statements, like we have discussed before the statements which are made one side the other, we call those nested.
So, if we are making a switch inside the a switch then we will call them a nested switch.
And if you see it's syntax once, we have made the switch statement, inside that we have put a variable to check the value and in its first case we have printed this statement and we have put one more switch inside it.
It is a little complex but as per our requirement, after practising slowly we'll find it very easy.
And we would think that if it was not present then how we would have completed the programmes.
So, this was our entire decision-making statement.
We have seen if, if else, nested if, switch and nested switch statements.
If you think that there is any part of this video
or this topic, you might have not understood then
you can ask us any query or
question related to that topic without any inhibition.
If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.
In the next video, the topic name would be loops in C programming.
We will know What loops are basically, how we can use them and run one statement again and again.
All these things we will see in the coming video.
Till that time, thank you so much and keep watching.
Share a personalized message with your friends.