Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.

Slides:



Advertisements
Similar presentations
Chapter 4: Making Decisions.
Advertisements

Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 3 Control Statements.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
C++ for Engineers and Scientists Third Edition
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
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 J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
Flow of Control Part 1: Selection
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Chapter 4 Selection: the if-else and switch-case instructions.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
Control statements Mostafa Abdallah
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
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.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
Chapter 4: Control Structures I
CNG 140 C Programming (Lecture set 3)
Chapter 3 Control Statements
Chapter 4: Control Structures I
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Part II The Switch Statement
Chapter 4: Control Structures I
Chapter 3:Decision Structures
Chapter 4: Control Structures I (Selection)
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Presentation transcript:

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

2 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement

3 Relational Expressions Relational expressions –Expressions that compare operands –Sometimes called conditions –Evaluated to yield result –Typically used as part of selection statement Simple relational expression –Consists of relational operator connecting two variable and/or constant operands

4 Relational Expressions (continued) Figure 5.1: The anatomy of a simple relational expression

5 Relational Expressions (continued) Table 5.1: Relational Operators for Primitive Data Types

6 Logical Operators Complex conditions can be created using Boolean logical operations: –AND –OR –NOT AND operator –&& –True only if both expressions are true

7 Logical Operators (continued) OR operator –|| –True if either (or both) expressions are true NOT operator –! –Changes expression to opposite state Relational operators have higher precedence than logical operators

8 Logical Operators (continued) && and || operators: –Can only be used with Boolean operands –Second operand not evaluated if evaluation of first is sufficient to determine final value of logical operation (called short-circuit evaluation)

9 Logical Operators (continued) Table 5.2: Operator Precedence

10 A Numerical Accuracy Problem Tests for equality of numbers using relational operator == should be avoided –Applies to: Floating-point Double-precision Many decimal numbers cannot be represented exactly in binary using finite number of bits Require that absolute value of difference between operands be less than extremely small value

11 The if-else Statement Directs computer to select sequence of instructions based on result of comparison Condition is evaluated first –If value of condition is true Statement in the if clause is executed –If value is false Statement after reserved word else executed

12 The if-else Statement (continued) if (condition) < no semicolon here statement1; else < no semicolon here statement2;

13 The if-else Statement (continued) Figure 5.2: The if-else flowchart

14 Compound Statements Any number of single statements contained between braces Takes place of single statement Semicolon is not placed after braces that define compound statement

15 Compound Statements (continued) Figure 5.5: A compound statement consists of individual statements enclosed within braces

16 The Boolean Data Type Tested condition in if-else statement must always evaluate to Boolean value Value of condition must be either true or false Boolean data type –Restricted in usage as value of relational expression –Boolean values can be: Displayed Compared Assigned

17 One-Way Selection No else expression Syntax: if (condition) statement; Statement following if (condition) –Only executed if condition has true value

18 Placement of Braces in a Compound Statement Common practice for some programmers –Place opening brace of compound statement on same line as if and else statements if (condition) { statement1; } else { statement2; }

19 Nested if Statements One or more if-else statements can be included within either part of if-else statement Last else with closest unpaired if –Unless braces alter default pairing Process of nesting if statements can be extended indefinitely

20 The if-else Chain Else part of an if statement contains another if-else Syntax: if (expression1) statement1; else if (expression2) statement2; else statement3;

21 The if-else Chain (continued) Alternate syntax: if (expression1) statement1; else if (expression2) statement2; else statement3; Used extensively in programming applications

22 import javax.swing.*; import java.text.*; // needed for formatting public class CalculateTaxes { public static void main(String[] args) { double taxable, taxes; String s1; DecimalFormat df = new DecimalFormat(",###.00"); s1 =JOptionPane.showInputDialog("Please type in the taxable income:"); taxable = Double.parseDouble(s1); if (taxable <= ) taxes = 0.02 * taxable; else taxes = * (taxable ) ; JOptionPane.showMessageDialog(null, "Taxes are $" + df.format(taxes), "QuickTest Program 5.1", JOptionPane.INFORMATION_MESSAGE); System.exit(0); }

23 Compound Statement Example int tempType; double temp, fahren, celsius; String s1, prompt, message; // set up format variable DecimalFormat num = new DecimalFormat(",###.00"); s1 = JOptionPane.showInputDialog("Enter the temperature to be converted: "); temp = Double.parseDouble(s1); prompt = "Enter a 1 if the temperature is in Fahrenheit\n" +" or a 2 if the temperature is in Celsius: "; s1 =JOptionPane.showInputDialog(prompt); tempType = Integer.parseInt(s1); if (tempType = = 1) { celsius = (5.0 / 9.0) * (temp ); message = "The equivalent Celsius temperature is " + num.format(celsius); } else { fahren = (9.0 / 5.0) * temp ; message = "The equivalent Fahrenheit temperature is " + num.format(fahren); }

24 import javax.swing.*; public class CheckLimit { public static void main(String[] args) { final double LIMIT = ; String s1; double mileage; s1 = JOptionPane.showInputDialog("Please enter the mileage driven:"); mileage = Double.parseDouble(s1); if(mileage > LIMIT) JOptionPane.showMessageDialog(null, "For a mileage of " + mileage + "\nThis car is over the mileage limit", "QuickTest Program 5.3", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "End of Program", "QuickTest Program 5.3", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } One Way Selection

25 Nested Chain s1 = JOptionPane.showInputDialog("Enter the value of monthly sales:"); monthlySales = Double.parseDouble(s1); if (monthlySales >= ) income = * monthlySales; else if (monthlySales >= ) income = * monthlySales; else if (monthlySales >= ) income = * monthlySales; else if (monthlySales >= ) income = * monthlySales; else if (monthlySales >= ) income = * monthlySales; else income = * monthlySales; outMessage = "For monthly sales of $" + num.format(monthlySales) + "\nThe income is $" + num.format(income);

26 Lab Work Input and run QuickTest programs Begin lab 9 assignments