Preliminary
- Up until now, all code was executed in a linear fashion
- The way the code is executed is called the flow of control
- Now we are going to work on ways to alter the flow of control
Type Boolean:
- George Bool
- Two basic elements True and False
- Rules for combining (or operations on) these elements
- And (Both must be true)
-
A | B | A and B |
F | F | F |
F | T | F |
T | F | F |
T | T | T |
- Or (either one or the other can be true)
-
A | B | A or B |
F | F | F |
F | T | T |
T | F | T |
T | T | T |
- not (oppisite of what we have)
- These tables are called truth tables.
- In Bool's system, we can construct algebraic expressions
- (A and (not B)) or ((not A ) and B)
- This is called an Exclusive Or
- We can build a truth table for this.
- This entire system is called Boolean Algebra.
- We have a boolean data type
- bool done,
avalue = true,
work = false;
- This is really an integer type, where the values are
limited to 0 and 1, 0 = false, 1 = true
- && is the and operator
- || is the or operator.
- We can generate values from other types too.
- == tests to see if two values are the same
- Be careful this is not =
- != tests for inequality
- > is greater than
- < is less than
- >= greater than or equal to
- <= is less than or equal to
- We can compare floats, chars, strings and ints this way.
- We can combine these to make complex statements
- Consider this code