Lesson 39: Conditionals #3 (W11D4)

Similar documents
Lesson 38: Conditionals #2 (W11D3)

Lesson 36: for() Loops (W11D1)

Lesson 24: Recursive Algorithms #1 (W07D3)

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

Lesson 26: ArrayList (W08D1)

Control Structures: The IF statement!

Decision Structures. Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Flow of Control Branching 2. Cheng, Wei COMP May 19, Title

Text Input and Conditionals

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

Boolean evaluation and if statements. Making decisions in programs

Checking Multiple Conditions

Decision Structures. Lesson 03 MIT 11053, Fundamentals of Programming

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Boolean Expressions. Is Equal and Is Not Equal

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

Boolean Expressions. Is Equal and Is Not Equal

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Chapter 4 The If Then Statement

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

Lesson 04: Our First Java Program (W01D4

5. Selection: If and Switch Controls

Advanced if/else & Cumulative Sum

Computer Science & Engineering 150A Problem Solving Using Computers

Maltepe University Computer Engineering Department. Algorithms and Programming. Chapter 4: Conditionals - If statement - Switch statement

Python Unit

2.3 Flow Control. Flow-Of-Control. If-Else: Leap Year. If-Else

CS 112 Introduction to Programming

CS 112 Introduction to Programming

Building Java Programs

APCS Semester #1 Final Exam Practice Problems

Conditions, logical expressions, and selection. Introduction to control structures

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0. L J Howell UX Software Ver. 1.0

LAB 5: SELECTION STATEMENTS

Computational Expression

SELECTION. (Chapter 2)

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

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 4: Control Structures I (Selection)

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

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 5. Repetition in Programs. Notes. Notes. Notes. Lecture 05 - Loops

Chapter 2 Control in Programming. lecture 2 1

Selection Statement ( if )

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

Building Java Programs

Announcements. Homework 0: using cin with 10/3 is NOT the same as (directly)

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

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

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

bool bool - either true or false

Chapter. Solving Problems that Require Decisions. Objectives: At the end of the chapter students should be able to:

CS1 Lecture 5 Jan. 26, 2018

Pull Lecture Materials and Open PollEv. Poll Everywhere: pollev.com/comp110. Lecture 12. else-if and while loops. Once in a while

conditional statements

Control Structures in Java if-else and switch

Loop structures and booleans

Midterm Review Topics Java Basics and Structure Variables Branching Loops Methods Arrays. Midterm Review Kage Weiss Lab 001 TA, SPR 17

CONDITIONAL EXECUTION

! Sequence of statements that are actually executed in a program. ! Conditionals and loops: enable us to choreograph control flow.

Announcements. HW0 is posted on schedule, due next Friday at 9pm (pretty easy)

CSc 372. Comparative Programming Languages. 36 : Scheme Conditional Expressions. Department of Computer Science University of Arizona

SECONDARY SCHOOL, L-IMRIEĦEL HALF YEARLY EXAMINATIONS 2016/2017

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

AP Computer Science Homework Set 2 Class Design

Code Generation. CS 1622: Code Generation & Register Allocation. Arrays. Why is Code Generation Hard? Multidimensional Arrays. Array Element Address

CIS220 In Class/Lab 1: Due Sunday night at midnight. Submit all files through Canvas (25 pts)

UNIT-4 Black Box & White Box Testing

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Modern Methods in Software Engineering. Testing.

CMSC 201 Spring 2018

Computer Programming. Basic Control Flow - Decisions. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

Lec 3. Compilers, Debugging, Hello World, and Variables

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme

Control Structures in Java if-else and switch

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

UNIT-4 Black Box & White Box Testing

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

CSCE150A. Introduction. While Loop. Compound Assignment. For Loop. Loop Design. Nested Loops. Do-While Loop. Programming Tips CSCE150A.

COMP250: Introduction to algorithms The set-intersection problem

Lesson 7 Part 2 Flags

CSc 520 Principles of Programming Languages

Chapter 5 Selection Statements. Mr. Dave Clausen La Cañada High School

BRANCHING if-else statements

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Making Decisions In Python

COMS 469: Interactive Media II

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent

Lesson 1. Introduction to Programming OBJECTIVES

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

Chapter 4: Programming with MATLAB

Topic 11 Scanner object, conditional execution

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17)

Tutorials. Inf1-OP. Learning Outcomes for this week. A Foundation for Programming. Conditionals and Loops 1

The design recipe. Readings: HtDP, sections 1-5. (ordering of topics is different in lectures, different examples will be used)

Exercise 3 Logical Operators, Branching, Loops. Informatik I für Mathematiker und Physiker (HS 2015) Yeara Kozlov

Lecture. Loops && Booleans. Richard E Sarkis CSC 161: The Art of Programming

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

Boolean Logic & Branching Lab Conditional Tests

Transcription:

Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1 / 29

Do Now In order to qualify for a $50k loan, the following conditions must be met: Your annual income must be at least $26,000 Your age must be between 18 and 29 (inclusive) Write a boolean that would serve as an appropriate condition for the if() statement below. int income, age; //assume these values are set... Loan myloan = new Loan(50000); if( ) { myloan.grant(); else { myloan.deny(); 2 / 29

Aim Students will learn about Short-Circuit Evaluation, if()/else if, and work on programming exercises. 3 / 29

Short-Circuit Evaluation Consider this scenario: boolean a = false, b = true, c = true; if ( a && ( b c ) ) { // statements to run if true... How does Java evaluate the condition? 4 / 29

Short-Circuit Evaluation Consider this scenario: boolean a = false, b = true, c = true; if ( a && ( b c ) ) { // statements to run if true... How does Java evaluate the condition? In an and statement, if the left side is false, there s no way the overall truth value could possibly be true, so why bother to spend CPU cycles to evaluate the rest? 5 / 29

Short-Circuit Evaluation Consider this scenario: boolean a = false, b = true, c = true; if ( a && ( b c ) ) { // statements to run if true... How does Java evaluate the condition? In an and statement, if the left side is false, there s no way the overall truth value could possibly be true, so why bother to spend CPU cycles to evaluate the rest? This shortcut is called Short-Circuit Evaluation. 6 / 29

Short-Circuit Evaluation for or How might the principle of Short-Circuit Evaluation be applied to or? 7 / 29

Short-Circuit Evaluation for or How might the principle of Short-Circuit Evaluation be applied to or? Ex: You have to be at least 25 or have a military ID to rent a car. 8 / 29

Short-Circuit Evaluation for or How might the principle of Short-Circuit Evaluation be applied to or? Ex: You have to be at least 25 or have a military ID to rent a car. int age = 19; boolean inmilitary = true; if ( inmilitary age >= 25 ) {... 9 / 29

Short-Circuit Evaluation for or How might the principle of Short-Circuit Evaluation be applied to or? Ex: You have to be at least 25 or have a military ID to rent a car. int age = 19; boolean inmilitary = true; if ( inmilitary age >= 25 ) {... In an or statement, if the left side is true, then there s no way the entire or statement won t be true so don t bother evaluating the rest. 10 / 29

Short-Circuit Evaluation for Safety! Let s say you have this condition to evaluate: a/b > 6 11 / 29

Short-Circuit Evaluation for Safety! Let s say you have this condition to evaluate: a/b > 6 What is the potential problem with evaluating this boolean expression? (Consider the values that the variables might have!) 12 / 29

Short-Circuit Evaluation for Safety! Let s say you have this condition to evaluate: a/b > 6 What is the potential problem with evaluating this boolean expression? (Consider the values that the variables might have!) b must not be zero or else there will be a divide by zero exception! 13 / 29

Short-Circuit Evaluation for Safety! Let s say you have this condition to evaluate: a/b > 6 What is the potential problem with evaluating this boolean expression? (Consider the values that the variables might have!) b must not be zero or else there will be a divide by zero exception! Short-circuit evaluation prevents that case: b!= 0 && a/b > 6 14 / 29

if()-wrapped if() What if you need to branch in more than two directions based on a condition? 1 $3 return/payout on $2 bet 15 / 29

if()-wrapped if() What if you need to branch in more than two directions based on a condition? Example: The game of Blackjack You are dealt two cards. Based on the sum of their values, here are possible actions: sum action 21 (e.g., A & K ) Dealer pays 3:2 1 10 (e.g., 3 & 7 ) Double down <10 (e.g., 5 & 2 ) Hit 1 $3 return/payout on $2 bet 16 / 29

if()-wrapped if() How one might implement the Blackjack logic from the last slide: 2 int cardoneval, cardtwoval; // call method to deal cards, set card values int sum = cardoneval + cardtwoval; if ( sum == 21 ) { dealer.payout(3,2); //winner! 2 cases grossly oversimplified! 17 / 29

if()-wrapped if() How one might implement the Blackjack logic from the last slide: int cardoneval, cardtwoval; // call method to deal cards, set card values int sum = cardoneval + cardtwoval; if ( sum == 21 ) { dealer.payout(3,2); //winner! else { //2 more cases: // * sum is 10, // * sum is less 18 / 29

if()-wrapped if() How one might implement the Blackjack logic from the last slide: int cardoneval, cardtwoval; // call method to deal cards, set card values int sum = cardoneval + cardtwoval; if ( sum == 21 ) { dealer.payout(3,2); //winner! else { if ( sum == 10 ) { player.doubledown(); dealer.dealcard(); // * sum is less 19 / 29

if()-wrapped if() How one might implement the Blackjack logic from the last slide: int cardoneval, cardtwoval; // call method to deal cards, set card values int sum = cardoneval + cardtwoval; if ( sum == 21 ) { dealer.payout(3,2); //winner! else { if ( sum == 10 ) { player.doubledown(); dealer.dealcard(); else { dealer.dealcard(); 20 / 29

if()-wrapped if() if ( sum == 21 ) { dealer.payout(3,2); //winner! else { if ( sum == 10 ) { player.doubledown(); dealer.dealcard(); else { dealer.dealcard(); What we have here are nested if() statements 21 / 29

if()-wrapped if() if ( sum == 21 ) { dealer.payout(3,2); //winner! else { if ( sum == 10 ) { player.doubledown(); dealer.dealcard(); else { dealer.dealcard(); What we have here are nested if() statements Such a construct is often hard to understand and debug 22 / 29

if()-wrapped if() if ( sum == 21 ) { dealer.payout(3,2); //winner! else { if ( sum == 10 ) { player.doubledown(); dealer.dealcard(); else { dealer.dealcard(); What we have here are nested if() statements Such a construct is often hard to understand and debug Better way: else if() 23 / 29

else if() Here s the same logic using else if(): if ( sum == 21 ) { dealer.payout(3,2); //winner! 24 / 29

else if() Here s the same logic using else if(): if ( sum == 21 ) { dealer.payout(3,2); //winner! else if ( sum == 10 ) { player.doubledown(); dealer.dealcard(); 25 / 29

else if() Here s the same logic using else if(): if ( sum == 21 ) { dealer.payout(3,2); //winner! else if ( sum == 10 ) { player.doubledown(); dealer.dealcard(); else { //all other cases dealer.dealcard(); 26 / 29

else if() if ( sum == 21 ) { dealer.payout(3,2); //winner! else if ( sum == 10 ) { player.doubledown(); dealer.dealcard(); else { //all other cases dealer.dealcard(); Indentation makes the code more readable, easier to follow You can have as many else if() statements as you need to cover all possible outcomes for which you need different behaviors You ll learn switch/case/break soon, which is essentially a special form of what you see above 27 / 29

Programming Exercises You can find the PDF of Ch. 6 here. Either alone or with a partner, write classes that include these methods, and place them in a new project called Lesson39. Develop isleapyear() as per Ch. 6, #13. Make sure you understand the requirements for a leap year by reading the description carefully! Develop findbestfit() as per Ch. 6, #19. A tester class is available here. 28 / 29

HW Finish 3 of PS #6 by next class. Come with questions about book problems you re unable to figure out! 29 / 29