Cloudbook: C

  1. Home
  2. Variables
  3. Logicals
  4. § 2 exercise →
Logicals

In the wide world of logical operators, the relational and equality operators compare numbers and strings for size equality, or inequality. Different operators are used for strings and for numbers. To compare numbers, == tests for equality, and != for inequality; < for strict less than, <= for less than or equal to, > for strict greater than, and >= for grater than or equal to.

Test for equality and assignment have different symbols, == versus =. Be careful not to confuse them.

To compare strings, "eq" tests for equality, "ne" for inequality; "lt", "le", "gt" and "ge" are the tests for inequality.

There is also "and" and "or" operations that will combine logical values. For instance, to test that a number is between 2 and 10, test that it is less than or equal to 10 "and" it is more than or equal to 2. The "and" operation is &&; the "or" operation is ||. Be careful, there are non-logical and and or operations, written with single characters & and |. These give different results.

Copyright 2015, 2017 Burt Rosenberg. All Rights Reserved.