Operators (Part 3)

Previous Index Next

Relational and equality operators Logical (Boolean) operators Conditional operator

Relational and equality operators Top

In C++, "==" and "!=" are equality operators while "<", ">", "<=" and ">=" are relational operators:

Operator in C++ Operation Sample C++ expression Equivalent Pascal expression
== equal to a == b a = b
!= not equal to a != b a <> b
< less than a < b a < b
> greater than a > b a > b
<= less than or equal to a <= b a <= b
>= greater than or equal to a >= b a >= b

All equality and relational operators are binary operators.

Note that "==" (equality) should not be confused with "=" (assignment).


Logical (Boolean) operators Top

(In the following, assume that a and b are of numerical type in C++ and of Boolean type in Pascal.)

Operator in C++ Operation Sample C++ expression Equivalent Pascal expression
&& logical AND a && b a and b
|| logical OR a || b a or b
! logical negation (NOT) !a not a

As seen from the above table, "&&" and "||" are binary operators, while "!" is a unary operator with its operand on its right.

See the following tables for their operations.

Logical AND (&&)

a b a && b
0 0 0
0 non-zero 0
non-zero 0 0
non-zero non-zero 1

Logical OR (||)

a b a || b
0 0 0
0 non-zero 1
non-zero 0 1
non-zero non-zero 1

Logical negation (!)

a !a
0 1
non-zero 0

There is no Boolean type in C++. The values returned by the equality, relational and logical operations are 1 (true) and 0 (false). In general, non-zero numerical value is considered as "true" while zero is considered as "false" in C++.


Conditional operator Top

The conditional operator is a ternary operator (an operator which operates on three operands). It is in the following form:
    <expression_1> ? <expression_2> : <expression_3>

<expression_1> is always evaluated first. If it is non-zero ("true"), the result of this operation is the returned value of <expression_2>; otherwise, the result is the returned value of <expression_3>. Usually, it may be interpreted as:
    "If <expression_1> then <expression_2> else <expression_3>."

This means that a statement using the conditional operator can be replaced by an if-else control structure.

See the following program as an example:

Program 9: Using the conditional operator
#include <iostream.h>

main()
{
  int x, y, min;

  cout << "Enter x: ";
  cin >> x;
  cout << "Enter y: ";
  cin >> y;
 
min = (x < y ? x : y);
  cout << x << (
x > y ? " > " : " <= ") << y << endl;
  cout << min << " is the smaller number." << endl;

  return 0;
}
Sample output 1 (text in red is entered by user):
Enter x: 10
Enter y: 5
10 > 5
5 is the smaller number.
Sample output 2 (text in red is entered by user):
Enter x: 4
Enter y: 7
4 <= 7
4 is the smaller number.

min = (x < y ? x : y);
If x is less than y, the expression "x < y" will be true (i.e. non-zero) and the value of x will be assigned to min.
If x is greater than or equal to y, the expression "x < y" will be false (i.e. zero) and the value of y will be assigned to min.

x > y ? " > " : " <= "
If x is greater than y, the expression "x > y" will be true, and the above expression will return " > ".
If x is less than or equal to y, the expression "x > y" will be false, and the above expression will return " <= ".

Previous Index Next