Type boolean boolean: A logical type whose values are true and false.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Selection Control Structures Chapter 5: Selection Asserting Java © Rick Mercer.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-3: Boolean Logic reading: 5.2 self-check: # exercises: #12 videos:
Building Java Programs
Building Java Programs
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 4: Conditional Execution.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Conditionals (Cont’d). 2 Nested if/else question Formula for body mass index (BMI): Write a program that produces output like the following: This program.
Computer Science 210 Computer Organization Introduction to Boolean Algebra.
1 Building Java Programs Chapter 5 Lecture 5-3: Boolean Logic and Assertions reading: 5.3 – 5.5.
Chapter 51 Logical Operators Used with Boolean expressions Not – makes a False expression True and vice versa And – will yield a True if and only if both.
Do/while Loops Another Type of Indefinite Loop. 2 The do/while loop do/while loop: Performs its test at the end of each repetition. –Guarantees that the.
Boolean Values The true story ;=P. Expressions.
Boolean Values The true story ;=P Thanks to Margaret Reid-Miller for her original ideas at Carnegie Mellon’s CSE.
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
Decisions in Python Boolean functions. A Boolean function This is a function which returns a bool result (True or False). The function can certainly work.
Building Java Programs Chapter 4 Conditional Execution Copyright (c) Pearson All rights reserved.
Laws of Boolean Algebra Commutative Law Associative Law Distributive Law Identity Law De Morgan's Theorem.
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises:
Copyright 2009 by Pearson Education Building Java Programs Chapter 5 Lecture 5-3: Boolean Logic reading: 5.2 self-check: # exercises: #12 videos:
1 Building Java Programs Chapter 5 Lecture 12: Random, boolean Logic reading: 4.4, 5.1, 5.3, 5.6 (Slides adapted from Stuart Reges, Hélène Martin, and.
Copyright 2008 by Pearson Education 1 The if statement Executes a block of statements only if a test is true if ( test ) { statement ;... statement ; }
CSc 110, Autumn 2017 Lecture 13: Cumulative Sum and Boolean Logic
Building Java Programs
Topic 16 boolean logic - George Boole
Making Choices with if Statements
Building Java Programs
Logic Gates.
Building Java Programs Chapter 4
CSc 110, Spring 2017 Lecture 14: Boolean Logic and Midterm Review

Factoring if/else code
Building Java Programs
Type boolean boolean: A logical type whose values are true and false.
Lecture 4: Conditionals
Building Java Programs
Building Java Programs
CSc 110, Autumn 2016 Lecture 14: Boolean Logic
Building Java Programs
And now for something completely different . . .
Building Java Programs
Executes a block of statements only if a test is true
CSc 110, Spring 2017 Lecture 13: Random numbers and Boolean Logic
Types, Truth, and Expressions (Part 2)
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2018 Lecture 14: Booleans and Strings
Logic Gates.
CSc 110, Autumn 2016 Lecture 10: Advanced if/else; Cumulative sum
Building Java Programs
Building Java Programs
CSc 110, Spring 2018 Lecture 13: Cumulative Sum and Boolean Logic
De Morgan’s laws presentation
Lecture 8: The String Class and Boolean Zen
Building Java Programs
Building Java Programs
Building Java Programs
Lecture 6: Conditionals AP Computer Science Principles
Expressions.
Building Java Programs
Building Java Programs
Topic 16 boolean logic - George Boole
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 5 Lecture 5-3: Boolean Logic reading: 5.3, 5.4
Building Java Programs Chapter 4
Factoring if/else code
Building Java Programs
Presentation transcript:

Type boolean boolean: A logical type whose values are true and false. A logical test is actually a boolean expression. Like other types, it is legal to: create a boolean variable pass a boolean value as a parameter return a boolean value from methods call a method that returns a boolean and use it as a test boolean minor = age < 21; boolean isProf = name.contains("Prof"); boolean lovesCSE = true;

Logical operators Tests can be combined using logical operators: "Truth tables" for each, used with logical values p and q: Operator Description Example Result && and (2 == 3) && (-1 < 5) false || or (2 == 3) || (-1 < 5) true ! not !(2 == 3) p q p && q p || q true false p !p true false

Evaluating logical expressions Relational operators have lower precedence than math; logical operators have lower precedence than relational operators 5 * 7 >= 3 + 5 * (7 – 1) && 7 <= 11 5 * 7 >= 3 + 5 * 6 && 7 <= 11 35 >= 3 + 30 && 7 <= 11 35 >= 33 && 7 <= 11 true && true true Relational operators cannot be "chained" as in algebra 2 <= x <= 10 true <= 10 (assume that x is 15) Error! Instead, combine multiple tests with && or || 2 <= x && x <= 10 true && false false

"Boolean Zen", part 1 Students new to boolean often test if a result is true: if (isPrime(57) == true) { // bad ... } But this is unnecessary and redundant. Preferred: if (isPrime(57)) { // good A similar pattern can be used for a false test: if (isPrime(57) == false) { // bad if (!isPrime(57)) { // good

"Boolean Zen" template Replace with public static boolean name(parameters) { if (test) { return true; } else { return false; } with return test;

Master boolean zen, you will

De Morgan's Law De Morgan's Law: Rules used to negate boolean tests. Useful when you want the opposite of an existing test. Example: Original Expression Negated Expression Alternative a && b !a || !b !(a && b) a || b !a && !b !(a || b) Original Code Negated Code if (x == 7 && y > 3) { ... } if (x != 7 || y <= 3) {

Lotto Write a method seven that accepts a Random parameter and uses it to draw up to ten lotto numbers from 1-30. If any of the numbers is a lucky 7, the method should stop and return true. If none of the ten are 7 it should return false. The method should print each number as it is drawn. 15 29 18 29 11 3 30 17 19 22 (first call) 29 5 29 4 7 (second call)