Operators in C Programming 2 | Relational & Logical Operators | Decoding Computers

Operators in C Programming  2 | Relational & Logical Operators | Decoding Computers

2 million students appeared for exams this year. Everyone waits on the day of results. And then finally it is declared on tv or newspapers about the highest ranking and top positions. How are these rankings calculated Keep watching this video to know how. And here we will learn about two more of the operators in C language – Relational & Logical Operators. But first I want you to subscribe my channel so that you do not miss out on any of my videos. Let’s first have a quick recap of what we’ve already covered in C operators. Pls watch my previous video for detailed explanation. The link is in the description. We’ve seen what operators are, how many types, what are Unary operators, what are Arithmetic operators & how they operate. We’ve also seen how Precedence & Associativity principle dictates the way an equation is calculated. Let’s come back to our operators chart & see which ones are we going to cover today. Here they are. The relational & the logical operators. Both of them Binary operator type. Remember I told you Binary operators means operators acting on two data elements called operands to produce a result. As the name suggests, these are the symbols that are used to find the relation between two data entities which can be some constant values or variables. After all a variable also carries a value. Which means that Relational operators are used to compare two values. By comparing means if one is bigger than the other, smaller, equal or what ? Let’s see the 6 types of these Relational operators If A and B are the two operands, then – You don’t need to be brainy to know what next one does An “!” is used as NOT in computer languages. Now get this – these operators are there to compare & not to do any mathematical operation. Which means that the result of comparison could either be a Right or a Wrong. In computer programming languages, we say a True for the right answer & False for the wrong one. And in digital world, we use 1 for a true & 0 for a false. This means the Relational operators will give the result as either a zero or a one. Ok, for better understanding let’s take these 3 friends – Ranchoddas Shyamaldas Chanchad ‘Rancho’, Farhan Qureshi & Raju Rastogi & their first year in college. Don’t keep your hopes high, we’re just gonna discuss only their academic results & not their adventures or romance affairs So, lets say they had a class test and the results are out. The plot & screenplay of the movie was wonderful Ahhmmm !! Yeah yeah, okay okay (coming back to programming) In our program, we declare their marks as this – Rancho 95, Farhan 85 & Raju 95. And these statements will get us the comparison result Here this Comparison1 statement is True because 95 is greater than 85 & hence on execution, it gives us a 1. Now since 85 is not greater than 95, Comparison2 statement is False, & hence on execution, we got 0. Also since marks of Rancho & Raju are equal, the 3rd statement will return a False, that is a zero Let’s run this program & see. Correct, that’s what we thought. So, I think this makes it clear about the resultant 0’s & 1’s in case of a Relational operator. How these will be used in program-writing to check conditions, we’ll do later in coming videos. Okay, next you need to understand that these Relational operators also work well with Float type data. If the marks were like this, Rancho 95.45, Farhan 84.92 & Raju 95.20 which are 95, 85 & 95 after rounding up. But what about the results, let’s see. Did you notice that now Comparison3 returns a 1 that is a True. Got that ?? Please remember that these Relational Operators work well with constant-values AND expressions too. Basically it means that Relational Operators work well with all those who want comparison between elements. Now let’s learn about Precedence in these Operators. In this chart you can see precedence from low to high. So, if we have this expression in a program. What would be the result ? Let’s first calculate in steps, using the precedence chart. In this,>&90 & Raju>90. This && ka symbol is for Logical AND & the two conditions are written like this. See the result, the professor got his 1. This means for an expression ‘condition1 && condition2’ the result can be summarized in this table. But it would be a False even when ANY one condition also returns a False. In other words, a Logical AND will return a True ONLY when both the conditions are True, else it will return a False. Similarly, there’s another operator represented by this symbol called a Logical OR operator If we replace this logical AND operator with logical OR, then it means that now both the marks need not be above 90 for the Professor to get a 1 It only needs to have any one condition to be true. Lets make Raju’s marks as 89 & see the result. See, the Professor still got his 1. So, the result table for ‘condition1 || condition2’ would be like this Got that friends ? The 3rd Logical Operator is represented by an exclamation mark and is called a Logical NOT operator. As the name suggests, it returns the opposite value of its operand and it works on a single condition. This is it’s result table. Alright ? Let’s summarize what we’ve learnt so far about Logical Operators. Now guys, we’ve covered Unary Operators, Arithmetic Operators in our last video & Relational Operators & Logical Operators today. And this is their precedence. Let’s use this knowledge and try decoding another expression. Where we’re using a unary prefix increment ++, a>=, a logical AND, a unary postfix decrement –, a plus & a minus. If we place them in correct precedence order, the Logical AND will have the lowest precedence & unary prefix increment the highest. Let’s input Rancho as 4 & Farhan as 6. So the steps of execution would be like this. After execution, value of Farhan is decremented to 5. Please notice Any constant except 0 is considered a True & hence this returns a one. Now let’s see a special case. In the same expression, if Rancho is changed to 1, the steps would be like this. And the result is a zero. The special note here is that Farhan is NOT decremented to 5 but its value remains 6. You see, in case of Logical AND operator, if any one of the conditions is a False, the result is false. So while executing && operation, it checks 1st condition If it is true it goes to check the second condition But if it is false, it doesn’t waste time in checking the second one & the compiler skips it. Therefore in our example, since this would return a false or zero, it will not go to this decrementing condition. Hence Farhan remains 6. Isn’t it interesting ?? The Compiler is intelligent & doesn’t waste any time. We also don’t waste any time in our videos & instead explain the topic in shortest time possible. So keep watching. See you in the next video with more important operators

2 thoughts on “Operators in C Programming 2 | Relational & Logical Operators | Decoding Computers”

  1. it is very beneficial as relational and logical operators are covered with interesting examples and very well explained. Must watch👍

Leave a Reply

Your email address will not be published. Required fields are marked *