Presentation is loading. Please wait.

Presentation is loading. Please wait.

Boolean Logic.

Similar presentations


Presentation on theme: "Boolean Logic."— Presentation transcript:

1 Boolean Logic

2 Basic Logic Statement Evaluates To (True or False) True False False
The sky is blue The sky is NOT blue The grass is pink The grass is NOT pink A snail is fast A snail is NOT fast The School is 3 stories high The School is NOT 3 stories high A soccer ball is round and pink A soccer ball is NOT round and NOT pink True False False True False True True False False False What do you notice when you compare the two columns?

3 Boolean Variables A Boolean variable can store only two values:
True False Boolean variable naming is the same as all other variable naming except you should choose names written as Yes or No questions E.g. isSkyUp

4 Boolean Variables What would you name each Boolean variable to represent the statements in the first table? The sky is blue The grass is pink A snail is fast The school is 3 stories high A soccer ball is round and pink

5 Boolean Operators Like math has operators; +, -, x, ÷, etc.
Boolean logic has its own operators AND (Turing command is and) E.g. var isWeatherGood : boolean := isSkyBlue and isWarmOutside OR (Turing command is or) E.g. var willBallBounce : boolean := isBallRubber or isGroundRubber NOT (Turing command is not) E.g. var isDayTime : boolean := not(isNightTime) Boolean logic is a fundamental concept in making decisions in programming

6 Boolean Operators AND: OR: NOT:
Evaluates to True when all elements are True, Otherwise the result is False OR: Evaluates to True when at least 1 element is True, NOT: Evaluates to True when the entire expression is False, NOTE: Whenever you use these operators you are creating a complex boolean expression called a Compound Expression

7 Truth Table Example (Follow BEDMAS)
Own Xbox (1) Own PS3 (2) 1 AND 2 1 OR 2 NOT (1) AND NOT (2) NOT (1 OR 2) STEP 1: Fill out ALL possibilities for the basic information, Columns 1 and 2 in this example STEP 2: Fill out the rest of the table ONE COLUMN at a time QUESTION: What do you notice from the last two columns This shows us that NOT (A) AND NOT (B) = NOT (A OR B)

8 Question Why do computer programs need to make decisions? Interactivity – a program is like a movie if it does not change depending on the inputs, and it is not very useful if it is like a movie.

9 In Code Below is an example of a simple program that determines if the user is both tired and hungry: var isHungry : boolean var isTired : boolean var isUserTiredAndHungry : boolean put “Are you tired (true or false)” get isTired put “Are you hungry (true or false)” get isHungry isUserTiredAndHungry := (isTired and isHungry) put “The results of you being both tired and hungry are: “, isUserTiredAndHungry

10 Practice Time Found on the Moodle under the “Selection” Section, compare your responses with another student. Remember: When doing a truth table, fill out all of the possibilities for the base information first, then proceed column by column


Download ppt "Boolean Logic."

Similar presentations


Ads by Google