My name is Aaditya and we are continuing with this C programming course in which in the last video we had discussed operators used in C programming, which means these operators are of how many types, how are they written, what is the meaning of their operations and by using them what all things can we make.
We had discussed all these things in our last video.
Now the time comes of decision making, which means we know that in real life we take a lot of decisions and our entire day goes in making decisions.
If we're waking up in the morning, what time should we wake up, should we wake up at seven, six or ten o’clock.
This is our decision based on the work we are doing, in the same way the work that we have to do after getting up in the morning, depends if you want to go for a walk in the morning, in that case you have to wake up at six o'clock.
If you wake up at 11 o'clock and go for a walk, that is not good for your body.
In this way you make different decisions in real life, in the same way we make decisions in programming, we make different decisions based on some conditions, which means based on few values and conditions we take the decision and there are different ways of taking the decision, which we will be seeing in this video.
So, how the decision is taken, what is the meaning of this decision making, all these things we are trying to understand in this video.
So, we will go ahead and see what exactly is decision making.
If you talk about the real-life situations, if you see any condition, the decisions that we have to take on it at that time we ask ourselves if questions, like if I'm going on a road and I'm seeing two roads in front of me, my destination is fixed but there are two road options in front of me.
So, what I will do is, I'll put a condition that if I have to reach my destination early, I will have to choose a way which will be efficient for me and which will help me reach my destination early.
So, to choose such a way our condition was that I have to reach my destination early.
So based on that condition, I asked myself the question: if I go from the left side, will I reach my destination fast? or if I go from the right side, will I reach early?
So, the if and else question that I'm asked myself, in the same way in our programming, the compiler asks these questions and we write this kind of program, so that that compiler can understand based on some conditions, whether you have to choose this way or that way, in this way the decision-making works, if we take an example.
We can see that our driving age, which means after which age, we can start driving is 18.
If you're a student and your age is less than 18, will you be able to drive? Legally you will not be able to.
So, in the same way we can make a decision-making program in which we will be asking your age and if your age is less than 18, we can say that you cannot drive a vehicle for now and if your age is above18, we will say that yes, you can now drive the vehicle.
In the same these decision-making things, if we can execute our program, we will understand its flow once.
Let's understand that this black dot is our starting point.
And as and when we come down, we can see this part.
“A” is basically a condition, how this condition happened in our last example, the condition was that your age should be more than 18 if you have to drive.
This “A” is basically telling us whether our age is more than 18.
If it is more than 18, that thing is true, we will come on the B way, our decision is changed based on the condition that is it greater than 18 or not.
If it is greater than 18, then the statement will become true and we will come on the B part but if your age is less than 18, then what will happen is that the statement or condition will become false and then you will have to come on the other way, on the false one.
So, if it is true if your age is more than 18, you will come on the B way, if it is less than 18 you will go on the C way.
So, in this way, your decision varies B means you are driving and C means you cannot drive now.
So, in this way we can make the decisions with the help of different statements by using them in our program.
Which means, by using different statements in our program, we can make these kinds of decisions.
Going ahead in the next slide we'll see how we can make these different types of decisions.
So, we will see which are the different types of decision making statements, which we would be seeing in our program.
Here there are few statements given like “if”.
What is this “if”? Like we had discussed in our previous example, there are two ways in front of us.
So, we asked ourselves a question.
If I go from this side, will I be able to reach our destination fast or else if I go from this side, if not here, if I go from there, will I reach fast?
So, these are questions that we are asking ourselves.
The same type of questions we write in our program as well, that is the reason we use “if” statement, apart from that the if and but situation, in which if not this then, (repeat) this statement which was there, if else, we call it if else in the program.
If this, if this is not true, or else go for this.
So, this if else part we see in if else.
Now, this nested if.
What is this nested if? One inside the other, one inside the other, different conditions are there.
Like you said if the age is 18 plus, you can drive, then that statement is true.
And you came inside it and there was another question asked to you.
Do you have a driving licence? So, this becomes the next question.
If you don't have the driving licence and you are over 18, will you be able to drive the vehicle? No, you will not be able to.
So, in the same way, if we make a different if, inside the if, inside another if, we call it nested if.
Then there is a switch condition or switch statement.
What is a switch statement? There are values given for one statement and whichever value that will come in the output, based on that part of our code would be there which our compiler will execute.
All these things we will understand in the coming slides.
For not in this part, we will pay attention on if and if else, which means on these two statements and in the next part means in part two we will see nested if switch and nested switch as well.
Now we will start with our if statement, we will go ahead and see what this if statement is.
“If statement is the basic decision-making statement”.
Which means, this is the most basic part in which we can make the decision and it is used to decide whether a certain statement or block of statements will be executed or not.
We know that we keep writing lines in our program if you have to do something like this.
In the middle of the program if you write two or three lines.
You want to run it only when your value is more than five; it means that A is your variable and there is a value inside it.
If it is more than five then you have to execute or compile two lines of your program or you have to skip those two lines if you have to perform these things through your programme that time, we can use this if statement appropriately and we will see practically how we are able to use it.
We will understand its syntax once.
Which means the way of writing it. How we will tell that condition and how to tell that this is if statement and what are the different parts inside it, this will be told by the syntax here.
We will first of all right if, I F.
So, the compiler will understand that here, “if” statement is talked about.
Then we used the round bracket, you can see them here.
What are the round brackets saying? Inside the round brackets are conditions. Like I told you if A’s value is more than five, then you have to execute or run some part, only two or three lines.
So, this condition that is A greater than five, this condition will come in this round bracket and after that with these curly brackets the body inside our if statement will come, body means there are few statements which you decided, if the if condition is true only then you have to run the statement.
So, where do you have to write those statements? you will have to write here in the curly brackets.
So, what will happen here will be understood quickly.
Our condition is greater than five okay.
So, this is the condition if it happens to be true, our compiler will come inside this round bracket and all the statements that would be written inside it for example, statement one is written over here.
So, this statement one will be run by our compiler.
So, after running the bracket gets over you can see here then coming out of the bracket the statement outside the bracket, it will start executing, compiling or running it.
But, what if this condition doesn't come true? If it is not true then the compiler will not concentrate on what is inside the bracket, it will ignore it and straight away statement X will come where we are seeing it.
So, somewhat in this way our if statement will be run.
We will see it practically as well before that we will understand one more thing which is our if else statement, it is very similar to if statement the only thing is one more option or part gets added to it.
Like we had made only one “if” part.
In the same way there is also an “else” part that is made in this.
If this is true, the one inside the “if” gets executed.
But what if it is not true? The “else” part, like we say “or else” in English.
In that else part we will write a few things, if the if condition does not get true should the things run then else part of the things should run.
You might not understand it superficially.
As soon as we implement it practically, you will understand it quite easily.
So, “if else statement allows selecting any one of the two available options depending upon the output of the test condition”, which means it will depend on the condition whether if condition will be applied or the else one will be applied.
We will see its syntax as well.
We had seen “if” that in “If” there would be a condition and the statements in the round bracket that we have to run with if that would be there but if the condition is not true, then it will come in the else part and the statement written in else curly brackets, it will execute it.
So, in this way our if and else statements are working.
Now we will straight away go to our Visual Studio Code and see how the if and if else statements work.
We'll go on to our Visual Studio Code.
We have come here.
I have already made a “if and else” named C file and I have written a program in it.
What is the program that we have written? First of all is our preprocessor command, hash include one.
In which there is an S T D I O dot H file which we are importing.
So, we have understood this before.
After that, there’s our main function.
Even in the main function we know that after this, we have started to write our program.
After that we have seen that we have made two variables.
A is equal to 5 and B is equal to 4.
Which means in A there will be 5 and in B there will be 4.
Now you will see here in If you wrote a condition, is A greater than B or not.
We had defined earlier that A is 5 and B is 4.
What does this condition say that 5 is greater than 4.
Is A greater than 4? So, yes, five is greater than four.
So, any statement inside the curly bracket of if statement, compiler will run it.
If it was written something like this, that A less than B, in this case, the compiler would have never come to this condition, it would have directly come to the “else” part and whatever is written in the “else” part.
And whatever is written in the “else” part that our compiler would have understood and converted it and make the computer understand it.
So, in this way our “if and else” works.
So, here for now I will keep this sign, so we can understand how this will work.
So, I have saved it.
Now, you see if A is greater than B becomes true.
So, what will get printed with the help of printf function? A is greater than B.
This will get printed.
If this doesn't get printed, which means A is not greater than B.
If this condition comes.
It will come in the else part and say A is less than B.
So, in this way we have written the program, so that we get to know whether A is actually greater than B or not.
So, what we will do is save with Ctrl+S and we will right click and click on the run code.
And we will see what is the output that comes.
So, we will wait two seconds for the output and see in which way our output comes.
Yes, this is getting executed now and now our output has come.
What is its saying in the output? A is greater than B, which means that this statement was there, if you pay attention here.
The A is greater than the B, print F statement that has got printed but this has not got printed, A is less than B.
Why did it not get printed? Because if the “if” condition gets true.
Either the inside things of “if” would be executed.
But if the condition is not true, the statements inside else would be executed.
So, in the same way if I reduce A’s value compared to B.
If I make A’s value as 3.
Now A is smaller than B.
So, this condition will not be true.
Now we will see what will be printed.
Now I will once again press Ctrl+S and save it and I will right click and click on the run code.
Now see that our statement has come A is less than B.
So now since we have made A’s value as 3.
So, whenever we are putting this relation or condition, this becomes false as A is not greater than B.
So, it will not come in this, the statement that we have written inside the bracket.
This will not get executed as the compiler will not reach here.
Reading this condition, he will straight away go outside and he will come in the “else” part.
And in “else”, the statement that we have written is less than B, that part will get executed.
In this way we can use “if and else”.
If there are different conditions if I see them here.
If A is our age and if I say here A is greater than 18.
I will make it 19.
So, I have made A as a variable which means the age.
A is defining the age.
And what I will write here, if A gets bigger than 18, (typing) “you can drive car”.
So, this is the statement I want to get printed.
But if the age is not greater than 18.
So, what will I get printed, “you cannot drive a car”.
So, this is my statement.
So, you can see that the age is 19 for example.
Is 19 greater than 18? Yes, it is greater.
So, what will happen? The print F statement should run and the “else” statement should not run.
So, we will execute it once.
We will run and see what is the statement that gets printed.
So, you can see that since the age was greater than 18, so it got printed here, “you can drive car”.
So, in this way we can make different combinations of “if and else”, we can test different conditions and make the decisions on the basis of that to our compiler.
In this video we discussed if an if else statement and why decision making is important, how is it done? All these things we have seen in this video.
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, we'll be seeing the switch statements, nested switch and even nested if as well.
Till that time, thank you so much.
better than i thought
learnvern provide great content for learner
It is very good and understanding course. I like it very much and it is abousuletly free course .