180 likes | 635 Views
Boolean Logic in Programming. Boolean Logic. Branching and looping routines both contain conditions that are either true or false. In 1854 George Boole outlined a system of logic dealing with true and false values. Today, this is called Boolean Logic . Boolean Logic.
E N D
Boolean Logic • Branching and looping routines both contain conditions that are either true or false. • In 1854 George Boole outlined a system of logic dealing with true and false values. • Today, this is called Boolean Logic.
Boolean Logic • A form of mathematics in which the only values are true and false. • There are three basic operations: • AND • OR • NOT
Boolean Logic • AND and OR each require two operands • NOT requires one operand • When two values are combined using AND, the result is true if both values are true. • Result of an OR operation is true if either value is true. • Result of a NOT operation is the opposite value
Must be precise How do I specify the following in Boolean logic: “Give me a list of all our offices in Pennsylvania and New Jersey.”
Comparing Values • Conditions in branching and looping routines are often based on Boolean expressions that compare values. • Example: • “The passenger-side air bag may cause injury to children who are under the age of 12 or who weight less than 48 pounds.” • IF (age < 12 OR weight < 48) THEN do not sit in the front passenger seat
Comparing Values IF (age < 12 OR weight < 48) THEN do not sit in the front passenger seat • Evaluate age < 12 (yields true/false) • Then weight < 48 (yields true/false) • Combine the two values using OR (yields true/false)
Boolean Functions • True and false values in computing can also come from Boolean functions. • A function is a method that returns a value. • You can use methods to determine the distance between two objects, etc. • A Boolean function returns a true/false value instead of a numeric value.
Built-in Boolean Functions • Is behind is one of many functions built-in to nearly all classes in Alice. • Many Boolean functions exist for our use.
Comparison Functions • Alice also has comparison functions. • These can be found on the world’s function tab. • Using AND, OR, and NOT functions together with the comparison functions, we can build arbitrarily complex Boolean expressions.
Examples • If (age < 12 OR height <= 54) • While (aliceLiddel distance to whiteRabbit <= 10) • While NOT (target = windmill) OR NOT (target = gazebo) • If NOT (seaplane is in front of camera) AND ((time < 10) OR (time > 30)) • While numberOfGuesses <= log(range)