CIS 110: Introduction to Computer Programming

Slides:



Advertisements
Similar presentations
Selection Control Structures Chapter 5: Selection Asserting Java © Rick Mercer.
Advertisements

Logic & program control part 2: Simple selection structures.
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
From Chapter 4 Formal Specification using Z David Lightfoot
Copyright 2008 by Pearson Education 1 Midterm announcements Next week on Friday May 8 Must bring an ID Open book, open notes, closed electronics Must attend.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Computer Science 101 The Boolean System. George Boole British mathematician ( ) Boolean algebra –Logic –Set theory –Circuits –Conditions in if.
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.
CS 101E – Exam 2 Review Spring 2007 Michele Co. Announcements Review Session Tonight, 7/7:30 p.m., OLS 009 Will be announced via In-class Exam Wednesday.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
1 Logical Assertions. 2 Logical assertions assertion: A statement that is either true or false. Examples: –Java was created in –The sky is purple.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
1 CSE 142 Midterm Review Problems These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or modified.
Mathematics for Comter I Lecture 2: Logic (1) Basic definitions Logical operators Translating English sentences.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
Copyright 2009 by Pearson Education Building Java Programs Chapter 5 Lecture 5-3: Boolean Logic reading: 5.2 self-check: # exercises: #12 videos:
TOPIC 8 MORE ON WHILE LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Copyright © Curt Hill The C++ IF Statement More important details More fun Part 3.
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
CS0007: Introduction to Computer Programming
CompSci 230 S Programming Techniques
CprE 185: Intro to Problem Solving (using C)
Computer Science 210 Computer Organization
CS Chapter 3 (3A and ) Part 1 of 8
Propositional Equivalence
Brent M. Dingle Texas A&M University Chapter 6, Sections 1 and 2
Section 7.1 Logical Operators
More important details More fun Part 3
Control Structures.
Lecture 3- Decision Structures
CS Chapter 3 (3A and ) Part 1 of 8
Midterm Review Problems
Expressions and Control Flow in JavaScript
Type boolean boolean: A logical type whose values are true and false.
Building Java Programs
Building Java Programs
Building Java Programs
Dr. Clincy Professor of CS
Propositional Calculus: Boolean Algebra and Simplification
Information Technology Department
Georgia Institute of Technology
Building Java Programs
Logical assertions assertion: A statement that is either true or false. Examples: Java was created in The sky is purple. 23 is a prime number. 10.
Dr. Clincy Professor of CS
Building Java Programs
Building Java Programs
Building Java Programs
Control Structure Chapter 3.
Building Java Programs
Lab Instructors will overview the MSP430
Building Java Programs
CIS 110: Introduction to Computer Programming
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Building Java Programs
Building Java Programs
Building Java Programs
Indefinite loop variations
Outline Software Development Activities
CprE 185: Intro to Problem Solving (using C)
CSS161: Fundamentals of Computing
Controlling Program Flow
Control Structure.
CIS 110: Introduction to Computer Programming
Boolean Expressions September 1, 2019 ICS102: The course.
The boolean type and boolean operators
CIS 110: Introduction to computer programming
Presentation transcript:

CIS 110: Introduction to Computer Programming Lecture 14 Booleans and Program Assertions (§ 5.3-5.5) 11/27/2018 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Booleans 11/27/2018 CIS 110 (11fa) - University of Pennsylvania

All hail the mighty boolean Booleans are primitives that have two possible values: true or false boolean bTrue = true; boolean bFalse = false; Whenever we needed a guard or test, we really needed a boolean value. if (/* boolean */) { } for (int i = 0; /* boolean! */; i++) { } while (/* boolean! */) { } 11/27/2018 CIS 110 (11fa) - University of Pennsylvania

Relational operators revisited Relational operators compare primitive values and return booleans! Booleans are themselves primitive, so we can compare them with != and ==. boolean b1 = 5 > 0; // true boolean b2 = 12.7 == 13.5; // false boolean b3 = 'c' != 'd'; // true boolean b4 = b1 == b2; // false 11/27/2018 CIS 110 (11fa) - University of Pennsylvania

Logical operators revisited Logical operators take booleans as arguments and produce a boolean value. && (logical AND): true iff both args are true || (logical OR): true iff at least one arg is true ! (logical NOT): opposite of the argument boolean b1 = (5 > 0) && ('c' == 'd'); // false boolean b2 = b1 || (5 <= 35); // true boolean b3 = !(3 < 0); // true // boolean b4 = x == 1 || 2 || 3 // bad code! 11/27/2018 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Truth Tables Since booleans only have two possible values, we can summarize the results of logical operators using truth tables. a && b | true | false | -------|-------|--------| true | true | false | false | false | false | a || b | true | false | -------|-------|--------| true | true | true | false | true | false | a | !a ------|------ true | false false | true 11/27/2018 CIS 110 (11fa) - University of Pennsylvania

Java operator precedence review | ! ++ -- + - | * / % | + - | < > <= >= | == != | && | || | = += -= *= /= %= &&= ||= | V (In order of decreasing precedence) e.g., these two are different: b1 && b2 || b3 && b4 b1 && (b2 || b3) && b4 11/27/2018 CIS 110 (11fa) - University of Pennsylvania

Short-circuiting && and || && and || will not evaluate their second argument if it is unnecessary to do so i.e., if the first argument is false (&&) or true (||). Necessary behavior to write some guards cleanly! Scanner in = new Scanner(System.in); String line = in.nextLine(); int pos = 0; while (pos < line.length() && line.charAt(pos) != 'X') { pos++; } String toX = line.substring(0, pos); System.out.println(toX); Without short-circuiting would cause an exception when pos == line.length()! 11/27/2018 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania DeMorgan's Laws Identities concerning logical ops and negation. !(b1 || b2) = !b1 && !b2 !(b1 && b2) = !b1 || !b2 Useful for simplifying and reasoning about boolean expressions. while (!(s.equals("yes") || s.equals("no"))) { ... } = while (!s.equals("yes") && !s.equals("no")) { ... } 11/27/2018 CIS 110 (11fa) - University of Pennsylvania

Boolean flags One use of boolean variables is a boolean flag. This will be true if we ever see "Pie". Scanner in = new Scanner(System.in); boolean seenPie = false; for (int i = 1; i <= 5; i++) { System.out.print("Enter word " + i + ": "); String line = in.nextLine(); if (line.equalsIgnoreCase("pie")) { seenPie = true; } if (seenPie == true) { System.out.println("Haha. You said pie."); } else { System.out.println("Good words!"); 11/27/2018 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Boolean Zen Boolean zen is realizing the simplicity of the boolean expressions. If seenPie and seenPie == true have the same values, why don't I replace...? if (seenPie == true) { // ... } seenPie | seenPie == true ------------------------- true | true false | false if (seenPie) { // ... } Much simpler! 11/27/2018 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Program Assertions 11/27/2018 CIS 110 (11fa) - University of Pennsylvania

Imperative programming Imperative programming: being able to mutate (change) state/variables. Mutation makes program reasoning hard! Need to keep track of both control flow and state. int x = 0; int y = 5; int z = 25; while (x + y < z) { System.out.printf("x = %d, y = %d, z = %d\n", x, y, z); x += y / 2; y += (int) (x * 1.5); z += x * 2; } System.out.printf("Final: x = %d, y = %d, z = %d\n", x, y, z); 11/27/2018 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Assertions An assertion is a claim that is true, false, or sometimes true and sometimes false. e.g., "2 + 2 = 4", "Cats bark", "The sky is blue" Programming assertions are such claims made about the state of a program. while (i > 0) { // ... } // Is i > 0 always/never/sometimes true here? 11/27/2018 CIS 110 (11fa) - University of Pennsylvania

Assertions implicitly made by programming constructs By design, certain language constructs enforce some assertions, e.g., if-statements. At point A, test is always true. if (test) { // point A } // point B At point B, test is sometimes true. 11/27/2018 CIS 110 (11fa) - University of Pennsylvania

Assertions and if-else test is always true. if (test) { // point A } else { // point B } // point C test is always false. test is sometimes true. 11/27/2018 CIS 110 (11fa) - University of Pennsylvania

More assertions and if-else test1 is always true; test2 is sometimes true. if (test1) { // point A } else if (test2) { // point B } else { // point C } // point D test1 is always false; test2 is always true. test1 is always false; test2 is always false. test1 is sometimes true; test2 is sometimes true. 11/27/2018 CIS 110 (11fa) - University of Pennsylvania

Assertions and while loops while(test) { // point A } // point B test is always true test is always false 11/27/2018 CIS 110 (11fa) - University of Pennsylvania

Assertions and for loops for (int i = 0; test; i++) { // Point A } // Point B test is always true test is always false 11/27/2018 CIS 110 (11fa) - University of Pennsylvania

Exercise that mental model In general, we must rely on our mental model of computation to reason about assertions. public static String repeat(String msg, int n) { String ret = ""; // Is ret.length() == 0 always/sometimes/never true? for (int i = 0; i < n; i++) { ret += msg; } // Is ret.length() == msg.length() * n // always/sometimes/never true? return ret; Always true! Always true! 11/27/2018 CIS 110 (11fa) - University of Pennsylvania

An extended example: mystery public static int mystery(int n) { int x = 0; // Point A if (n < 0) { return -1; } while (n != 0) { // Point B int d = n % 10; if (d % 2 == 1) { x += d; } // Point C n /= 10; // Point D return x; For each point, are the following always/sometimes/never true? 1) n < 0 2) x >= 0 3) d < 10 4) x < n (See AssertionProblem.java.) 11/27/2018 CIS 110 (11fa) - University of Pennsylvania