Skip to main content
Logo image

Subsection Logical Statements (Questions)

A logical statement is any MATLAB expression that produces a logical value, either true or false. The most common logical statements come from comparing two values. These comparisons use relational operators.
Table 19. Relational operators in MATLAB
Relational operator Logical statement Question being asked
== x == y Is \(\texttt{x}\) equal to \(\texttt{y}\text{?}\)
~= x ~= y Is \(\texttt{x}\) not equal to \(\texttt{y}\text{?}\)
< x < y Is \(\texttt{x}\) less than \(\texttt{y}\text{?}\)
<= x <= y Is \(\texttt{x}\) less than or equal to \(\texttt{y}\text{?}\)
> x > y Is \(\texttt{x}\) greater than \(\texttt{y}\text{?}\)
>= x >= y
\(\text{Is}\ \texttt{x}\ \text{greater than or equal to}\ \texttt{y} \text{?}\)
A common beginner mistake is confusing the assignment operator = with the comparison operator ==. The single equal sign assigns a value to a variable (telling MATLAB what to store), while the double equal sign compares two values (asking MATLAB a question).
cost = 7.1;     % Telling: assign a value
cost == 7.1     % Asking: compare a value

Checkpoint 20. Assignment vs. Comparison.

Which of the following statements is correct?
  • x = 5 sets x equal to 5, while x == 5 tests if x equals 5
  • Correct! The single equal sign is for assignment, and the double equal sign is for comparison.
  • x = 5 and x == 5 both do the same thing
  • Incorrect. These operators have completely different purposes: one assigns a value, the other asks a question.
  • x == 5 assigns 5 to x only if x doesn’t already have a value
  • Incorrect. The == operator never assigns values; it only compares them.
Here are a few comparison examples. The parentheses are optional, but they help clarify which parts of the expression are being compared.
a = 4; 
b = -4;

ans1 = (a == 4);	% ← Is a equal to 4?	Yes.
ans2 = (a == b);	% ← Is a equal to b?	No.
ans3 = (b < a);		% ← Is b less than a?	Yes.
This code defines two double variables, a and b, and three logical variables:
  • ans1 = 1 since the logical statement a == 4 is true.
  • ans2 = 0 since the logical statement a == b is false.
  • ans3 = 1 since the logical statement b < a is true.

Checkpoint 21. Check Your Understanding: Logical Statements.

(a) Logical Statements.

Let x, y, and z be numeric variables. Which of the following are valid logical statements in MATLAB?
  • x = y
  • Incorrect. The expression x = y uses a single equal sign, which is for assignment, not comparison.
  • z > y
  • Correct! The expression z > y is a valid logical statement that asks if z is greater than y.
  • y =< 2
  • Incorrect. The expression y =< 2 uses an incorrect operator; the correct operator for "less than or equal to" is <=.
  • z ~= z
  • Correct! The expression z ~= z is a valid logical statement that asks if z is not equal to z.
  • x ~ z
  • Incorrect. The expression x ~ z is not a valid logical statement because it lacks a proper relational operator.

(b)

What is the result of the following logical expression in MATLAB?
p = 10 > 5;
  • 1 (logical)
  • Correct! Since 10 is indeed greater than 5, the expression evaluates to true (logical 1).
  • 0 (logical)
  • Incorrect. Remember that 10 is greater than 5, so the expression evaluates to true.
  • 1 (double)
  • Incorrect. While 1 (double) and true (logical 1) may seem similar, they are different data types in MATLAB.
  • 0 (double)
  • Incorrect. The expression evaluates to true (logical 1), not false.

(c)

What is the result of the following logical expression in MATLAB?
r = (pi ~= 3.14);
  • 1 (logical)
  • Correct! Since 3.14 is only an approximation of pi, it is not equal to pi.
  • 0 (logical)
  • Incorrect. Since 3.14 is only an approximation of pi, it is not exactly equal to the value of pi in MATLAB, so the expression evaluates to true.
  • 1 (double)
  • Incorrect. While 1 (double) and true (logical 1) may seem similar, they are different data types in MATLAB.
  • 0 (double)
  • Incorrect. The expression evaluates to true (logical 1), not false, since pi and 3.14 are not exactly equal.