Lesson 3.11. Logical operators

In lesson 3.5, we studied bitwise logical operators. The logical operations were then applied bit by bit. The logical operators studied in this course work according to the following rule:

Here are some examples:

There are three logical operators:

Operator Name Type
&& logical AND binary
|| logical OR binary
! logical NOT unary

As for the comparison operators, these operators can only return 0 or 1. The returned value is always of integer type (int).

These operators are mostly used to combine comparisons:

if (a<5 && a>10)
  ...

Quiz

For a logical operator, the value -5 is considered as ....

Check Bravo! Any value other than zero is considered true. Try again...

What should be the type of the variable x?

x = 1 || 0;
Check Bravo! The type returned by the logical operators is always int Try again...

What is the bitwise logical operator AND?

Check Bravo! Do not confuse the bitwise logical operator & with the logical operator &&. Try again...

What does the following program display?

printf ("%d", 1565 || 0);
Check Bravo! We realize here a logical OR between 1565 (considered as TRUE) and 0 (considered as FALSE). Try again...

What does the following program display?

printf ("%d", (12 || 0) && 127);
Check Bravo! The equivalent logical operation is : \( (1 + 0).1 = 1 \). Try again...

How to test if p is between 0 and 100 inclusive?

Check Bravo! p must be greater than 0 AND less than 100. Try again...

What is the equivalent instruction?

x = !(a<b);
Check Bravo! The operator ! inverts the logical proposition a<b. Try again...

See also


Last update : 11/04/2022