Skip to main content
Logo image

Subsection Negating Logical Statements

To ask the opposite of a logical statement, use the NOT operator ~. If an expression is true, its negation is false, and vice versa.
~true	% returns false
~false	% returns true
The NOT operator negates a single logical value or statement. When the statement is a comparison, use parentheses so MATLAB negates the whole comparison.
For example, the following statements ask if “8 is not equal to 9”:
  • ~(8 == 9) \(\quad\Rightarrow\quad\) “NOT (8 is equal to 9)”
  • 8 ~= 9 \(\quad\Rightarrow\quad\) “8 is not equal to 9”
Similarly, the negation of x >= 5 is x < 5, so the two statements are equivalent:
  • ~(x >= 5) \(\quad\Rightarrow\quad\) “NOT (x is greater than or equal to 5)”
  • x < 5 \(\quad\Rightarrow\quad\) “x is less than 5”
When negating expressions, use parentheses to make your intent clear and improve the readability of your code.

Checkpoint 22. Negating Logical Statements.

Which of the following expressions correctly asks "Is x NOT greater than 10?" Select all that apply.
  • ~(x > 10)
  • Correct! This uses the NOT operator to negate the comparison.
  • x <= 10
  • Correct! This is the equivalent relational operator form without negation.
  • ~x > 10
  • Incorrect. Without parentheses, this negates x first, then compares the result to 10, which is not the intended meaning.
  • x < 10
  • Incorrect. This asks if x is strictly less than 10, which excludes the case where x equals 10.