Lesson 60 +10 XP

Boolean Expressions

Boolean Expressions

A boolean expression is a question that has yes or no (true or false) as its answer.

Comparison operators create booleans

cout << (5 > 3) << endl;   // true
cout << (5 < 3) << endl;   // false
cout << (5 == 3) << endl;  // false
cout << (5 != 3) << endl;  // true

The operators

  • > is greater than.
  • < is less than.
  • == is equal to.
  • != is not equal to.
  • They always return a bool.

Comparing variables

int x = 5;
int y = 3;
cout << (x > y);  // true

The expression x > y is evaluated and gives a boolean: true or false. Remember to use == for equality, not a single = (which assigns).

TL;DR

  • Comparison operators like >, <, ==, != create boolean expressions.
  • 5 > 3 evaluates to true.
  • Store or print the result of a comparison.
  • Comparing variables produces a bool.