Open main menu

Wikibooks β

C++ Programming

< C++ Programming‎ | Programming Languages‎ | C++‎ | Code‎ | Statements‎ | Variables‎ | Operators


To do:

  • Add missing information Relational operators.


Logical operatorsEdit

The operators and (can also be written as &&) and or (can also be written as ||) allow two or more conditions to be chained together. The and operator checks whether all conditions are true and the or operator checks whether at least one of the conditions is true. Both operators can also be mixed together in which case the order in which they appear from left to right, determines how the checks are performed. Older versions of the C++ standard used the keywords && and || in place of and and or. Both operators are said to short circuit. If a previous and condition is false, later conditions are not checked. If a previous or condition is true later conditions are not checked.

The iso646.h header file is part of the C standard library, since 1995, as an amendment to the C90 standard. It defines a number of macros which allow programmers to use C language bitwise and logical operators in textual form, which, without the header file, cannot be quickly or easily typed on some international and non-QWERTY keyboards. These symbols are keywords in the ISO C++ programming language and do not require the inclusion of a header file. For consistency, however, the C++98 standard provides the header <ciso646>. On MS Visual Studio that historically implements nonstandard language extensions this is the only way to enable these keywords (via macros) without disabling the extensions.

The not (can also be written as !) operator is used to return the inverse of one or more conditions.

  • Syntax:
condition1 and condition2
condition1 or condition2
not condition

  • Examples:

When something should not be true. It is often combined with other conditions. If x>5 but not x = 10, it would be written:

if ((x > 5) and not (x == 10)) // if (x greater than 5) and ( not (x equal to 10) ) 

When all conditions must be true. If x must be between 10 and 20:

if (x > 10 and x < 20) // if x greater than 10 and x less than 20

When at least one of the conditions must be true. If x must be equal to 5 or equal to 10 or less than 2:

if (x == 5 or x == 10 or x < 2) // if x equal to 5 or x equal to 10 or x less than 2

When at least one of a group of conditions must be true. If x must be between 10 and 20 or between 30 and 40.

if ((x >= 10 and x <= 20) or (x >= 30 and x <= 40)) // >= -> greater or equal etc...

Things get a bit more tricky with more conditions. The trick is to make sure the parenthesis are in the right places to establish the order of thinking intended. However, when things get this complex, it can often be easier to split up the logic into nested if statements, or put them into bool variables, but it is still useful to be able to do things in complex boolean logic.

Parenthesis around x > 10 and around x < 20 are implied, as the < operator has a higher precedence than and. First x is compared to 10. If x is greater than 10, x is compared to 20, and if x is also less than 20, the code is executed.

and (&&)Edit

statement1 statement2 and

The logical AND operator, and, compares the left value and the right value. If both statement1 and statement2 are true, then the expression returns TRUE. Otherwise, it returns FALSE.

if ((var1 > var2) and (var2 > var3))
  std::cout << var1 " is bigger than " << var2 << " and " << var3 << std::endl;

In this snippet, the if statement checks to see if var1 is greater than var2. Then, it checks if var2 is greater than var3. If it is, it proceeds by telling us that var1 is bigger than both var2 and var3.

The logical AND operator and is sometimes written as &&, which is not the same as the address operator and the bitwise AND operator, both of which are represented with &

or (||)Edit

statement1 statement2 or

The logical OR operator is represented with or. Like the logical AND operator, it compares statement1 and statement2. If either statement1 or statement2 are true, then the expression is true. The expression is also true if both of the statements are true.

if ((var1 > var2) or (var1 > var3))
  std::cout << var1 " is either bigger than " << var2 << " or " << var3 << std::endl;

Let's take a look at the previous expression with an OR operator. If var1 is bigger than either var2 or var3 or both of them, the statements in the if expression are executed. Otherwise, the program proceeds with the rest of the code.

not (!)Edit

The logical NOT operator, not, returns TRUE if the statement being compared is not true. Be careful when you're using the NOT operator, as well as any logical operator.

not x > 10

The logical expressions have a higher precedence than normal operators. Therefore, it compares whether "not x" is greater than 10. However, this statement always returns false, no matter what "x" is. That's because the logical expressions only return boolean values(1 and 0).