Lazy Evaluation Computer Science 3 Gerb Objective: Understand lazy evaluation in Java.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
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.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
Lecture 11. Today’s topic Conditional statement –Relational operators –if statement –if-else statement –If-elseif statement.
Thirteen conditional expressions: letting programs make “decisions”
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.
CSCI1402: Lecture 2 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Shorthand operators.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
CSC 204 Programming I Loop I The while statement.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
© 2006 Pearson Education 1 Obj: to use compound Boolean statements HW: p.184 True/False #1 – 6 (skip 3)  Do Now: 1.Test your “Charge Account Statement”
Logical OperatorstMyn1 Logical Operators Using logical operators, we can combine a series of comparisons into a single expression. if(letter>=‘A’ && letter
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Repetition Statements while and do while loops
LOOP & Type Conversion. do – while Loop In the while loop, the test expression is evaluated at the beginning of the loop. If the test condition is false.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CS 106 Introduction to Computer Science I 09 / 26 / 2007 Instructor: Michael Eckmann.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Operators in JAVA. Operator An operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators.
How to Test Methods Computer Science 3 Gerb Objective: Test methods properly.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
TestScore < 80 testScore * 2 >= < w / (h * h) x + y != 2 * (a + b) 2 * Math.PI * radius
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
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,
ICS102 Lecture 8 : Boolean Expressions King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Control I Expressions and Statements Lecture 9 – Control I, Spring CSE3302 Programming.
CompSci 230 S Programming Techniques
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Making Choices with if Statements
Section 7.1 Logical Operators
Introduction to Computer Science / Procedural – 67130
USING ECLIPSE TO CREATE HELLO WORLD
Conditional Loops.
Writing Methods.
Computing Adjusted Quiz Total Score
Logical Operators & Truth Tables.
Computers & Programming Languages
The Boolean (logical) data type boolean
Interfaces and Constructors
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises UTPA – Fall 2012 This set of slides is revised from.
Chapter 4 Topics: class declarations method declarations
class PrintOnetoTen { public static void main(String args[]) {
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Relational Operators.
Topics discussed in this section:
Names of variables, functions, classes
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Conditionals and Loops
LCC 6310 Computation as an Expressive Medium
Presentation transcript:

Lazy Evaluation Computer Science 3 Gerb Objective: Understand lazy evaluation in Java

Short-Circuit Evaluation If the first operand of an && is false, the expression is false no matter what the second is. –Pointless even to look at it –Therefore Java won’t look at the second operand of && if the first is false. Example: if (method1() && method2())... –If method1 returns false, method2 won’t even be called. –Important if method2 has side effects like printing, those side effects will not happen. This technique for evaluating && is called short- circuit evaluation or lazy evaluation.

Short-Circuit Evaluation of || If the first operand of || is true, the expression is true no matter what the second is. Therefore Java uses short-circuit evaluation for || as well. If the first operand of || is true, the second is never evaluated.

Why is Short Circuit Evaluation Important When a method does something other than returning a value, we call that a side effect Examples : –Printing something to console –Changing the value of an instance variable –Onscreen graphics If a method is not called, its side effects won’t happen

For Example: What happens if I answer true to the first question? class foo { public static boolean ask(String q){ System.out.println(q+“?”); return sc.nextBoolean(); } public static void main(String args){ if (ask(“Are you over 17”) || ask(“Did you take Driver Ed”)) System.out.println(“Take driving test”);} }

Will not even call ask a second time, so the Driver Ed question won’t be asked class foo { public static boolean ask(String q){ System.out.println(q+”?”); return sc.nextBoolean(); } public static void main(String args){ if (ask(“Are you over 17”) || ask(“Did you take Driver Ed”)) System.out.println(“Take driving test”);} } Are you over 17? true Take driving test

Summary && true if both operands are true, false otherwise. || false if both operands are false, true otherwise. ! true if single operand is false, false otherwise. Short-circuit, or lazy, evaluation –Second operand is only evaluated if necessary.