Lesson 38: Conditionals #2 (W11D3)

Similar documents
Lesson 36: for() Loops (W11D1)

Lesson 39: Conditionals #3 (W11D4)

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

JAVA OPERATORS GENERAL

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Chapter 3: Operators, Expressions and Type Conversion

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

RUBY OPERATORS. Ruby Arithmetic Operators: Ruby Comparison Operators:

Lesson 10: Quiz #1 and Getting User Input (W03D2)

Operators. Java operators are classified into three categories:

Logic Design: Part 2

QUIZ: What value is stored in a after this

CMSC201 Computer Science I for Majors

Operators & Expressions

Lesson 26: ArrayList (W08D1)

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 1 IDL Operators

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

Chapter 4: Basic C Operators

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

COMP Primitive and Class Types. Yi Hong May 14, 2015

Flow Control. So Far: Writing simple statements that get executed one after another.

Chapter 6 Primitive types

AP Computer Science A

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

BRANCHING if-else statements

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Prof. Navrati Saxena TA: Rochak Sachan

COMP combinational logic 1 Jan. 18, 2016

CS 117 Fall Compound boolean expressions. Control Statements, Part 2. Using boolean operators. Boolean operators

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Lesson 24: Recursive Algorithms #1 (W07D3)

Lecture 3 Operators MIT AITI

Arithmetic and Bitwise Operations on Binary Data

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

Operators in java Operator operands.

Arithmetic Operators. Portability: Printing Numbers

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

Lesson 7 Part 2 Flags

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

If Control Construct

Selections. CSE 114, Computer Science 1 Stony Brook University

CS 31: Intro to Systems Binary Arithmetic. Kevin Webb Swarthmore College January 26, 2016

ECEN 468 Advanced Logic Design

Selenium Class 9 - Java Operators

Operators in C. Staff Incharge: S.Sasirekha

COMP-202: Foundations of Programming. Lecture 5: More About Methods and Data Types Jackie Cheung, Winter 2016

Expressions & Assignment Statements

ENGI Introduction to Computer Programming M A Y 2 8, R E Z A S H A H I D I

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

5. Selection: If and Switch Controls

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

CIS 110: Introduction to Computer Programming

Control Structures in Java if-else and switch

CS Introduction to Data Structures How to Parse Arithmetic Expressions

Lecture 9. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 1

SMS 3515: Scientific Computing. Sem /2015

Department of Computer Science

ISA 563 : Fundamentals of Systems Programming

Expressions and Assignment Statements

CS Programming I: Branches

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Chapter 7. Expressions and Assignment Statements ISBN

Advanced Algorithms and Computational Models (module A)

Units 0 to 4 Groovy: Introduction upto Arrays Revision Guide

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following g roups:

Programming for Engineers Iteration

Computational Expression

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Comparing Data. Comparing Floating Point Values. Comparing Float Values. CS257 Computer Science I Kevin Sahr, PhD

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

CT 229. Java Syntax 26/09/2006 CT229

Boolean Algebra Boolean Algebra

Java enum, casts, and others (Select portions of Chapters 4 & 5)

STUDENT LESSON A14 Boolean Algebra and Loop Boundaries

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Relational and Logical Statements

More Programming Constructs -- Introduction

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. and Java. Chapter 2 Primitive Data Types and Operations

Chapter 7. Expressions and Assignment Statements

APCS Semester #1 Final Exam Practice Problems

CS1150 Principles of Computer Science Boolean, Selection Statements

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

L05 - Negating Statements

Lesson 12: OOP #2, Accessor Methods (W03D4)

C/C++ Programming Lecture 7 Name:

4. Number Representations

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington

Logical and Bitwise Expressions

Lecture Notes on Binary Decision Diagrams

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

Transcription:

Lesson 38: Conditionals #2 (W11D3) Balboa High School Michael Ferraro October 28, 2015 1 / 61

Do Now In Lesson38/DoNow.java, write a method called isdivisiblebyfour() that takes an int returns the boolean values true or false indicating whether the input value is evenly divisible by 4. Once finished, have main() call isdivisiblebyfour() using the following for() loop: for(int i = 0 ; i <= 100 ; i++) { if ( isdivisiblebyfour(i) ) { System.out.println(i+" is divisible by 4"); } } 2 / 61

Aim Students will learn how to perform basic logic operations (and, or, & not) in Java and how Java s order of operations works with respect to relational (e.g., <, <=, &!=) and logical operators. 3 / 61

Basic Logic: && (and) How does and work in logic? 4 / 61

Basic Logic: && (and) How does and work in logic? I work really hard and I get good grades. (p && q) 5 / 61

Basic Logic: && (and) How does and work in logic? I work really hard and I get good grades. (p && q) Evaluate the value of the boolean expression ( p && q ) when... 6 / 61

Basic Logic: && (and) How does and work in logic? I work really hard and I get good grades. (p && q) Evaluate the value of the boolean expression ( p && q ) when... 1 p = true, q = true 7 / 61

Basic Logic: && (and) How does and work in logic? I work really hard and I get good grades. (p && q) Evaluate the value of the boolean expression ( p && q ) when... 1 p = true, q = true 2 p = true, q = false 8 / 61

Basic Logic: && (and) How does and work in logic? I work really hard and I get good grades. (p && q) Evaluate the value of the boolean expression ( p && q ) when... 1 p = true, q = true 2 p = true, q = false 3 p = false, q = true 9 / 61

Basic Logic: && (and) How does and work in logic? I work really hard and I get good grades. (p && q) Evaluate the value of the boolean expression ( p && q ) when... 1 p = true, q = true 2 p = true, q = false 3 p = false, q = true 4 p = false, q = false 10 / 61

Basic Logic: && (and) How does and work in logic? I work really hard and I get good grades. (p && q) Evaluate the value of the boolean expression ( p && q ) when... 1 p = true, q = true 2 p = true, q = false 3 p = false, q = true 4 p = false, q = false && is true when both operands (inputs) are true. 11 / 61

Basic Logic: (or) How does or work in logic? 1 or is also true when both operands are true when using inclusive or 12 / 61

Basic Logic: (or) How does or work in logic? I m a slacker or I get good grades. (p q) 1 or is also true when both operands are true when using inclusive or 13 / 61

Basic Logic: (or) How does or work in logic? I m a slacker or I get good grades. (p q) Evaluate the value of the expression ( p q ) when... 1 or is also true when both operands are true when using inclusive or 14 / 61

Basic Logic: (or) How does or work in logic? I m a slacker or I get good grades. (p q) Evaluate the value of the expression ( p q ) when... 1 p = true, q = true 1 or is also true when both operands are true when using inclusive or 15 / 61

Basic Logic: (or) How does or work in logic? I m a slacker or I get good grades. (p q) Evaluate the value of the expression ( p q ) when... 1 p = true, q = true 2 p = true, q = false 1 or is also true when both operands are true when using inclusive or 16 / 61

Basic Logic: (or) How does or work in logic? I m a slacker or I get good grades. (p q) Evaluate the value of the expression ( p q ) when... 1 p = true, q = true 2 p = true, q = false 3 p = false, q = true 1 or is also true when both operands are true when using inclusive or 17 / 61

Basic Logic: (or) How does or work in logic? I m a slacker or I get good grades. (p q) Evaluate the value of the expression ( p q ) when... 1 p = true, q = true 2 p = true, q = false 3 p = false, q = true 4 p = false, q = false 1 or is also true when both operands are true when using inclusive or 18 / 61

Basic Logic: (or) How does or work in logic? I m a slacker or I get good grades. (p q) Evaluate the value of the expression ( p q ) when... 1 p = true, q = true 2 p = true, q = false 3 p = false, q = true 4 p = false, q = false is true when either operand (input) is true. 1 1 or is also true when both operands are true when using inclusive or 19 / 61

Examples Predict the output of the following: boolean p = true; //I like to sing boolean q = false; //I like to dance if( p q ) { System.out.println("Perform for us!"); } else { System.out.println("Get off the stage!"); } 20 / 61

Examples Predict the output of the following: boolean p = false; boolean q = false; //I like to sing //I like to dance if( p q ) { System.out.println("Perform for us!"); } else { System.out.println("Get off the stage!"); } 21 / 61

Examples Predict the output of the following: boolean p = false; boolean q = true; //I like to sing //I like to dance if( p && q ) { System.out.println("Perform for us!"); } else { System.out.println("Get off the stage!"); } 22 / 61

Examples Predict the output of the following: boolean p = true; boolean q = true; //I like to sing //I like to dance if( p && q ) { System.out.println("Perform for us!"); } else { System.out.println("Get off the stage!"); } 23 / 61

Using and and or Write a boolean expression that evaluates to true when a number is between 0 and 9, inclusive... 24 / 61

Using and and or Write a boolean expression that evaluates to true when a number is between 0 and 9, inclusive... Restated: true when a number is 0 and 9 25 / 61

Using and and or Write a boolean expression that evaluates to true when a number is between 0 and 9, inclusive... Restated: true when a number is 0 and 9 Solution: For a number, n, ( n >= 0 ) && ( n <= 9 ) 26 / 61

Using and and or Write a boolean expression that evaluates to true when a number is between 0 and 9, inclusive... Restated: true when a number is 0 and 9 Solution: For a number, n, ( n >= 0 ) && ( n <= 9 ) As used in an if() statement: if ( ( n >= 0 ) && ( n <= 9 ) ) {... } 27 / 61

Using and and or Write a boolean expression that evaluates to true when a number is less than 5 or greater than 6, exclusive 2... 2 Excluding the values 5 and 6, i.e., the number is strictly less than 5 or strictly greater than 6, and equal to neither. 28 / 61

Using and and or Write a boolean expression that evaluates to true when a number is less than 5 or greater than 6, exclusive 2... Restated: true when a number is < 5 or > 6 2 Excluding the values 5 and 6, i.e., the number is strictly less than 5 or strictly greater than 6, and equal to neither. 29 / 61

Using and and or Write a boolean expression that evaluates to true when a number is less than 5 or greater than 6, exclusive 2... Restated: true when a number is < 5 or > 6 Solution: For a number, n, ( n < 5 ) ( n > 6 ) 2 Excluding the values 5 and 6, i.e., the number is strictly less than 5 or strictly greater than 6, and equal to neither. 30 / 61

Basic Logic:! (not)! is the not, or negation, operator It flips a boolean s truth value boolean isempty = true; //glass is empty if (! isempty ) { takesip(); } else { refillglass(); } 31 / 61

Basic Logic:! (not)! is the not, or negation, operator It flips a boolean s truth value boolean isempty = true; //glass is empty if (! isempty ) { takesip(); } else { refillglass(); }! is referred to as an unary operator 32 / 61

Basic Logic:! (not)! is the not, or negation, operator It flips a boolean s truth value boolean isempty = true; //glass is empty if (! isempty ) { takesip(); } else { refillglass(); }! is referred to as an unary operator unary means that it takes one operand the values whose truth value is to be flipped 33 / 61

Basic Logic:! (not)! is the not, or negation, operator It flips a boolean s truth value boolean isempty = true; //glass is empty if (! isempty ) { takesip(); } else { refillglass(); }! is referred to as an unary operator unary means that it takes one operand the values whose truth value is to be flipped cf. binary operators like +, -, *, /, %,, etc. 34 / 61

Mixing Logical Operators Determine the value of c. boolean a = true; boolean b = false; boolean c = ( a &&!b ); 35 / 61

Mixing Logical Operators Determine the value of c. boolean a = true; boolean b = true; boolean c = (!a!b ); 36 / 61

Mixing Logical Operators Are things any different now? boolean a = true; boolean b = true; //boolean c = (!a!b ); boolean c =!( a && b ); 37 / 61

Mixing Logical Operators Are things any different now? boolean a = true; boolean b = true; //boolean c = (!a!b ); boolean c =!( a && b ); We ll revisit this shortly... 38 / 61

Revisiting the Example Write a boolean expression that evaluates to true when a number is less than 5 or greater than 6, exclusive: ( n < 5 ) ( n > 6 ) 39 / 61

Revisiting the Example Write a boolean expression that evaluates to true when a number is less than 5 or greater than 6, exclusive: ( n < 5 ) ( n > 6 ) In other words, true when n is < > 2 3 4 5 6 7 8 n 40 / 61

Revisiting the Example Write a boolean expression that evaluates to true when a number is less than 5 or greater than 6, exclusive: ( n < 5 ) ( n > 6 ) In other words, true when n is < > 2 3 4 5 6 7 8 n Isn t it reasonable to say the following instead? It is not true that n is between 5 and 6, inclusive! ( ( n >= 5 ) && ( n <= 6 ) ) 41 / 61

Revisiting the Example Write a boolean expression that evaluates to true when a number is less than 5 or greater than 6, exclusive: ( n < 5 ) ( n > 6 ) In other words, true when n is < > 2 3 4 5 6 7 8 n Isn t it reasonable to say the following instead? It is not true that n is between 5 and 6, inclusive! ( ( n >= 5 ) && ( n <= 6 ) ) both boolean expressions are logically equivalent 42 / 61

DeMorgan s Rules The rules are fully described in PS #6, 3.1.2 43 / 61

DeMorgan s Rules The rules are fully described in PS #6, 3.1.2 Summary: Distribute the negation, swap the and/or 44 / 61

DeMorgan s Rules The rules are fully described in PS #6, 3.1.2 Summary: Distribute the negation, swap the and/or! ( A B )!A &&!B 45 / 61

DeMorgan s Rules The rules are fully described in PS #6, 3.1.2 Summary: Distribute the negation, swap the and/or! ( A B )!A &&!B! ( A && B )!A!B 46 / 61

DeMorgan s Rules The rules are fully described in PS #6, 3.1.2 Summary: Distribute the negation, swap the and/or! ( A B )!A &&!B! ( A && B )!A!B Example: 47 / 61

DeMorgan s Rules The rules are fully described in PS #6, 3.1.2 Summary: Distribute the negation, swap the and/or! ( A B )!A &&!B! ( A && B )!A!B Example:!( a && b ) 48 / 61

DeMorgan s Rules The rules are fully described in PS #6, 3.1.2 Summary: Distribute the negation, swap the and/or! ( A B )!A &&!B! ( A && B )!A!B Example:!( a && b )!a!b 49 / 61

Second DeMorgan s Example!( ( n >= 5 ) && ( n <= 6 ) ) 50 / 61

Second DeMorgan s Example!( ( n >= 5 ) && ( n <= 6 ) )!( n >= 5 )!( n <= 6 ) 51 / 61

Second DeMorgan s Example!( ( n >= 5 ) && ( n <= 6 ) )!( n >= 5 )!( n <= 6 )!( n >= 5 )!( n <= 6 ) 52 / 61

Second DeMorgan s Example!( ( n >= 5 ) && ( n <= 6 ) )!( n >= 5 )!( n <= 6 )!( n >= 5 )!( n <= 6 ) ( n < 5 ) ( n > 6 ) 53 / 61

Java Order of Operations When do we need ()s? When don t we? 54 / 61

Java Order of Operations Priority Operator Type Operators highest Pemdas () Unary!,, casts, ++, pemdas pemdas, (also modulo, %) pemdas +, Relational Operators <, <=, >, >=, ==,! = Logical and && lowest Logical or x y 55 / 61

Java Order of Operations Let s see if we can simplify some earlier examples... 56 / 61

Java Order of Operations Let s see if we can simplify some earlier examples... ( n >= 0 ) && ( n <= 9 ) are ()s needed? 57 / 61

Java Order of Operations Let s see if we can simplify some earlier examples... ( n >= 0 ) && ( n <= 9 ) are ()s needed? n >= 0 && n <= 9 58 / 61

Java Order of Operations Let s see if we can simplify some earlier examples... ( n >= 0 ) && ( n <= 9 ) are ()s needed? n >= 0 && n <= 9 ( n < 5 ) ( n > 6 ) are ()s needed? 59 / 61

Java Order of Operations Let s see if we can simplify some earlier examples... ( n >= 0 ) && ( n <= 9 ) are ()s needed? n >= 0 && n <= 9 ( n < 5 ) ( n > 6 ) are ()s needed? n < 5 n > 6 60 / 61

HW Complete 1-3.2 of PS #6, inclusive The reading for Litvin Ch. 6 is available here. 61 / 61