C Programming Tutorial 49 – Relational Operators

In the last blog I spoke of Good Coding Practices.  Check the one out first! Are you new here? Start at the beginning, Intro to C!

In the previous videos we have worked with if statements that work with strictly true or false values.  We’ve used both bool variables and we’ve also replaced variables with actual values and learned that any number that is not zero is considered to be true. 

What if we want to change the way our if statements work and instead of consider anything not zero to be false, we want to pick our own rules with what is true and false?  This is where relational operators come in.

The easiest way to explain relational operators is to give an example.  Let’s say we are creating an application that asks the user on a scale from 1 to 10, how much they love pizza.  I know, super practical right?

The concept can be applied to many examples.  For example, we can use the principles here to restrict apps by username and password once we get into some more advanced comparisons.  For now, we will stick to simple examples, though.

We, as senior lead senior architects at whatever company we are working for, have decided that the app should only grant access to people who give a rating of 7 or higher. 

This means we need an if that evaluates to true of the number is larger than 6.  To do this, we would write code like this:

where pizzaRating is the variable used to store the input.   In this situation, the entire expression pizzaRating > 6 will evaluate to either true or false.  Fortunately, if statements are actually pretty easy to read.  It literally read something like, “if pizza rating is greater than 6 then grant access.”  Now this expression is evaluated based on the value of pizza rating.  

If pizza rating is greater than 6, the body will be executed. 

If pizza rating is not greater than 6, it will skip the body.  

This is just one example of a relational operator, but there are many more.  Let’s go over them.   

The easiest two are < and >.  

The next are <= and >=. 

Then, we have ==.  This one is interesting because you might be used to seeing = instead of ==.  This is a big challenge for new programmers to get used to, but please remember that you will want to use two equal signs when seeing if two values are equal.  That’s because a single equals sign is already reserved as the assignment operator.   

Lastly, we have !=, which is not equal.  Most of the time, the ! in computer science means not, which flips the value.  You can actually use this operator in a of cool ways, as I’m sure you’ll see as time goes on.  The kind of annoying thing that you have to get used to hear though is that we are flipping the equality operator, not the assignment operator.  So  != means not equal while == means equal.  Remember that there is one less = in the not equal operator.  

Now, what if we do something like this: 

It would be the same thing as: 

Which is the same thing as pizzaRating > 6.  The main thing I am trying to show here is that the relational operators have a lower precedence than arithmetic operators.  In the next blog we will be going through an example of using these operators in actual code!

Leave a Reply

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