Team 2550
Technical Documentation
 All Files Variables Pages
Program Flow

Programming would be almost useless if it were simply a list of actions to perform. This is where flow control comes in. Using flow control, you can allow your program to make decisions.

Boolean Logic

One useful capability of almost any programming language is the ability to take multiple tests and put them into a single boolean (true/false) result. This is done through logic gates.

C++ Logic Gate Operators

Operation Symbol
OR ||
AND &&
NOT !

An OR statement takes two values as input and, if either value is true, the or statement will be true. Note that "either" also includes both values as shown in the truth table below. An OR operation uses short-circuit evaluation, meaning that the computer will not run any more comparisons than necessary. For instance, if the first value in the OR statement is TRUE, then the output will be TRUE regardless of the second operand - the computer will skip the second comparison if it can.

Note
A truth table is a simple table which shows the behavior of a logic gate by showing the output for every possible value of inputs (in order to show the behavior in any situation, you only need two inputs).

1 = TRUE
0 = FALSE

An AND statement is only true if all of the inputs are true. Short-circuit evaluation is not possible when using the AND operator.

The NOT operator returns the opposite of the actual result of an evaluation so that TRUE = FALSE and FALSE = TRUE. This can be very useful, as you can put the "!" operator in front of any logical statement. For instance, !(2 > 3) will return FALSE.

Truth Tables

A B A || B A && B
0 0 0 0
0 1 1 0
1 0 1 0
1 1 1 1
Note
It is very important that you remember to type in || for "OR" and && for "AND" - putting a single | or & character will use the logic gate in bitwise mode, which is completely different.

NOT Truth Table

INPUT !(INPUT)
0 1
1 0

Order of Operations

When evaluating an expression, C++ has a defined order of operations for logic gates. The complete order of operations for expression evaluation in C++ is as follows...

  1. Parenthesis
  2. Multiplication and division from left to right
  3. Addition and subtraction from left to right
  4. Comparison operations (inequalities), includes <, <=, >, >=
  5. Comparison operations (equalities), includes ==, !=
  6. Logical AND "&&"
  7. Logical OR "||"

A more complete table can be found at cppreference.com

Even though C++ has a set order of operations, adding parenthesis often makes it easier to understand.** The table below demonstrates this.

Expression Result Reason
1 == 2 && 1 < 2 || 1-1 > -5 TRUE Remember that the OR operator is evaluated last. The AND statement evaluates to FALSE, but the other side is TRUE, yielding a result of TRUE. This is the same as writing...

(1 == 2 && 1 < 2) || (1-1 > -5)

true && true || false && true TRUE True because the expression to the right of the || is true. The values "true" and "false" are valid in C++ and correspond to 1 (true) and 0 (false).
!(false || false) && true FALSE True because the opposite of false is true (the ! is outside the parenthesis).
!((false && true) || (false)) TRUE Everything is inside parenthesis, with a ! outside. The opposite of false is true.
!(1<2) FALSE The opposite of true is false.

More Information