Boolean values Gateway to decision making. Background Our problem-solving solutions so far have the straight-line property –They execute the same statements.

Slides:



Advertisements
Similar presentations
1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc.
Advertisements

What is the Result and Type of the Following Expressions? int x=2, y=15;double u=2.0,v=15.0; -xx+yx-y x*vy / xx/yy%xx%y u*vu/vv/uu%v x * u(x+y)*uu /(x-x)
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
If Statements & Relational Operators Programming.
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Control Constructs Mechanisms for deciding when and how often an action should be taken.
Boolean Types & Compound Conditionals CSC 1401: Introduction to Programming with Java Lecture 4 – Part 3 Wanda M. Kunkle.
Introduction to Computers and Programming Lecture 5 New York University.
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Computer Science 101 The Boolean System. George Boole British mathematician ( ) Boolean algebra –Logic –Set theory –Circuits –Conditions in if.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Decision Structures and Boolean Logic
Computer Science Selection Structures.
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.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Dale Roberts Program Control using Java - Boolean Expressions Dale Roberts, Lecturer Computer Science, IUPUI Department of.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
Computer Science 210 Computer Organization Introduction to Boolean Algebra.
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.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
1 Decisions, decisions, decisions Chapter 5 Fall 2006 CS 101 Aaron Bloomfield.
1 Decisions, decisions, decisions Chapter 4 Trey Kirk.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
03 August 2004 NLP-AI Java Lecture No. 4 Operators & Decision Constructs Satish Dethe.
1 Programming in C++ Dale/Weems/Headington Chapter 5 Conditions, Logical Expressions.
Decisions, decisions, decisions. Background  Our problem-solving solutions so far have the straight-line property They execute the same statements for.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Sahar Mosleh California State University San MarcosPage 1 Data Types and Operators.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.
Decisions Bush decision making.
Control statements Mostafa Abdallah
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to.
BOOLEAN ALGEBRA LOGIC GATES. Introduction British mathematician George Boole( ) was successful in finding the link between logic and mathematics.
Decisions. Background Our problem-solving solutions so far have the straight-line property –They execute the same statements for every run of the program.
What is the Result and Type of the Following Expressions? int x=2, y=15;double u=2.0,v=15.0; -xx+yx-y x*vy / xx/yy%xx%y u*vu/vv/uu%v x * u(x+y)*uu /(x-x)
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 15, 2004 Lecture Number: 11.
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
Decisions Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Decision Structures Michele Co CS 101-E. 2 UVa CS101E Spring 2007 Today if statements Logical expressions –Truth tables –Boolean algebra –DeMorgan’s laws.
Computer Science 210 Computer Organization
Decisions, decisions, decisions
A mechanism for deciding whether an action should be taken
Decisions.
Multiple variables can be created in one declaration
Type Conversion, Constants, and the String Object
OPERATORS (2) CSC 111.
Expression Review what is the result and type of these expressions?
Bools and simple if statements
Relational Operators Operator Meaning < Less than > Greater than
Computer Science 210 Computer Organization
Control Structure Chapter 3.
Additional control structures
Outline Software Development Activities
COMS 261 Computer Science I
Controlling Program Flow
Control Structure.
Presentation transcript:

Boolean values Gateway to decision making

Background Our problem-solving solutions so far have the straight-line property –They execute the same statements for every run of the program public class DisplayForecast // main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world"); System.out.print(" market for maybe five "); System.out.println("computers. “); System.out.print(" Thomas Watson, IBM, “); System.out.println("1943.“); }

Background For general problem solving we need more capabilities –The ability to control which statements are executed –The ability to control how often a statement is executed We will concentrate first on controlling which statements are executed Java provides the if and switch conditional constructs to control whether a statement list is executed –The if constructs use logical expressions to determine their course of action Examination begins with logical expressions

Logical expressions The branch of mathematics dealing with logical expressions is Boolean algebra –Developed by the British mathematician George Boole

Logical expressions A logical expression has either the value logical true or logical false –Some expressions whose values are logical true The year 2004 is a leap year A meter equals 100 centimeters –Some expressions whose values are logical false A triangle has four sides The area of square is always equal to twice its perimeter

Logical expressions There are three primary logical operators for manipulating logical values –Logical and –Logical or –Logical not The operators work as most of us would expect

Truth tables We use truth tables to give formal specifications of the operators –“It works as most of us would expect” allows for ambiguity of interpretation Jim is smiling or Patty is smiling –Can both Jim and Patty be smiling? Truth tables –Lists all combinations of operand values and the result of the operation for each combination pq p and q False FalseFalse False TrueFalse True FalseFalse True TrueTrue

Or and not truth tables pq p or q False FalseFalse False TrueTrue True FalseTrue True TrueTrue pnot p False True True False

Boolean algebra Can create complex logical expressions by combining simple logical expressions –not (p and q) pq p and q not (p and q) False FalseFalseTrue False TrueFalseTrue True FalseFalseTrue True TrueTrueFalse

DeMorgan’s laws not (p and q) equals (not p) or (not q) not ( not p) or pq p and q (p and q) ( not p) (not q) ( not q) False False False True True True True False True False True True False True True False False True False True True True True True False False False False

DeMorgan’s laws not (p or q) equals (not p) and (not q) ( not p) and pq p or q not (p or q) ( not p) (not q) ( not q) False False False True True True True False True True False True False False True False True False False True False True True True False False False False

A boolean type Java has the logical type boolean Type boolean has two literal constants –true –false Operators –The and operator is && –The or operator is || –The not operator is !

Defining boolean variables Local boolean variables are uninitialized by default boolean isWhitespace; boolean receivedAcknowledgement; boolean haveFoundMissingLink;

Defining boolean variables Local boolean variables with initialization boolean canProceed = true; boolean preferCyan = false; boolean completedSecretMission = true;

Other operators Equality operators == and != –Operator == Evaluates to true if the operands have the same value; otherwise, evaluates to false –Operator != Evaluates to true if the operands have different values; otherwise, evaluates to false –The operators work with all types of values

Evaluating boolean expressions Suppose boolean p = true; boolean q = false; boolean r = true; boolean s = false; What is the value of p p && s !s p == q q q != r p && r r == s q || sq != s

Evaluating boolean expressions Suppose int i = 1; int j = 2; int k = 2; char c = '#'; char d = '%'; char e = '#'; What is the value of j == k i != k i == j j != k c == e d != e c == d c != e

Take care with floating-point values Consider double a = 1; double b = ; double c = ; Two false Java expressions! a == bb != c Two true Java expressions! a != bb == c Problem lies with the finite precision of the floating-point types Instead test for closeness with floating-point values Math.abs(a – b) < Small number

Ordering operators Java provides ordering operators for the primitive types –Four ordering operators,, = –They correspond to mathematical operators of, >, and > Together the equality and ordering operators are known as the relational operators False is less than true

Evaluation boolean expressions Suppose int i = 1; int j = 2; int k = 2; What is the value of i < j j < k i <= k j >= k i >= k

Evaluation boolean expressions Suppose char c = '2'; char d = '3'; char e = '2'; What is the value of c < d c < e c <= e d >= e c >= e

Operator precedence revisited Highest to lowest –Parentheses –Unary operators –Multiplicative operators –Additive operators –Relational ordering –Relational equality –Logical and –Logical or –Assignment