Boolean Values and Conditional Expressions

Comparison and Logical Operators

Conditional if statements operate on Boolean values. An expression with a comparison operator, such as

1 == 2

evaluates to true or false, depending on the operands. (The above expression evaluates to false. This is the standard behavior of all the comparison and logical operators in C: ==, <=, >=, <, >, !=, && and, ||. ) Note that the above symbol is a DOUBLE = sign, which indicates comparison rather than the SINGLE = sign that is used for assigning a value to a variable.

It is easy to misuse the comparison operators when writing code. In mathematics, the expression:

-2 < -1 < 0

makes sense and would evaluate to true. However, in C, the semantics of these expressions is different, and the above expression in C actually evaluates to false. Specifically, comparison operators associate left to right and evaluate to Boolean values, so -2 < -1 < 0 is evaluated as if parentheses were added to give (-2 < -1) < 0. The first comparison expression evalutes to true, which is actually the integer value 1, because -2 is indeed less than -1. Next, 1 is compared to 0. 1 is not less than 0 so the entire expression evaluates to false.

To do multiple tests at once, use logical AND (&&), and logical OR (||) operators. For example, the previous example can be correctly expressed in C as:

(-1 > -2) && (-1 < 0)

This expression in C works fine, but note that some simplification is possible, because the && operation has lower precedence than either > or <. Taking advantage of the assumed precedence of these options, the parentheses can be removed from the above expression, and C will still understand that it should perform the comparisons with > and < operations before applying &&. Thus, C programmers could simplify the above expression to

-1 > -2 && -1 < 0

However, this version is harder to read by humans, who must then "manually" apply the precedence rules. Adding parentheses will not affect the resulting compiled code, but it will help other programmers understand your intent.

Redundant Comparisons

Conditional statements (ifs) are designed to work with Boolean values. If an expression is true, the specified code will execute. In addition to all the logical and comparison operators in C, many functions also return Boolean values to indicate different outcomes. Review the Zen of Booleans from CSC151, if necessary.

It is a good idea to avoid testing against Boolean values in your conditionals if a value is already considered to be Boolean. If a function or expression is supposed to return true on success and false on failure, do not further test that result against true or false. Consider the following:

if ( true )

and:

if ( true == true )

Initially, it may seem the second case is more accurate than the first. But, add a third case:

if ( true == true == true)

The third case is no more descriptive or accurate than the second in the same way the second is no more accurate than the first. It is good practice to avoid this kind of redundancy in your testing.

Returning Boolean Values

Since comparison and logical operators in C evaluate to Boolean values, good coding practice takes advantage of this evaluation when returning Boolean results from functions. For example, consider the following code segment:

if ( x == y )
  return 1;
else
  return 0;

This code may be correct, but it also is inefficient and wordy. Think about how operators which return Boolean values (such as the == operator) work. If x and y have common values, then the result of the comparison x == y is true—which in C is represented by the value 1. If the comparison is false, the result is false, represented by the value 0. Thus, the return values are exactly what C computes with the statement x == y, and the complex statement above can be simplified to

return ( x == y );

Switch Statements

Instead of using many if statements, we can use switch statements to simplify our code. When there are cases in which a variable is compared to other integer values, and a certain code is executed if the compared values are equal, we can use switch cases.

Here is how to use switch statements:

switch (variable)
{
  case value0:
    //Execute this code if variable == value0
    break;
  case value1:
    //Execute this code if variable == value1
    break;
  case value2:
    //Execute this code if variable == value2
    break;

  // And so on with other cases,

  default:
    // Execute this code if variable did not equal any of the cases above
    break;
}

The program second-counter.c provides an example of how switch statements work:

second-counter.c