Chapter 5 – Decisions Big Java by Cay Horstmann

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

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Math 130 Decisions II Wed, Sept 12, 2007 Notes: ref. C. Horstmann, Java Concepts.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Chapter 5 – Decisions Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
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.
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. if (amount
Decisions CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (Chapter 5, Horstmann*
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.
©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.
1 2. Program Construction in Java. 2.4 Selection (decisions)
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.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
ICT Introduction to Programming Chapter 4 – Control Structures I.
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.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Decisions Bush decision making.
Array length = maximum number of elements in array Usually, array is partially filled Need companion variable to keep track of current size Uniform naming.
Decision Statements, Short- Circuit Evaluation, Errors.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
1 Identifiers: Names of variables, functions, classes (all user defined objects), Examples: a b gcd GCD A COSC1373 TAX Tax_Rate Tax Rate if else while.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Control Structure  What is control Structure?  Types of Controls  Use the control structure in VBScript.  Example Summery.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
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 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Slides by Evan Gallagher
Slides by Evan Gallagher
A final variable is a constant
John Woodward A Simple Program – Hello world
Lecture 3 John Woodward.
Data Structures and Algorithms revision
Chapter 4 – Fundamental Data Types
Welcome to Computer Science Jeopardy
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 5/6 Decisions.
Chapter Three - Implementing Classes
Copyright © 2014 John Wiley & Sons, Inc. All rights reserved.
Programming 2 Decision-Making.
Wednesday 09/23/13.
Chapter 5 – Decisions Big Java by Cay Horstmann
Chapter 2 Programming Basics.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Classes CS 21a: Introduction to Computing I
Use a variable to store a value that you want to use at a later time
4.3 Arithmetic Operators and Mathematical Functions
Each object belongs to a class
1.7 Errors Compile-time error: A violation of the programming language rules that is detected by the compiler Example: System.ou.println("Hello, World!);
6.2 for Loops Example: for ( int i = 1; i
Parameter: an input to a method
Copyright © 2014 John Wiley & Sons, Inc. All rights reserved. Chapter 20 Managing the Multinational Financial System Tenth Edition Alan C. Shapiro Multinational.
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Presentation transcript:

Chapter 5 – Decisions Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

To be able to implement decisions using if statements Chapter Goals To be able to implement decisions using if statements To understand how to group statements into blocks To learn how to compare integers, floating-point numbers, strings, and objects To recognize the correct ordering of decisions in multiple branches To program conditions using Boolean operators and variables To understand the importance of test coverage Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

5.1 The if Statement The if statement lets a program carry out different actions depending on a condition if (amount <= balance) balance = balance – amount; Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

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

Statement Types Simple statement: Compound statement: balance = balance - amount; Compound statement: if (balance >= amount) balance = balance - amount; (don’t use this!) Block statement: { double newBalance = balance - amount; balance = newBalance; } Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Syntax 5.1 The if Statement Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

What is logically wrong with the statement Self Check 5.1 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.