Java Programming Fifth Edition

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.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
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
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
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.
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.
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
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.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
1 2. Program Construction in Java. 2.4 Selection (decisions)
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Chapter 5: Making Decisions
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
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.
Java Programming Fifth Edition Chapter 5 Making Decisions.
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.
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.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
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.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
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.
CNG 140 C Programming (Lecture set 3)
More on the Selection Structure
Selections Java.
Chapter 4: Making Decisions.
EGR 2261 Unit 4 Control Structures I: Selection
Debugging and Random Numbers
Chapter 4: Making Decisions.
JavaScript: Control Statements.
JavaScript: Control Statements I
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Control Structures I (Selection)
CMSC 202 Java Primer 2.
3 Control Statements:.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Java Programming Control Structures Part 1
Boolean Expressions to Make Comparisons
PROGRAM FLOWCHART Selection Statements.
Using C++ Arithmetic Operators and Control Structures
CSC215 Lecture Control Flow.
Controlling Program Flow
Boolean Expressions September 1, 2019 ICS102: The course.
Presentation transcript:

Java Programming Fifth Edition Chapter 5 Making Decisions

Java Programming, Fifth Edition

Java Programming, Fifth Edition

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 Java Programming, Fifth Edition

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

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 Edition

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

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 Edition

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 Edition

The if...else Structure Single-alternative if Dual-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 Edition

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 Java Programming, Fifth Edition

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

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 Edition

Java Programming, Fifth Edition

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 Java Programming, Fifth Edition

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

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 Java Programming, Fifth Edition

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

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 Java Programming, Fifth Edition

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

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 Java Programming, Fifth Edition

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

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 < 5.65 && 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 Edition

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

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 Edition

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 Java Programming, Fifth Edition

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

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 Edition

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 Edition

Using the NOT Operator NOT operator Statements with 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 Edition

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 Java Programming, Fifth Edition

Understanding Precedence (continued) Java Programming, Fifth Edition

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

Understanding Precedence (continued) Java Programming, Fifth Edition