Hello, hello everyone. How are you all? In today's lecture, we are going to learn C++ conditionals.
Let's start with an example to understand where we want to go. So, in our real life, sometimes it happens that our actions are dependent on a situation or a condition. Like if it is raining outside or it is raining, then I will carry an umbrella with me.
Remember now? No need to do that. Similarly, if my brother is making me tea, then I will give him a gift on Rakshabandhan. Otherwise, I don't know.
Similarly, if we have to do any operation in the code, which depends on some other condition, then we use conditionals there. So, in C++, what do we use? We use if statement.
And you see its syntax here. It is written. If some condition, then the condition which will be true, then the code written in curly brackets will be executed.
So if I write from the example of rainy, if rainy weather, if this condition is true, only then Whatever I write in curly brackets, this will get executed. If it is false, if the condition written here is not true, then it will not execute the code written in the if statement. Similarly, if I want to write that if your marks, I am taking input from the user, marks, If the marks are more than 33, then I have to print pass. Okay. So how can I do this?
A variable is marks. Okay. Which we are taking input from the user. Now I can write if mark is more than 33, then shout.
Pass. Right. So this is how I can use if statements.
Now I have told you that if the condition is true, then whatever is written in the if statement, the code will be executed. But I want that if that statement is false, that condition is false, then I want to do something else. I want to do some other operation if the statement is false. Okay.
So for that I use else. And along with this, it is called if else statement. So in the previous example, if I am checking, if marks are more than 33, then print pass.
If marks are less than 33, then I want to print fail. So for that, I will use else statement. Else.
print fail now here are some things to be noted first of all you guys understand that if the condition I have written in if is true then it goes into the if statement if anything else happens other than that condition then it will go into my else block ok so according to this condition whether it is true or false according to that if statement, if block statement or else block statement executes. One more thing to pay attention here, you will see this cout, I have not written it in the same line, the whole code is not left aligned, so in C++, you have to pay attention to indentation a lot. What is indentation? Here I am giving this space, I call it indentation.
So, So, whatever statements will come in these curly brackets will come after the tab, after some indentation. Okay, so you will understand this in code too. What happens in code is that when we write in code, we enter in the next line, then our code editor is very smart and automatically gives us some space, it automatically gives us indentation.
Right? But if we... you know pen and paper or you are doing some other code then you have to pay attention to indentation sometimes due to indentation errors come in the code ok so now we have read if else now let's write a code for it here the question is you have to print odd if the input value given by user is odd otherwise you have to print even ok so in the lecture of operators I told you that we have to print a number How can we judge odd and even? If I have a number and I make it modulo 2, then either I will get 0 or 1. If I get 0, then I can say that it is even.
If I divide any number by 2 and get 0, then it is even. Otherwise, it is odd. We will use this with if-else.
Let's see the code of this. First of all, we make a variable int num and take this input from the user. After that, we will use if statement if num modulo 2 equal to equal to 0. We had read equal to equal to, it is a conditional operator. So this statement will be either true or false. If it is true then we will print even or let's print odd.
Okay. Let's run this and let's give some input 5 and it tells us odd. Okay.
Let's run it again. Let's give an even number and it tells us even. Okay.
So our code is working properly. Right. Let's move ahead.
Now let's come to if. and else if statement. Suppose, we have seen that we have considered the condition either true or false.
If it is true, then if statement will go. If it is false, then else statement will go. Now, suppose I have three cases.
Suppose, I am getting marks in input and I have to tell if it is more than 80, then its grade is A. If it is more than 30, then it is D. Otherwise, Now I have 3 cases. Now here I can use else if statement. How?
What we will do is, we will give one statement in if, one statement, means any other statement, we will give in else if and if the above statements are not true, then it will go to the else block in the last. Ok. And it is not necessary that I can use only once else if, I can use multiple times else if.
So if I am getting marks of a user, I am saying if marks more than 80, then I am printing its grade A. Else if marks more than 80. 60, so I can print something else like B grade, else if marks more than 40, okay, so I can print C grade and if nothing is true then it will go to else statement where I can print F, okay. So here I have not put curly brackets but in code we would prefer to put curly brackets For understanding you can understand that how many else if statements we can put here If we want to check multiple conditions And if none of those conditions are met then in the end our else condition will be taken Now let's see one more example here We have to write a program to divide people into three age groups according to the input. So we are taking input from the user, their age and according to the age, we are dividing them into three groups.
If they are below 12, then child, between 12 and 18, then teenager and if they are more than 18, then adult. So I can use the else if statement here. If age is less than 12, Then I can say, I can print child else if. My next condition, I can do more than 18. If it is more than 18, then I will print adult else.
The third case is same, if it is not less than 12, not more than 18, means between 12 and 18, then we will just print teenager. Ok, let's run this. Teach group and let's give input 6 and it says child.
Let's give input 15 and it says teenager. Ok, so here you can try to have even more categories. Try to add another category in it.
If you are over 60, then you can say senior. Senior citizen or something. So try this on your own. We will just add one more else if statement in it. Let's move ahead.
Nested if else statement. What does nested mean here? That we can use more if else statements in our if else statement. Suppose I am getting marks from a user and I am saying that if they are more than 33, then I know that it is a fail. If they are more than 33, then I have to check if the marks are more than 80, then it is gracefully pass.
between 33 and 80, then just pass. Okay. So, like this, we can also apply multiple conditions.
So, here, this condition, where we are seeing something more than 80 or less than 80, we will see this only when our marks are more than 33. So, how will its code be here? I am saying that if marks more than 33 hai, toh what I will do is I will again check ki marks 80 se zyada hai ya nahi. Thik hai, agar 80 se zyada hai toh I will print gracefully, agar 80 se kam hai toh I will just print pass. Thik hai and agar 33 se kam hai toh we are just printing, yahaan pe kali brackets khatan.
We are just printing. So, here you can use if else statement and if else statement in the same way. So, let's move ahead. Now we will be talking about some logical operators. Like logical AND operator and logical OR operator.
We have seen this a little bit before. What happens in logical AND operator? If we have multiple conditions. which we have to check.
We have to check their accumulative result. So, we use logical and illogical or operator. How?
For example, I have an expression 1 and expression 2. And I want that when both these statements are true, then only I am doing something. If any of them is false, then I don't want to do anything. So I can use and here.
Logical and what will it do? If one of the two is false, then it will return false. Similarly, what happens in logical or?
If I write expression 1 or expression 2. So here if one of the two expressions is true, then it will give me true. Otherwise it will give me false. So here logical AND and logical OR operators are used in IF-ELSE many times. Because sometimes we don't have to check only one condition, we have to check multiple conditions together. That their cumulative result is true or false.
So for that we use AND and OR operators here. One more operator which we talked about in the lecture of operators was ternary operator. Turnery operator is a short version of if else. In if else, we have to use multiple lines.
First if, then condition, then we write the code of curly brackets, then else and then the code of else in curly brackets. In turnery operator, we can write that thing in one line. If I want to use the example of marks, pass or fail in turnery operator, then how will I do that and how in if else?
Let's see in the code. Suppose I have made a variable marks which I am taking input from the user. Now if I want to use if else statement then I will write here if marks more than 33 then you print pass else you print fail.
Here if I want to use terminary operator then my code would be in one line. How? I would compare directly marks more than 33 question mark. If this statement is true then I want my expression to be print pass or fail.
So here if my statement is true then it will go and execute this. If it is false then it will go and execute this. So let's print this once. Both the methods will print the answer. Let's run this.
And we will give input 45. Both the cases are getting the same results. But you can see here, I have written so many lines in if-else, I have written them in one line. Okay. So if you want to execute, If you want to execute in if condition or else condition that is one line or you want to do some short operation basis of some condition then you can use ternary operator and that is preferred as your code looks small Now we will discuss some misconceptions some confusions in some operators which happens while using our conditionals These both operators are AND and these both operators are OR. So, what is the difference between them?
We had read that this is a bitwise operator and this is also bitwise OR. These are logical AND and logical OR. Right? So, the difference is that bitwise operators work on bits.
And logical operators are used to get a cumulative result of multiple conditions. Suppose I have put a condition in the code. If false bitwise and 5 divided by 0 is equal to 1. Now here, It will matter that which and I have used, bitwise or logically.
If I have a bitwise operator, it will evaluate both the expressions. And it will give me an error here because I am dividing 5 by 0, so it will throw a runtime error. If I was using logical and here, it will not give me an error.
Why? Because in logical and, before that, only first condition is checked. If that is false, what happens in the logical AND?
If any condition is false, then your cumulative result is false. So if the first condition is false, then it will not check further. This basically code will not throw me an error if I am using logical AND here.
Similarly, if I am using OR here, then So, again it will throw an error in OR. And suppose I have TRUE here. If I am using OR bitwise, it will throw an error because in bitwise OR, the expressions of both sides will be evaluated.
But if I am using LOGICAL OR here, then it will check the first expression or condition which is already TRUE. And what happens in LOGICAL OR is that if one is also TRUE, So the result is true. So it will not check further.
And this will not throw me an error. Okay. So bitwise and or and logical and or. The difference is this.
And wherever you are using the code. Or you are seeing somewhere. So keep this thing in mind.
That it is a bitwise operator. Or it is a logical operator. Because your result can vary from that. Okay. Now.
It is enough. Let's come back to the code. Let's see an example of AND operator. We have to write a program to print the value if it is even and divisible by 3. So I will give an int num to the user. I will take that input.
Now what I have to check is that if it is even and it is divisible by 3 only then I have to print the number. So sorry here we have two statements and both of them should be true so for that we can use logical AND operator. First of all we will check if num is even.
For num, modulo 2 should be equal to 0. We have seen this first, logical and operator and the second condition is that it should be divisible by 3. So, for divisible by 3, its modulo 3 should also be 0. So, if this statement is true, then we will print our number which we have taken input from the user. Otherwise, we will not be doing anything. So let's run this and I'll give an input 6. So I have 6 printed here because it is doing both the conditions. Because it is doing both the conditions true.
If I do 8 here, then nothing will print here because 8 is divisible by 2. It is an even number but it is not divisible by 3. Okay. So. If I had used logical or operator here, then I would have got true here. But if it is and, then both the conditions should be true. Let's see one more question.
Write a program to print the value if it is divisible by 3 or divisible by 5. So here we will print it if any one condition is being true. So we will use logical or operator here. if num is divisible by 3 or num is divisible by 5 in that case we print num ok let's run this so let me give an input where 3 is divisible 9 is divisible by 3 but not by 5 but it's still getting printed because we have used the or operator here ok Now let's move ahead.
Next topic which we will discuss is switch case. Now we have read if else statements, nested if else, and else ifs. If I tell you that I have to write a code where I have to decide my clothes according to my day of the week.
So what will happen that it will compare the day with Monday. So he will be doing something else. He will compare with Tuesday, he will do something else.
And similarly, he will be comparing with all 7 days. So our 7 conditions will come. Right? So here we have to write the if-else statements.
Else if statements will have to be written 7 times or 6 times. And our last if-else statement will be done. Right? Now to simplify this, Here because we have so many cases.
To simplify it, we use switch case. What happens in switch case is that whenever we have to compare one value against multiple values, then we use switch case. We can also use else's statements. We can compare that variable with values from different days of the week.
Or we can use switch case. How to use switch case? Let's see its syntax so you will understand. In switch case, we compare our expression with any variable or condition. If there is any condition, then only two cases are made, true or false.
If I am comparing an expression, then there can be multiple cases. For example, if I am comparing day of the week, then I can have seven cases, one for each day. So, I define the expression here, that against which variable I have to compare my cases.
And then I have a case for every value that I want to compare my expression to. So, here if the case is Monday, where I have written the code according to Monday, then case will be Tuesday, where some code will be written according to Tuesday and so on. Now, in the end, notice one thing, Default.
default case is executed when no case is being met. So that's when we execute the default case. So let's see an example of this.
Here if we have to write a program to print the day based on the day number. So our day number will be 127 and according to that we have to to print output which is the day of the week. So here we will write an example of switch case program.
The question is to write a program to print the day based on the date number. So the user will give a day number which will be between 1 to 7. We will take input from the user and then we will use the switch case to compare it with the 7 numbers and according to this we will get the day number. days of the week print kar rahe hoge.
Thik hai. To switch day. Sorry. Switch day and case 1 mera hoga. Next line mein likhne hai isko.
Jahan pe I will be printing Monday. And then break statement. So, yahan pe hum break isle likhte hai.
Taaki. case 1 if it is meeting then its code should be executed and come out of switch statement if we will not put break statement here then if case 1 matches then all the cases after that and the default case will be executed so our case 2 would be Tuesday similarly our case 3 would be Wednesday and so on. I don't think we need to write all of them. You get the idea, right? So similarly, we can also make a default case here.
Okay, I will make a default case here where I will write default case. and I will show you the break statement let's remove the break statement from the fourth case so what will happen is that if the fourth case matches then it will first print Thursday and then it will also print the default case ok so let's run it if I put 3 here then I get Wednesday if I put 4 here So, first I get Thursday and then the default case. So, let's use break statements to avoid this. Let's see another example of switch statement. Now, the question is that you have to take a character input from the user and you have to tell that it is a consonant or vowel.
Now in our 26 alphabets we know that A I O U is our vowels. So what we can do is we can put switch case. If any of them is matching then it is a vowel. Or else we can put it in default case that it is a consonant. Right?
So let's code this. Character, character. And user say him.
we will take the input of the character after that we will have a switch case where we will be comparing the character to what first of all we will write A and similarly we will write the cases of our other vowels so in this case we will print vowel and similarly our 4 more cases will come. So here if any case does not match then it will go to the default case and print consonants. Okay.
So let's run this and see if we are getting the right result. And vowels. So let's put a vowel first. And we are getting the right result.
H consonants. Okay. So, once again, this question of vowels, use if else statement and use in teleif else that we are applying conditions in else if and or we can use logical operators here.
We will have 5 cases and we can apply logical operators in all of them. Which logical operator will we apply? and or or? If any of them is matching then it is a vowel or else we will print consonants. So you have to try this once.
And for now we will do some MCQs which we have read today. So here in the first MCQ we have to tell what will be the output of our program. So first it is defining these two variables our X and Y.
And it is saying that if Y modulo X is equal to equal to 0, then print good or print bad. Modulo I told you operator that calculates the remainder. So if we divide 90 by 60, our remainder will be 30. So here our print will be bad.
Our if statement will not go in, else statement will go in. Next what it says? If y modulo x is equal to 10, this condition is not true.
So, we are not going here. Else if there is one more condition. If y minus x is equal to 30. So, that is true. y minus x is equal to 30. So, we are going in this condition.
Means, this condition is meeting. So, we are going in this block. Else if our print will be neutral. Right. Next MCQ is if Y multiplied by X equal to equal to 540 and this is logical and X plus Y equal to equal to 150. So here there are two conditions.
If both the conditions are true then only it will go in the if block. Y into X is not equal to equal to 540. So here it will be false. and I told you that if the first statement is false then we know that the result of AND will be false so here our else statement will be going and print will be bad next MCQ let's see here it is like the previous one but here we have put OR operator instead of AND in OR operator we know that if any condition is true then it will be true here we have seen this condition that it is false Let's see the second condition, x plus y equal to equal to 150. This condition is true. So, false or true, our result will be true. So, it will be in the if block and it will print good.
Let's see the last MCQ, what is the output of the following? Our switch statement is being used. So we have made a variable course and given it a value of 2. Now here we have made two cases for the course.
Case 1 will print Java, case 2 Python and default CPP. So what should be printed here? It should go to case 2 and it should print Python. But if you see one thing here, here break statement is missing.
Isn't it? Break statement. is missing.
If we don't have break statement here, then what will happen is that first it will match on the case2 but after printing that python it will print that cpp also. So if we had break statement here then our python would have been printed but now what would be printed? Python cpp.
This is what will get printed in this case. You can run the code and verify what output is coming. And that's all from my side.
I hope you have understood what conditionals are in today's lecture. Practice the questions. You will get all the notes.
Do a lot of questions. Use if-else, else-if, nested if-else and switch cases. We will use this a lot in the future questions.
Okay, so meet you in the next lecture. Thank you and goodbye.