Brent M. Dingle Texas A&M University Chapter 6, Sections 1 and 2

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
Lecture 7 Topics –Boolean Algebra 1. Logic and Bits Operation Computers represent information by bit A bit has two possible values, namely zero and one.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Selection Boolean What is Boolean ? Boolean is a set with only two values : –true –false true and false are standard identifiers in Pascal, called Boolean.
Loops Brent M. Dingle Texas A&M University Chapter 7 – part D (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Chapter 5: Making Decisions
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Laws of Boolean Algebra Commutative Law Associative Law Distributive Law Identity Law De Morgan's Theorem.
Decision Making CMSC 201 Chang (rev ).
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators.
Loops Brent M. Dingle Texas A&M University Chapter 6 – Section 6.3 Multiway Branches (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Fall 2001(c)opyright Brent M. Dingle 2001 Multidimensional Arrays Brent M. Dingle Texas A&M University Chapter 10 – Section 2, part B (and some from Mastering.
Copyright © Curt Hill The C++ IF Statement More important details More fun Part 3.
 Type Called bool  Bool has only two possible values: True and False.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Control Structures I Chapter 3
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 4 The If…Then Statement
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Making Choices with if Statements
Chapter 3: Decisions and Loops
Section 7.1 Logical Operators
More important details More fun Part 3
CMPT 201 if-else statement
EGR 2261 Unit 4 Control Structures I: Selection
CPSC Pascal Brent M. Dingle Texas A&M University 2001, 2002
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Decision Structures and Boolean Logic
Introduction To Robot Decision Making
JavaScript conditional
Topics The if Statement The if-else Statement Comparing Strings
Logical Operators & Truth Tables.
Relational Operators Operator Meaning < Less than > Greater than
Georgia Institute of Technology
Chapter 4: Decision Structures and Boolean Logic
Introduction to Primitive Data types
Logical Operations In Matlab.
Procedures Brent M. Dingle Texas A&M University
Introduction To Robot Decision Making
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
CprE 185: Intro to Problem Solving (using C)
Chapter 2 Programming Basics.
JavaScript conditional
The Data Element.
Relational Operators.
Evaluating Boolean expressions
Simple Branches and Loops
Selection—Making Decisions
The Data Element.
Chapter 4: Decision Structures and Boolean Logic
Complex Array Structures
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Brent M. Dingle Texas A&M University Chapter 5 – Section 1
CprE 185: Intro to Problem Solving (using C)
Boolean Expressions September 1, 2019 ICS102: The course.
Introduction to Primitive Data types
Control Structures.
Presentation transcript:

Brent M. Dingle Texas A&M University Chapter 6, Sections 1 and 2 Boolean Expressions Brent M. Dingle Texas A&M University Chapter 6, Sections 1 and 2

True or False Boolean expressions are named after George Boole. A boolean expression has only two possible values – usually thought of as true or false. Boolean expressions are sometimes referred to as conditional expressions.

Recall Comparison Operators Pascal English Example = equal to Ans = ‘Y’ <> not equal to x <> y < less than z < 5 <= less than or equal to z <= 10 > greater than q > -9 >= greater than or equal to q >= 12

Recall Comparison Ops 2 Comparison Operators Result in Boolean Expressions. So the expression: (x < 2) is a boolean expression which EVALUATES to true or false.

Another ‘comparision’ Say we had a set of values: { 1, 4, 6, 11, 21, 38 } and we wanted to know if the variable x was equal to any of those values. We could ask a lot of questions like: is x = 1 is x = 4 is x = 6 and so on until we got to is x = 38 But that would take 6 questions.

Another ‘comparision’ 2 So rather than ask 6 questions we would like to ask just one: Is x in the set { 1, 4, 6, 11, 21, 38 } Pascal lets us do this by saying: ( x IN [1, 4, 6, 11, 21, 38 ] ) And if x’s value is one of those values the above expression would evaluate to be true. Note Pascal only allows character sets or integer sets containing the values from 0 to 255.

And, or, not The reserved words AND, OR, NOT have special meanings when used in Pascal. They allow us to create complex boolean expressions.

NOT Not is used to negate a boolean expression. For example you might say: not (x < 2) Which would evaluate the same as: (x >= 2)

AND Loosely speaking, AND is used to require two boolean expressions to both be true. For example if you wanted to know if x is between 0 and 5 you could say: (x > 0) AND (x < 5)

OR Loosely speaking, OR is used to require one condition OR another to be true OR both. For example if you wanted to know if x is less than 3 OR greater than 10 you could say: (x < 3) OR (x > 10)

NOT – truth table Expression Value not (true) false not (false) true

AND – truth table Expression Value true AND true true true AND false false AND true false AND false

OR – truth table Expression Value true OR true true true OR false false OR true false OR false false

Where to use boolean expressions Most often you will use boolean expressions in if-then-else (conditional) statements or in loops.

Complex Boolean In Pascal we are allowed to mix up and create very complicated boolean expressions. For example we could say: if ( NOT (x < 3) AND (y > 4) ) OR ( x < 3) AND ( y <= 4) then But we should try to make them as easy to evaluate as possible.

Examples of complex boolean expressions Refer to the book (or presentation in class) for examples of complex boolean expressions.

Short Circuit Evaluation Turbo Pascal defaults to short circuit evaluation of boolean (conditional) expressions. This means that in a program as soon as a determining expression is evaluated in a complex expression, the evaluation of the complex expression stops.

Short Circuit Example Pretend x = 10 and y = 7 Consider the below statement: if (x > 12) AND (y > 5) then As soon as the (x > 12) expression is evaluated to false we know that the complex expression (x > 12) AND (y > 5) MUST be false (because false AND [anything] is false).

Short Circuit Evaluation 2 Short circuit evaluation can be used to do ‘clever’ things in a program. Good programmers will NOT rely on short circuit evaluation to do anything. Doing so makes programs difficult to read and maintain and further relies on the assumption that short circuit evaluation will always occur – which is NOT a valid assumption.

Short Circuit Evaluation What you need to know Because not all programmers are good, you must be able to understand how a program would work if it was using short circuit evaluation and how the same program would work if it was not using short circuit evaluation. Note if short circuit evaluation is NOT used, then we say complete evaluation was.

Boolean types On a nice note, if you ever want to see the value of a boolean expression, Pascal allows you to declare variables of type boolean. These variables may have only two values: true or false. The program on page 208 offers a good example of how to use boolean variables.

Excercises (not graded) I would recommend doing exercises: 1, 2, 3 on pages 206, 207 4, 5, 6, 7, 9, 10 on pages 211, 212

End of Boolean Expressions