• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Caleb Curry

Making Money and Investing

  • Home
  • Make Money Online
  • How to Buy Bitcoin
  • Get Started with Real Estate Investing
  • How to Start a Website
  • Courses
    • Full Time Income Online
  • Sponsorship

C Programming Tutorial 49 – Relational Operators

November 8, 2018 by Caleb Curry Leave a Comment

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!

Filed Under: Uncategorized Tagged With: c programming, relational operators

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

my face to show my face

My name is Caleb Curry and I started my online business teaching software development and doing digital marketing for brands. Entrepreneurship has changed my life and has allowed me to earn a full time living from anywhere. I now have the freedom and energy to pursue skills and opportunities that I find interesting. This website is all about my journey.

Get my top Python Tips and Tricks

50+ Python Tips and Tricks!

Subscribe to our newsletter to get notes and latest content by email.

    We won't send you spam. Unsubscribe at any time.

    Follow Me!

    • Instagram
    • YouTube

    Recent Posts

    Copyright © 2021 · Genesis Sample on Genesis Framework · WordPress · Log in