Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Advertisements

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Flow of Control (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
The if Statement The code in methods executes sequentially.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Logical Operators and Conditional statements
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Chapter 3: Decision Structures Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis.
C++ for Engineers and Scientists Third Edition
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
CSCI 130 Chapter 4 Statements, Expressions, and Operators.
© 2012 Pearson Education, Inc. All rights reserved. Chapter 3: Decision Structures Starting Out with Java: From Control Structures through Data Structures.
Chapter 3 Decision Structures 1. Contents 1.The if Statement 2.The if-else Statement 3.The if-else-if Statement 4.Nested if Statement 5.Logical Operators.
Lecture 3 Decision Structures. 4-2 Topics – The if Statement – The if - else Statement – The PayRoll class – Nested if Statements – The if - else - if.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
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
Starting Out with Java: From Control Structures through Objects
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
CS102 Introduction to Computer Programming Chapter 4 Making Decisions Continued.
Chapter 3 Decision Structures Starting Out with Java: From Control Structures through Objects.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
INLS 560 – C ONDITIONALS Instructor: Jason Carter.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
Flow of Control Part 1: Selection
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Control Stuctures Module 4 Copyright © Copyright © Slide #2 Decision Structures Chapter Objectives To understand: how to write boolean expressions.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Chapter 3: Decision Structures
CHAPTER 3 Decision Structures Copyright © 2016 Pearson Education, Inc., Hoboken NJ.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: From Control Structures through Objects Third Edition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Control statements Mostafa Abdallah
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC INTRO TO COMPUTING - PROGRAMMING If Statement.
Decision Statements, Short- Circuit Evaluation, Errors.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
CHAPTER 3 Decision Structures Copyright © 2016 Pearson Education, Ltd.
CS0007: Introduction to Computer Programming
Chapter 3: Decision Structures by Tony Gaddis and Godfrey Muganda
Chapter 3: Decision Structures
Chapter 4: Making Decisions.
Lecture 3- Decision Structures
Topics The if Statement The if-else Statement Comparing Strings
Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
Chapter 3: Decision Structures
if-else-if Statements
Chapter 3: Decision Structures
Topics The if Statement The if-else Statement Comparing Strings
Chapter 3: Program Statements
Chapter 3:Decision Structures
Comparing Strings Strings can be compared using the == and != operators String comparisons are case sensitive Strings can be compared using >, =, and.
Topics The if Statement The if-else Statement Comparing Strings
The System.exit() Method
Logic of an if statement
CprE 185: Intro to Problem Solving (using C)
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Presentation transcript:

Chapter 3:Decision Structures

 3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical Operators  3.6 Comparing String Objects  3.7 More About Variable Declaration and Scope  3.9 The switch Statement  3.10 Creating Objects with the DecimalFormat Class  3.12 Common Errors to Avoid

Nested if Statements  If an if statement appears inside of another if statement (single or block) it is called a nested if statement.  The nested if is only executed if the if statement it is in results in a true condition.  Nested if statements can get very complex, very quickly.

LoanQualifier.java double salary, yearsOnJob; String input; input = JOptionPane.showInputDialog("Enter your " + "annual salary."); salary = Double.parseDouble(input); input = JOptionPane.showInputDialog("Enter the number of " +"years at your current job."); yearsOnJob = Double.parseDouble(input);

LoanQualifier.java if (salary >= 30000) { if (yearsOnJob >= 2) { JOptionPane.showMessageDialog(null, "You qualify " + "for the loan."); } else { JOptionPane.showMessageDialog(null, "You must have " + "been on your current job for at least " + "two years to qualify."); } else { JOptionPane.showMessageDialog(null, "You must earn at” + “least $30,000 per year to qualify."); }

Nested if Statement Flowcharts Wear a jacket. Yes Is it cold outside? Wear shorts. Is it snowing? Wear a parka. No Yes

if-else Matching  Curly brace use is not required if there is only one statement to be conditionally executed.  However, sometimes curly braces can help make the program more readable.  Additionally, proper indentation makes it much easier to match up else statements with their corresponding if statement.

if-else Matching if (employed == 'y') { if (recentGrad == 'y') { System.out.println("You qualify for the special interest rate."); } else { System.out.println("You must be a recent college graduate to qualify."); } else { System.out.println("You must be employed to qualify."); } This else matches with this if. This else matches with this if.

Nested if Statements  If an if statement appears inside of another if statement (single or block) it is called a nested if statement.  The nested if is only executed if the if statement it is in results in a true condition.  Nested if statements can get very complex, very quickly.

LoanQualifier.java double salary, yearsOnJob; String input; input = JOptionPane.showInputDialog("Enter your " + "annual salary."); salary = Double.parseDouble(input); input = JOptionPane.showInputDialog("Enter the number of " +"years at your current job."); yearsOnJob = Double.parseDouble(input);

LoanQualifier.java if (salary >= 30000) { if (yearsOnJob >= 2) { JOptionPane.showMessageDialog(null, "You qualify " + "for the loan."); } else { JOptionPane.showMessageDialog(null, "You must have " + "been on your current job for at least " + "two years to qualify."); } else { JOptionPane.showMessageDialog(null, "You must earn at” + “least $30,000 per year to qualify."); }

Nested if Statement Flowcharts Wear a jacket. Yes Is it cold outside? Wear shorts. Is it snowing? Wear a parka. No Yes

if-else Matching  Curly brace use is not required if there is only one statement to be conditionally executed.  However, sometimes curly braces can help make the program more readable.  Additionally, proper indentation makes it much easier to match up else statements with their corresponding if statement.

if-else Matching if (employed == 'y') { if (recentGrad == 'y') { System.out.println("You qualify for the special interest rate."); } else { System.out.println("You must be a recent college graduate to qualify."); } else { System.out.println("You must be employed to qualify."); } This else matches with this if. This else matches with this if.

Logical Operators  Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions.  Java also provides one unary (!) logical operator to reverse the truth of a boolean expression.

Logical Operators OperatorMeaningEffect &&AND Connects two boolean expressions into one. Both expressions must be true for the overall expression to be true. ||OR Connects two boolean expressions into one. One or both expressions must be true for the overall expression to be true. It is only necessary for one to be true, and it does not matter which one. !NOT The ! operator reverses the truth of a boolean expression. If it is applied to an expression that is true, the operator returns false. If it is applied to an expression that is false, the operator returns true.

The && Operator  The logical AND operator (&&) takes two operands that must both be boolean expressions.  The resulting combined expression is true iff (if and only if) both operands are true. Expression 1Expression 2 Expression1 && Expression2 truefalse truefalse true

LogicalAND double salary; // Annual salary double yearsOnJob; // Years at current job String input; // To hold string input input = JOptionPane.showInputDialog("Enter your " + "annual salary.") salary = Double.parseDouble(input); input = JOptionPane.showInputDialog( "Enter the number of " +"years at your current job."); yearsOnJob = Double.parseDouble(input);

LogicalAND // Determine whether the user qualifies for the loan. if (salary >= && yearsOnJob >= 2) { JOptionPane.showMessageDialog(null, "You qualify " + "for the loan."); } else { JOptionPane.showMessageDialog(null, "You do not " + "qualify for the loan."); }

The || Operator  The logical OR operator (||) takes two operands that must both be boolean expressions.  The resulting combined expression is false iff (if and only if) both operands are false. Expression 1 Expression 2 Expression1 || Expression2 truefalsetrue falsetrue false true

LogicalOR double salary; // Annual salary double yearsOnJob; // Years at current job String input; // To hold string input input = JOptionPane.showInputDialog("Enter your " + "annual salary.") salary = Double.parseDouble(input); input = JOptionPane.showInputDialog( "Enter the number of " +"years at your current job."); yearsOnJob = Double.parseDouble(input);

LogicalOR // Determine whether the user qualifies for the loan. if (salary >= || yearsOnJob >= 2) { JOptionPane.showMessageDialog(null, "You qualify " +"for the loan."); } else { JOptionPane.showMessageDialog(null, "You do not " + "qualify for the loan."); }

The ! Operator  The ! operator performs a logical NOT operation.  If an expression is true, !expression will be false. if (!(temperature > 100)) System.out.println(“Below the maximum temperature.");  If temperature > 100 evaluates to false, then the output statement will be run. Expression 1!Expression1 truefalse true

Short Circuiting  Logical AND and logical OR operations perform short-circuit evaluation of expressions.  Logical AND will evaluate to false as soon as it sees that one of its operands is a false expression.  Logical OR will evaluate to true as soon as it sees that one of its operands is a true expression.

Order of Precedence  The ! operator has a higher order of precedence than the && and || operators.  The && and || operators have a lower precedence than relational operators like.  Parenthesis can be used to force the precedence to be changed.

Order of Precedence OperatorsDescription 1 (unary negation) ! Unary negation, logical NOT 2* / %Multiplication, Division, Modulus 3+ -Addition, Subtraction 4 = Less-than, Greater-than, Less- than or equal to, Greater-than or equal to 5== !=Is equal to, Is not equal to 6&&Logical AND 7||Logical NOT 8 = += -= *= /= %= Assignment and combined assignment operators.

Comparing String Objects  In most cases, you cannot use the relational operators to compare two String objects.  Reference variables contain the address of the object they represent.  Unless the references point to the same object, the relational operators will not return true. StringCompareTo.java

StringCompare.java String name1 = "Mark", name2 = "Mark", name3 = "Mary"; // Compare "Mark" and "Mark" if (name1.equals(name2)) { System.out.println(name1 + " and " + name2 + " are the same."); } else { System.out.println(name1 + " and " + name2 + " are the NOT the same."); }

StringCompare.java // Compare "Mark" and "Mary" if (name1.equals(name3)) { System.out.println(name1 + " and " + name3 + " are the same."); } else { System.out.println(name1 + " and " + name3 + " are the NOT the same."); }

StringCompareTo.java String name1 = "Mary", name2 = "Mark"; // Compare "Mary" and "Mark" if (name1.compareTo(name2) < 0) { System.out.println(name1 + " is less than " + name2); } else if (name1.compareTo(name2) == 0) { System.out.println(name1 + " is equal to " + name2); } else if (name1.compareTo(name2) > 0) { System.out.println(name1 + " is greater than " + name2); }

Ignoring Case in String Comparisons  In the String class the equals and compareTo methods are case sensitive.  In order to compare two String objects that might have different case, use: equalsIgnoreCase, or compareToIgnoreCase

SecretWord.java if (input.equalsIgnoreCase("PROSPERO")) { System.out.println("Congratulations!” + “You know the“ + “ secret word!"); } else { System.out.println("Sorry, that is NOT” “the " +"secret word!"); }