Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. if (amount <= balance) balance = balance – amount; else balance =

Slides:



Advertisements
Similar presentations
DECISIONS Chapter 5. The if Statement  Action based on a conditions  If the condition is true, the body of the statement is executed if (amount
Advertisements

C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13.
Math 130 Decisions II Wed, Sept 12, 2007 Notes: ref. C. Horstmann, Java Concepts.
Chapter 5 – Decisions Big Java by Cay Horstmann
Random Number Generation public class Hand { private int card1; private int card2; private int card3; private Random rand; public static final int NO_CARD.
Chapter 5 Decisions Goals To be able to implement decisions using if statements To be able to implement decisions using if statements To understand how.
Chapter 6 Horstmann Programs that make decisions: the IF command.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
Chapter 5  Decisions 1 Chapter 5 Decisions. Chapter 5  Decisions 2 Chapter Goals  To be able to implement decisions using if statements  To understand.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Computer Science A 3: 10/2. Logical Expressions Expressions that has a value which is either true or false. Logical expressions are essential in a program.
If Statement if (amount
Chapter 5 – Decisions Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
Chapter 6 Decisions. Chapter Goals To be able to implement decisions using if statements To understand how to group statements into blocks To learn how.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Chapter 5: Decisions. To be able to implement decisions using if statements To understand how to group statements into blocks To learn how to compare.
Chapter 6 - Decisions. Characters char char primitive data type primitive data type stores a single character stores a single character operations (same.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
COM S 207 IF Statement Instructor: Ying Cai Department of Computer Science Iowa State University
Chapter 5 Decisions. Assignments  Written exercises pp 217 – 220 R5.1, R5.3, R5.5, R5.6, R5.9 – R5.15, R5.17, R R5.1, R5.3, R5.5, R5.6, R5.9 –
Chapter Goals To implement decisions using if statements
Decisions CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (Chapter 5, Horstmann*
Decisions: Handling Infinite Cases CS 21a: Introduction to Computing I First Semester,
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. int: integers, no fractional part: 1, -4, 0 double : floating-point.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 5: Decisions 1 Chapter 5 Decisions.
Fall 2006Slides adapted from Java Concepts companion slides1 Decisions Advanced Programming ICOM 4015 Lecture 5 Reading: Java Concepts Chapter 6.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
1 Chapter 4: Basic Control Flow ► Chapter Goals  To be able to implement decisions using if statements  To understand statement blocks  To learn how.
If Statement if (amount
1 CS1001 Lecture Overview Java Programming Java Programming Arrays Arrays.
Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Chapter 3 Boolean Expressions Section 3.2 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 6 Decisions. Chapter Goals To be able to implement decisions using if statements To understand how to group statements into blocks To learn how.
Input Validation 10/09/13. Input Validation with if Statements You, the C++ programmer, doing Quality Assurance (by hand!) C++ for Everyone by Cay Horstmann.
Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Decisions Bush decision making.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Array length = maximum number of elements in array Usually, array is partially filled Need companion variable to keep track of current size Uniform naming.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
7-1 boolean Data Type George Boole ( ) boolean variables may have only two values, true or false You define boolean fields or boolean local variables.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] : double[] data = new double[10]; When array.
ICS102 Lecture 8 : Boolean Expressions King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A string is a sequence of characters Strings are objects of the String.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 5 - Decisions.
 Type Called bool  Bool has only two possible values: True and False.
Chapter Goals To implement decisions using if statements
Chapter 4 – Fundamental Data Types
Chapter 3 Edited by JJ Shepherd
Chapter 5/6 Decisions.
Objectives After studying this chapter, you should be able to:
Control Structure Chapter 3.
Chapter 5 – Decisions Big Java by Cay Horstmann
Chapter 2 Programming Basics.
Chapter 6 - Decisions.
Use a variable to store a value that you want to use at a later time
Building Java Programs Chapter 4
Chapter 5 – Decisions Big Java by Cay Horstmann
Control Structure.
Presentation transcript:

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. if (amount <= balance) balance = balance – amount; else balance = balance – OVERDRAFT_PENALTY The if/else Statement

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Simple statement: balance = balance - amount; Compound statement: if (balance >= amount) balance = balance - amount; Also loop statements — Chapter 6 Block statement: { double newBalance = balance - amount; balance = newBalance; } Statement Types

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. What is logically wrong with the statement if (amount <= balance) newBalance = balance - amount; balance = newBalance; and how do you fix it? Answer: Only the first assignment statement is part of the if statement. Use braces to group both assignment statements into a block statement. Self Check 5.2

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Relational operators compare values JavaMath NotationDescription > >Greater than >= ≥Greater than or equal < <Less than <= ≤Less than or equal == =Equal != ≠Not equal Comparing Values: Relational Operators

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. The == denotes equality testing: a = 5; // Assign 5 to a if (a == 5)... // Test whether a equals 5 Relational operators have lower precedence than arithmetic operators: amount + fee <= balance Comparing Values: Relational Operators

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Consider this code: double r = Math.sqrt(2); double d = r * r; if (d == 2) System.out.println("sqrt(2)squared 2"); else System.out.println("sqrt(2)squared 2 but “ + d); It prints: sqrt(2)squared 2 is not 2 but Comparing Floating-Point Numbers

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. To avoid roundoff errors, don’t use == to compare floating-point numbers To compare floating-point numbers test whether they are close enough: |x - y| ≤ ε final double EPSILON = 1E-14; if (Math.abs(x - y) <= EPSILON) // x is approximately equal to y ε is a small number such as Comparing Floating-Point Numbers

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. To test whether two strings are equal to each other, use equals method: if (string1.equals(string2))... Don’t use == for strings! if (string1 == string2) // Not useful == tests identity, equals tests equal contents Case insensitive test: if (string1.equalsIgnoreCase(string2)) Comparing Strings

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. string1.compareTo(string2) < 0 means: string1 comes before string2 in the dictionary string1.compareTo(string2) > 0 means: string1 comes after string2 string1.compareTo(string2) == 0 means: string1 equals string2 "car" comes before "cargo" All uppercase letters come before lowercase: "Hello" comes before "car" Comparing Strings

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Lexicographic Comparison

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. == tests for identity, equals for identical content Rectangle box1 = new Rectangle(5, 10, 20, 30); Rectangle box2 = box1; Rectangle box3 = new Rectangle(5, 10, 20, 30); box1 != box3, but box1.equals(box3) box1 == box2 Caveat: equals must be defined for the class Comparing Objects

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Object Comparison

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. null reference refers to no object: String middleInitial = null; // Not set if (... ) middleInitial = middleName.substring(0, 1); Can be used in tests: if (middleInitial == null) System.out.println(firstName + " " + lastName); else System.out.println(firstName + " " + middleInitial + ". " + lastName ); Use ==, not equals, to test for null null is not the same as the empty string "" Testing for null

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Relational Operator Examples

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Nested Branches

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. George Boole ( ): pioneer in the study of logic value of expression amount < 1000 is true or false boolean type: one of these 2 truth values Using Boolean Expressions: The boolean Type

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. && and || or ! not if (0 < amount && amount < 1000)... if (input.equals("S") || input.equals("M"))... if (!input.equals("S"))... Using Boolean Expressions: The Boolean Operators

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. && and || Operators

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Boolean Operators

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. private boolean married; Set to truth value: married = input.equals("M"); Use in conditions: if (married)... else... if (!married)... Also called flag It is considered gauche to write a test such as if (married == true)... // Don't Just use the simpler test if (married)... Using Boolean Variables