Course Introduction to Programming
Logical Operators
In this lesson, we will learn what are and how to use the logical operators, addressing the following topics:
- Operator AND
- Operator OR
- Precedence
- Parentheses
Operator AND
Imagine the following problem: how to find out if an integer number is even and positive?
To solve this problem, first we need to know what conditions are necessary for an integer
Even:
Positive:
Knowing these conditions, we can proceed with two approaches:
- Make two if's, one inside the other.
if( x % 2 == 0 ){ // check if x is even
if( x > 0){ // if x is even, then check if it is positive
printf("Even and positive!\n"); // if x is even and positive, execute the printf
}
}
- Make only one if using the logical operator AND.
In order to not have to use an if inside the other, we can use the logical operator AND.
This operator is represented by the junction of two ampersands (&&) and will only be true if the two statements to be analyzed are true. Its syntax is as follows:
<affirmation1> && <affirmation2>
Therefore, for the resolution of our problem, where
if( x % 2 == 0 && x > 0 ){
printf("Even and positive!\n");
}
Thus, the condition analyzed by the if will only be true if both statements (even and positive) are true.
Operator OR
Now, imagine another problem: how to find out if a number is even or positive?
In this case, we have two approaches again:
- Make two if's.
if( x % 2 == 0 ){ // check if x is even
printf("Even or positive!\n"); // if x is even, execute the printf
}
if( x > 0){ // checks if x is positive
printf("Even or positive\n"); // if x is positive, execute the printf
}
- Make only one if using the logical operator OR.
We can use the logical operator OR so we don't have to use two if's,
This operator is represented by the junction of two vertical bars (||) and will be true if at least one of the two statements to be analyzed is true. Its syntax is as follows:
<affirmation1> || <affirmation2>
Therefore, for the resolution of our problem, where
if( x % 2 == 0 || x > 0 ){
printf("Even or positive!\n");
}
Thus, the condition analyzed by if will be true if at least one of the two statements (pair or positive) is true. That is, if the number is even or positive, or even and positive.
Precedence
In operations where several logical operators are used, the AND operators are checked before the OR operators. To facilitate understanding, 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 the visualization easier, we can represent it in the following way:
if(false && false || true){
printf("Entered the IF\n");
}
According to the precedence, the operator AND will be analyzed first. As the two statements he analyzes are false, so their value will also be false. This way, we can reduce the condition:
if(false || true){
printf("Entered the IF\n");
}
Analyzing the operator OR, we can say that its value is true, because the second value is true.
Therefore, the condition analyzed by if has true value and the printf is executed.
Parentheses
We can use parentheses to change the operators' execution order like with arithmetic operators,
This way, if the above code 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 OR, we will obtain the value true, because your second statement is true. After that, the operator AND will be analyzed and will result in false, because no matter how true your second statement is, the first is false. Therefore, the printf inside the if will not be executed and nothing will be printed on the screen.
See you in the next class!