Codementor Events

Conditional, Logical and Relational

Published Sep 08, 2021

Conditional statements are heavily used in almost every programming project there is. They are also big part of assignments and quizes. They are mostly used together with relational (==, !=, <, >, <=, >=) and logical (&&, ||) operators.

Understanding how to read them properly will save you lots of debugging, and trial and error.

For the purpose of this post I'll use Java code, but the concepts can be reused among any other language out there.

Let's see an example that might pop up on a quiz.

Given the following example. What's the return value of variable 'x'?

int a = 9;
int b = 17;
int x = 2;

if (a <= b) {
  x = 4;
} else {
  if (a != b) {
    	x = 6;
    } else {
    	x = 5;
    }
}
return x;

So, here's the quiz trying to make you trip over. The reality is that for reading this, you need to pay attention at the if-else at root level, like so:

if (a <= b) {
// ...
} else {
// ...
}

Given that, you can replace the variables in your head (or in a notepad).

if (9 <= 17) {
// ...
} else {
//...
}

Since 9 is most certainly lower than (or equal to) 17, then we only care about that portion of the code.

if (a <= b) {
  x = 4;
}

The answer to the question would be "4".
Now. Consider the same example, with some slight changes.

int a = 9;
int b = 17;
int x = 2;

if (a > b) {
  x = 4;
} else {
  if (a != b) {
    	x = 6;
    } else {
    	x = 5;
    }
}
if (a > x) {
  x = 3;
}
return x;

At first glance, you would feel tempted to persuit the else block.

if (a > b) {
// ...
} else {
// ...
}

And you wouldn't be wrong, whatever it is inside the else block will be executed. But... for the purpose of the excercise, it doesn't really matter. Since we have an additional condition at the same level, that also gets executed, after the initial if-else statement. And it also computes to true.

if (a > b) {
// ...
} else {
// ...
}
if (a > x) {
// ...
}

So that's the only block we'll be interested in to answer the question:

if (9 > 2) {
  x = 3;
}

Since 9 is most certainly greater than 2. The answer for this example is '3'.

Something similar happens with logical operators. The difference between AND(&&) and OR(||) is that && requires that both ends of the operation to compute to true in order to get executed. And || only needs that one end of the operation to compute to true in order to get executed.

Consider the following example.

a = 2;
b = 3;
x = 4;

if (a > b && a != x && (x < b && a != b)) {
  x = 5;
}

return x;

Wow, that looks crazy... but...
Again, here the quiz only wants to trip you over.
Remember, the && operator requires for every end to compute to true.
So, when we see the first end of the operator; a > b, we read it as their asigned values 2 > 3. That would compute to false. And that's all we need to know. We don't even care about trying to find out what the other clauses would compute to i.e. a != x && (x < b && a != b).
That block won't even get executed. Meaning that we don't even need to read it.
Keeping that in mind, we can read above code as the following:

a = 2;
b = 3;
x = 4;

if (a > b) {
  // ...
}

return x;

Since the if block doesn't even get executed. The value of x remains as its original asignment. In this case, '4'.

Bonus track:
Above examples are actually dirty and hard to read, and that's what quizes usually do, trying to make students to trip over. But when we write actual code, we use meaningful names, and get rid of code that never get's executed. An example closer to a real world scenario would be something like:

public void printHowTemperatureFeelsOutside(int temperature) {
int coldTemperature = 17;
int hotTemperature = 22;

  if (temperature <= coldTemperature) {
    	System.out.println("It's cold outside.");
    } else if (temperature < hotTemperature) {
    	System.out.println("It's tempered outside.");
    } else {
    	System.out.println("It's hot outside.");
    }
}

So, another way to give these examples some clarity is to try to read them with less abstract examples. 'a' and 'b' tend to be too abstract sometimes, using a notepad and replacing the variables with their asigned values helps too. Keep in mind what code would get executed and what doesn't, and the order that it gets executed as well.

Happy coding!

Discover and read more posts from Nicolas Pohle
get started
post commentsBe the first to share your opinion
Show more replies