+ All Categories
Home > Documents > Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I:...

Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I:...

Date post: 29-Jan-2016
Category:
Upload: daniel-morris
View: 220 times
Download: 0 times
Share this document with a friend
42
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection
Transcript
Page 1: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

Java Programming: From Problem Analysis to Program Design, 3e

Chapter 4

Control Structures I: Selection

Page 2: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

2Java Programming: From Problem Analysis to Program Design, 3e

Chapter Objectives

• Learn about control structures

• Examine relational and logical operators

• Explore how to form and evaluate logical (Boolean) expressions

• Learn how to use the selection control structures if, if…else, and switch in a program

Page 3: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

3Java Programming: From Problem Analysis to Program Design, 3e

Control Structures

• Three methods of processing a program– In sequence– Branching– Looping

• Branch: altering the flow of program execution by making a selection or choice

• Loop: altering the flow of program execution by repetition of statement(s)

Page 4: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

4Java Programming: From Problem Analysis to Program Design, 3e

Flow of Execution

Page 5: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

5Java Programming: From Problem Analysis to Program Design, 3e

Relational Operators

• Relational Operator– Allows you to make comparisons in a program– Binary operator

• Condition is represented by a logical expression in Java

• Logical expression: expression that has a value of either true or false

Page 6: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

6Java Programming: From Problem Analysis to Program Design, 3e

Relational Operators in Java

Page 7: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

7Java Programming: From Problem Analysis to Program Design, 3e

Relational Operators and Primitive Data Types

• Can be used with integral and floating-point data types

• Can be used with the char data type

• Unicode Collating Sequence

Page 8: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

8Java Programming: From Problem Analysis to Program Design, 3e

Relational Operators and the Unicode Collating Sequence

Page 9: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

9Java Programming: From Problem Analysis to Program Design, 3e

Comparing Strings

• class String – Method compareTo– Method equals

• Given string str1 and str2

str2str1 0

str2str1 0

str2str1 0

reTo(str2)str1.compa

string ifinteger an

string toequal is string if

string ifinteger an

Page 10: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

10Java Programming: From Problem Analysis to Program Design, 3e

Comparing Strings (continued)String str1 = "Hello";

String str2 = "Hi";

String str3 = "Air";

String str4 = "Bill";

String str5 = "Bigger";

Page 11: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

11Java Programming: From Problem Analysis to Program Design, 3e

Comparing Strings (continued)

Page 12: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

12Java Programming: From Problem Analysis to Program Design, 3e

Comparing Strings (continued)

Page 13: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

13Java Programming: From Problem Analysis to Program Design, 3e

Logical (Boolean) Operators

Page 14: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

14Java Programming: From Problem Analysis to Program Design, 3e

Logical (Boolean) Operators (continued)

Page 15: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

15Java Programming: From Problem Analysis to Program Design, 3e

Logical (Boolean) Operators (continued)

Page 16: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

16Java Programming: From Problem Analysis to Program Design, 3e

Logical (Boolean) Operators (continued)

Page 17: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

17Java Programming: From Problem Analysis to Program Design, 3e

Precedence of Operators

Page 18: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

18Java Programming: From Problem Analysis to Program Design, 3e

Short-Circuit Evaluation

• Definition: a process in which the computer evaluates a logical expression from left to right and stops as soon as the value of the expression is known

Page 19: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

19Java Programming: From Problem Analysis to Program Design, 3e

Selection

• One-Way Selection

• Two-Way Selection

• Compound (Block of) Statements

• Multiple Selections (Nested if)

• Conditional Operator• switch Structures

Page 20: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

20Java Programming: From Problem Analysis to Program Design, 3e

One-Way Selection

• Syntax:if (expression)

statement

• Expression referred to as decision maker

• Statement referred to as action statement

Page 21: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

One-Way Selection (continued)

Java Programming: From Problem Analysis to Program Design, 3e 21

Page 22: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

22Java Programming: From Problem Analysis to Program Design, 3e

Example 4-11

//Program to determine the absolute value of an integer

import javax.swing.JOptionPane;

public class AbsoluteValue

{

public static void main(String[] args)

{

int number;

int temp;

String numString;

numString =

JOptionPane.showInputDialog

("Enter an integer:"); //Line 1

number = Integer.parseInt(numString); //Line 2

temp = number; //Line 3

One-Way Selection (continued)

Page 23: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

23Java Programming: From Problem Analysis to Program Design, 3e

if (number < 0) //Line 4 number = -number; //Line 5

JOptionPane.showMessageDialog(null, "The absolute value of " + temp + " is " + number, "Absolute Value", JOptionPane.INFORMATION_MESSAGE); //Line 6 System.exit(0); }

One-Way Selection (continued)

Page 24: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

24Java Programming: From Problem Analysis to Program Design, 3e

Two-Way Selection

• Syntax:

if (expression)

statement1

else

statement2• else statement must be paired with an if

Page 25: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

25Java Programming: From Problem Analysis to Program Design, 3e

Two-Way Selection (continued)

Page 26: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

26Java Programming: From Problem Analysis to Program Design, 3e

Two-Way Selection (continued)

Example 4-14

if (hours > 40.0) wages = 40.0 * rate + 1.5 * rate * (hours - 40.0); else wages = hours * rate;

Page 27: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

27Java Programming: From Problem Analysis to Program Design, 3e

Example 4-15

if (hours > 40.0); //Line 1 wages = 40.0 * rate + 1.5 * rate * (hours - 40.0); //Line 2else //Line 3 wages = hours * rate; //Line 4

•Because a semicolon follows the closing parenthesis of the if statement (Line 1), the else statement stands alone •The semicolon at the end of the if statement (see Line 1) ends the if statement, so the statement at Line 2 separates the else clause from the if statement; that is, else is by itself •Since there is no separate else statement in Java, this code generates a syntax error

Two-Way Selection (continued)

Page 28: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

28Java Programming: From Problem Analysis to Program Design, 3e

Compound (Block of) Statements• Syntax

Page 29: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

29Java Programming: From Problem Analysis to Program Design, 3e

Compound (Block of) Statements (continued)

if (age > 18){ System.out.println("Eligible to vote."); System.out.println("No longer a minor.");} else{ System.out.println("Not eligible to vote."); System.out.println("Still a minor.");}

Page 30: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

30Java Programming: From Problem Analysis to Program Design, 3e

Multiple Selection: Nested if

• Syntax

if (expression1)

statement1

else if (expression2)

statement2

else

statement3

• Else associated with most recent incomplete if

• Multiple if statements can be used in place of if…else statements

• May take longer to evaluate

Page 31: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

31Java Programming: From Problem Analysis to Program Design, 3e

Conditional (? :) Operator

• Ternary operator

• Syntaxexpression1 ? expression2 : expression3

• If expression1 = true, then the result of the condition is expression 2; otherwise, the result of the condition is expression3

Page 32: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

32Java Programming: From Problem Analysis to Program Design, 3e

switch Structures

Page 33: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

33Java Programming: From Problem Analysis to Program Design, 3e

• In Java, switch, case, break, and default are reserved words

• In a switch structure, the expression is evaluated first

• The value of the expression is then used to perform the actions specified in the statements that follow the reserved word case

• The expression is usually an identifier • The value of the identifier or the expression can

be only integral, that is, an integer

switch Structures (continued)

Page 34: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

switch Structures (continued)• Integral values also include values of type char• The expression is sometimes called the selector; its

value determines which statements are selected for execution

• A particular case value must appear only once• One or more statements may follow a case label,

so you do not need to use braces to turn multiple statements into a single compound statement

Java Programming: From Problem Analysis to Program Design, 3e 34

Page 35: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

switch Structures (continued)

• The break statement may or may not appear after each statements1, statements2, ..., statementsn

• A switch structure may or may not have the default label

Java Programming: From Problem Analysis to Program Design, 3e 35

Page 36: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

36Java Programming: From Problem Analysis to Program Design, 3e

switch Structures (continued)

Page 37: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

37Java Programming: From Problem Analysis to Program Design, 3e

switch Structures (continued)

Page 38: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

38Java Programming: From Problem Analysis to Program Design, 3e

Example 4-24

switch (grade){case 'A': System.out.println("The grade is A."); break;

case 'B': System.out.println("The grade is B."); break;

case 'C': System.out.println("The grade is C."); break;

switch Structures (continued)

Page 39: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

39Java Programming: From Problem Analysis to Program Design, 3e

case 'D': System.out.println("The grade is D."); break;

case 'F': System.out.println("The grade is F."); break;

default: System.out.println("The grade is invalid.");}

switch Structures (continued)

Page 40: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

40Java Programming: From Problem Analysis to Program Design, 3e

Programming Example: Cable Company Billing

• Input: customer’s account number, customer code, number of premium channels to which customer subscribes, number of basic service connections (in case of business customers)

• Output: customer’s account number and the billing amount

Page 41: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

41Java Programming: From Problem Analysis to Program Design, 3e

Programming Example: Cable Company Billing (continued)

• Solution: – Prompt user for information– Use switch statements based on customer’s

type– Use an if statement nested within switch

statement to determine amount due by each customer

Page 42: Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.

42Java Programming: From Problem Analysis to Program Design, 3e

Chapter Summary

• Control structures are used to process programs• Logical expressions and order of precedence of

operators are used in expressions• Compare strings • If statements• if…else statements• switch structures• Proper syntax for using control statements


Recommended