Java Programming Fifth Edition Chapter 5 Making Decisions.

Slides:



Advertisements
Similar presentations
Chapter 5– Making Decisions Dr. Jim Burns. In Java, the simplest statement.. Is the if(..) statement if(someVariable==10) System.out.println(“The value.
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
C++ for Engineers and Scientists Third Edition
Visual C++ Programming: Concepts and Projects
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Programming Logic and Design Fourth Edition, Introductory
Programming Logic and Design Sixth Edition
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Chapter 3 Making Decisions
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Java Programming, Second Edition Chapter Five Input and Selection.
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Jaeki Song ISQS6337 JAVA Lecture 04 Control Structure - Selection, and Repetition -
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
Programming Logic and Design, Second Edition, Comprehensive
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Chapter 5: Making Decisions
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Decision Statements, Short- Circuit Evaluation, Errors.
Flow of Control Joe McCarthy CSS 161: Fundamentals of Computing1.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Programming Logic and Design Fifth Edition, Comprehensive Chapter 4 Making Decisions.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Java Programming Fifth Edition
More on the Selection Structure
Chapter 4: Making Decisions.
EGR 2261 Unit 4 Control Structures I: Selection
The Selection Structure
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Making Decisions.
Microsoft Visual Basic 2005 BASICS
Topics The if Statement The if-else Statement Comparing Strings
3 Control Statements:.
Boolean Expressions to Make Comparisons
PROGRAM FLOWCHART Selection Statements.
Using C++ Arithmetic Operators and Control Structures
Presentation transcript:

Java Programming Fifth Edition Chapter 5 Making Decisions

Java Programming, Fifth Edition2 Objectives Learn about decision making Make decisions with the if and if...else structures Use multiple statements in an if or if...else structure Nest if and if...else statements Use AND and OR operators

Java Programming, Fifth Edition3 Objectives (continued) Learn to make accurate and efficient decisions Use the switch statement Use the conditional and NOT operators Understand precedence

Java Programming, Fifth Edition4 Understanding Decision Making Pseudocode –Use paper and pencil –Plan program’s logic By writing plain English statements –Accomplish important steps in a given task –Use everyday language Flowchart –Steps in diagram form –Series of shapes connected by arrows

Java Programming, Fifth Edition5 Understanding Decision Making (continued) Flowchart (continued) –Programmers use variety of shapes to represent different tasks Rectangle to represent any unconditional step Diamond to represent any decision Sequence structure –One step follows another unconditionally –Cannot branch away or skip a step

Java Programming, Fifth Edition6

7 Understanding Decision Making (continued) Decision structure –Involves choosing between alternative courses of action –Based on some value within program All computer decisions are yes-or-no decisions Boolean values –Values true and false –Used in every computer decision

Java Programming, Fifth Edition8

9 Making Decisions with the if and if...else Structures if statement –Simplest statement to make decision –Boolean expression appears within parentheses –Space between keyword if and opening parentheses –Execution always continues to next independent statement –Use double equal sign ( == ) to determine equivalency

Making Decisions with the if and if...else Structures (continued) Java Programming, Fifth Edition10

Pitfall: Misplacing a Semicolon in an if Statement No semicolon at end of first line of if statement –if (someVariable == 10) –Statement does not end there When semicolon follows if directly –Empty statement contains only semicolon –Execution continues with the next independent statement Java Programming, Fifth Edition11

Pitfall: Misplacing a Semicolon in an if Statement (continued) Java Programming, Fifth Edition12

Pitfall: Using the Assignment Operator Instead of the Equivalency Operator Attempt to determine equivalency –Using single equal sign –Rather than double equal sign –Illegal Can store Boolean expression’s value in Boolean variable –Before using in if statement Java Programming, Fifth Edition13

Pitfall: Attempting to Compare Objects Using the Relational Operators Use standard relational operators to compare values of primitive data types –Not objects Can use equals and not equals comparisons ( == and != ) with objects –To compare objects’ memory addresses instead of values Java Programming, Fifth Edition14

Java Programming, Fifth Edition15 The if...else Structure Single-alternative if –Only perform action, or not Based on one alternative Dual-alternative if –Two possible courses of action if...else statement –Performs one action when Boolean expression evaluates true –Performs different action when Boolean expression evaluates false

Java Programming, Fifth Edition16 The if...else Structure (continued) if...else statement (continued) –Statement that executes when if is true or false ends with semicolon –Vertically align keyword if with the keyword else –Illegal to code else without if –Depending on evaluation of Boolean expression following if Only one resulting action takes place

The if...else Structure (continued) Java Programming, Fifth Edition17

Using Multiple Statements in an if or if...else Structure Execute more than one statement –Use pair of curly braces Place dependent statements within block Crucial to place curly braces correctly Any variable declared within block local to that block Java Programming, Fifth Edition18

Java Programming, Fifth Edition19

Java Programming, Fifth Edition20 Nesting if and if...else Statements Nested if statements –Statements in which if structure is contained inside of another if structure –Use when two conditions must be met before some action is taken Pay careful attention to placement of else clauses else statements –Always associated with if on “first in-last out” basis

Nesting if and if...else Statements (continued) Java Programming, Fifth Edition21

Java Programming, Fifth Edition22 Using Logical AND and OR Operators Logical AND operator –Alternative to some nested if statements –Used between two Boolean expressions to determine whether both are true –Written as two ampersands (&&) Include complete Boolean expression on each side –Both Boolean expressions that surround operator Must be true before action in statement can occur

Using Logical AND and OR Operators (continued) Java Programming, Fifth Edition23

Java Programming, Fifth Edition24 Using Logical AND and OR Operators (continued) Logical OR operator –Action to occur when at least one of two conditions is true –Written as || Sometimes called pipes

Using Logical AND and OR Operators (continued) Java Programming, Fifth Edition25

Java Programming, Fifth Edition26 Making Accurate and Efficient Decisions Range check –Series of if statements that determine whether: Value falls within specified range –Java programmers commonly place each else of subsequent if on same line –Within nested if...else Most efficient to ask most likely question first Avoid asking multiple questions

Making Accurate and Efficient Decisions (continued) Java Programming, Fifth Edition27

Using AND and OR Appropriately Errors of beginning programmers –Using AND operator when they mean to use OR Example: no payRate value can ever be both below 5.65 and over 60 at same time if(payRate 60) System.out.println("Error in pay rate"); Use pipes “||” operator instead –Using single ampersand or pipe To indicate logical AND or OR Java Programming, Fifth Edition28

Using the switch Statement switch statement –Alternative to series of nested if statements –Test single variable against series of exact integer or character values Java Programming, Fifth Edition29

Java Programming, Fifth Edition30 Using the switch Statement (continued) Switch structure keywords –switch Starts structure Followed by test expression Enclosed in parentheses –case Followed by one of the possible values for test expression and colon

Java Programming, Fifth Edition31 Using the switch Statement (continued) Switch structure keywords (continued) –break Optionally terminates switch structure at end of each case –default Used prior to any action if test variable does not match any case Optional

Using the switch Statement (continued) Java Programming, Fifth Edition32

Java Programming, Fifth Edition33 Using the switch Statement (continued) break statements in switch structure –If break statement omitted Program finds match for test variable All statements within switch statement execute from that point forward case statement –No need to write code for each case –Evaluate char variables Ignore whether uppercase or lowercase

Java Programming, Fifth Edition34 Using the switch Statement (continued) Why use switch statements? –Convenient when several alternative courses of action depend on single integer or character variable –Use only when there are reasonable number of specific matching values to be tested

Java Programming, Fifth Edition35 Using the Conditional and NOT Operators Conditional operator –Requires three expressions Separated with question mark and colon –Used as abbreviated version of if...else structure –Never required to use Syntax of conditional operator testExpression ? trueResult : falseResult;

Java Programming, Fifth Edition36 Using the Conditional and NOT Operators (continued) Boolean expression evaluated as true or false –If testExpression value true Entire conditional expression takes on value of expression following question mark –If value false Entire expression takes on value of false result Advantage of using conditional operator –Conciseness of statement

Java Programming, Fifth Edition37 Using the NOT Operator NOT operator –Written as exclamation point ( ! ) –Negates result of any Boolean expression –When preceded by NOT operator Any expression evaluates as: –true becomes false –false becomes true Statements with NOT operator –Harder to read –Require double set of parentheses

Java Programming, Fifth Edition38 Understanding Precedence Combine as many AND or OR operators as needed Operator’s precedence –How expression is evaluated –Order agrees with common algebraic usage Arithmetic done first Assignment done last AND operator evaluated before OR operator Statements in parentheses evaluated first

Understanding Precedence (continued) Java Programming, Fifth Edition39

Java Programming, Fifth Edition40 Understanding Precedence (continued) Two important conventions –Order in which operators are used –Always use parentheses Change precedence Or make your intentions clearer

Understanding Precedence (continued) Java Programming, Fifth Edition41

Java Programming, Fifth Edition42 You Do It Using an if...else Creating an Event class to use in a decision- making application Writing an application that uses the Event class Using the switch statement

Don’t Do It Don’t ignore subtleties in boundaries used in decision making Don’t use the assignment operator instead of the comparison operator Don’t insert a semicolon after the Boolean expression in an if statement Don’t forget to block a set of statements with curly braces when several statements depend on the if or the else statement Java Programming, Fifth Edition43

Don’t Do It (continued) Don’t forget to include a complete Boolean expression on each side of an && or || operator Don’t try to use a switch structure to test anything other than an integer or character value Don’t forget a break statement if one is required Don’t use the standard relational operators to compare objects Java Programming, Fifth Edition44

Java Programming, Fifth Edition45 Summary if statement –Make decision based on Boolean expression Single-alternative if –Performs action based on one alternative Dual-alternative if –if...else –Performs one action when a Boolean expression evaluates as true –Performs different action when expression evaluates as false

Java Programming, Fifth Edition46 Summary (continued) AND operator –&& –Determines whether two expressions are both true OR operator –|| –Carries out some action even if only one of two conditions true switch statement –Tests a single variable against a series of exact integer or character values

Java Programming, Fifth Edition47 Summary (continued) Conditional operator –Abbreviated version of if...else statement NOT operator –! –Negates result of any Boolean expression Operator precedence