C Programming Tutorial 53 – Evaluating Complex Conditionals

In the last blog we went over Logical Operators! Check it out first! Are you new here? Start at the beginning, Intro to C!

This blog we are going to be discussing complex conditionals. First, you let’s do a brief review of conditionals. A conditional is something that evaluates to true or false. Inside of a conditional you can have relational operators. Let’s say we have a variable, x, which has the value 5. We can make a conditional like this:

x < 5.  This is the equivalent of saying 5 < 5, which would evaluate to false. 

Let’s say we have another example of x > 1, which would evaluate to true.   

With complex conditionals, we combine multiple conditionals. So we can say something like x < 5 OR x > 1. The final result ultimately depends on the operator I the middle, which is known as a logical operator.

We’ve discussed those in more detail in the last video, so the point of this video is to practice evaluating more complex conditionals. It’s super important to be able to read these because if you can’t you might have code that is not checking for what it is supposed to be checking for.

There are really two ways to analyze conditionals. The first is if we know the values of any variables. This allows us to work through the process and come up with a final truth-value. The second is if we do not know the values of the variables. In this situation, we write down all possible paths. The process of writing all possible paths is done with a truth table.

First, we will start with knowing the value of the variables.

We can work our way from the inside out to evaluate this expression.  

Most of the time you are not going to know the values of these variables ahead of time, or else there is no point in running a conditional. Because of this, you will want to learn how to think about these conditionals without giving direct values to variables. Consider the variables to have values that we asked from the user.

It’s also beneficial to look for ways to simplify our expression. 

For example, the last part has this: 

which could instead be written as: 

The middle section also has both x > 0 and x != 0.  The second one is unneeded because x > 0 includes checking x != 0.

Read the next blog now!

Leave a Reply

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