Course Introduction to Programming

Logical Operators

Course's Page 14 min Text by
User Image
Rahilly Machado

In this lesson, we will deepen our knowledge of Logical Operators, fundamental resources for building programs capable of making complex decisions based on multiple conditions.

We will learn how to use the AND (&&) and OR (||) operators to combine and invert logical expressions, understanding how the computer interprets these conditions. Additionally, we will explore the concept of short-circuiting, which influences how expressions are evaluated, and we will review the rules of precedence and the use of parentheses to ensure that the program's logic is executed exactly as planned.

AND Operator

Imagine the following problem: how to find out if an integer is even and positive?
To solve this problem, we first need to know the necessary conditions for an integer to be even and positive:

  • Even: % ==
  • Positive:

Knowing these conditions, we can proceed with two approaches:

1. Use two nested if's:

if( x % 2 == 0 ){ // checks if x is even
    if( x > 0){ // if x is even, then it checks if it is positive
        printf("Even and positive!\n"); // if x is even and positive, it executes the printf
    }
}

2. Use a single if with the logical AND operator:

To avoid using a nested if, we can use the logical AND operator. This operator is represented by two ampersands (&&) and will only have a true value if both statements being analyzed are true. Its syntax is as follows:

<statement1> && <statement2>

Therefore, to solve our problem, where must be even and positive, we can use this operator as follows:

if( x % 2 == 0 && x > 0 ){
    printf("Even and positive!\n");
}

This way, the condition analyzed by the if will only be true if both statements (even and positive) are true.

OR Operator

Now, imagine another problem: how to find out if a number is even OR positive?
In this case, we again have two approaches:

1. Use two if's:

if( x % 2 == 0 ){ // checks if x is even
    printf("Even or positive!\n"); // if x is even, it executes the printf
}

if( x > 0){ // checks if x is positive
    printf("Even or positive\n"); // if x is positive, it executes the printf
}

2. Use a single if with the logical OR operator:

To avoid using two if's, we can use the logical OR operator. This operator is represented by two vertical bars (||) and will have a true value if at least one of the two statements being analyzed is true. Its syntax is as follows:

<statement1> || <statement2>

Therefore, to solve our problem, where must be even or positive, we can use this operator as follows:

if( x % 2 == 0 || x > 0 ){
    printf("Even or positive!\n");
}

This way, the condition analyzed by the if will be true if at least one of the two statements (even or positive) is true. That is, if the number is even, or positive, or both even and positive.

Short-Circuit

Short-circuiting is an important behavior of the && and || logical operators. It allows avoiding unnecessary comparisons, making the program more efficient.

How it works:

  • In the AND (&&) operator, if the first condition is false, the rest is not evaluated (because the result will already be false).
  • In the OR (||) operator, if the first condition is true, the rest is not evaluated (because the result will already be true).

Examples:

  1. The second condition (x > 0) is not even checked, because the first one is already false.
if(0 && x > 0){
    printf("Doesn't enter\n");
}
  1. Here, the second condition is not evaluated, because the first one is already true.
if(1 || x > 0){
    printf("Enters the IF\n");
}

Precedence

In operations where multiple logical operators are used, the AND operators are checked before the OR operators. To make it easier to understand, analyze this example:

// false  false true
if( 2<1 && 2<0 || 1<2 ){
    printf("Entered the IF\n");
}
/* Output: 
Entered the IF

*/

Notice that, as commented in the code, the first statement is false, the second is false, and the third is true. To make it easier to visualize, we can represent it as follows:

if(false && false || true){
    printf("Entered the IF\n");
}

According to precedence, the AND operator will be analyzed first. Since the two statements it analyzes are false, its value will also be false. Thus, we can reduce the condition:

if(false || true){
    printf("Entered the IF\n");
}

Analyzing the OR operator, its value is true because the second statement is also true.
Therefore, the condition analyzed by the if has a true value and the printf is executed.

Parentheses

Just like with arithmetic operators, we can use parentheses to change the order of execution of the operators. Thus, if the code above is written as follows, we will have a completely different result:

if(2<1 && (2<0 || 1<2) ){
    printf("Entered the IF\n");
}
/* Output: 

*/

Notice that by first analyzing the OR, we will get a true value, because its second statement is true. After that, the AND operator will be analyzed and will result in false, because even though its second statement is true, the first is false. Therefore, the printf inside the if will not be executed and nothing will be printed on the screen.

Conclusion

In this lesson, you learned how to use logical operators (&&, ||) in conjunction with if, creating more complex and expressive conditions. You understood how short-circuiting works, which optimizes the execution of conditions, and the precedence between operators, as well as how parentheses can change the order of evaluation and improve code clarity.

Now you know how to create smarter and safer conditional structures, controlling the flow of programs in a logical, efficient, and precise way! ๐Ÿ˜ƒ