1 / 16

Boolean Logic in Programming

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.

sari
Download Presentation

Boolean Logic in Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Boolean Logic in Programming

  2. 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.

  3. Boolean Logic • A form of mathematics in which the only values are true and false. • There are three basic operations: • AND • OR • NOT

  4. 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

  5. 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.”

  6. 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

  7. 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)

  8. Logical Comparison Operators

  9. 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.

  10. Example: Boolean Function

  11. 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.

  12. Seaplane Example

  13. Seaplane Boolean Functions

  14. 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.

  15. Comparison functions + Boolean logic functions in Alice

  16. 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)

More Related