1 Fall 2009ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The...

15
1 Fall 2009 ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators Comparing String Objects The switch Statement The Conditional Operator Variable Declaration and Scope DecimalFormat Class printf Focus for now on the variations of the if statement, logical operators, comparing strings, switch

Transcript of 1 Fall 2009ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The...

Page 1: 1 Fall 2009ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.

1Fall 2009 ACS-1903

Chapter 3 Decision Structures

•The if Statement•The if-else Statement•The if-else-if Statement•Nested if Statements

•Logical Operators •Comparing String Objects•The switch Statement

•The Conditional Operator

•Variable Declaration and Scope

•DecimalFormat Class

•printfFocus for now on the variations of the if statement, logical operators, comparing strings, switch

Page 2: 1 Fall 2009ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.

2Fall 2009 ACS-1903

if statements

Suppose we need to calculate reward points for a retailer.

Assume a customer receives 1 reward point for each 25 dollars spent, but if the customer spends more than 300 dollars the customer receives an extra 50 points. Examples:

Dollars spent26

101199300301500

Reward Points147

126270

Page 3: 1 Fall 2009ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.

3Fall 2009 ACS-1903

pseudocode

If sale is less than or equal to 300

Then reward points are dollar value / 25

Otherwise reward points are 50 plus (dollar value / 25)

Page 4: 1 Fall 2009ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.

4Fall 2009 ACS-1903

Flowchart

Sale > 300

Reward points =50 + (Dollars / 25)

Reward points =Dollars / 25

truefalse

Page 5: 1 Fall 2009ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.

5Fall 2009 ACS-1903

if statement

if ( sale <= 300 )

rewardPoints = dollars / 25;

else

rewardPoints = 50 + dollars / 25;

see RewardPoints50.java

Page 6: 1 Fall 2009ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.

6Fall 2009 ACS-1903

if statements

• The if statement contains a section of code that executes if the value of a boolean expression is true. Optionally, there is a section of code that executes if the value of the boolean expression is false.

• General syntax

if (boolean expression) java statement;[ else java statement; ]

• “[“ and “]” show an optional component in syntax• If there is more than one statement required for a “then” clause or an “else” clause, you can create a compound statement using curly braces {}

Page 7: 1 Fall 2009ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.

7Fall 2009 ACS-1903

if statements

• Can we rewrite the previous code without an else clause?

• If-else is a java statement and If’s can be nested

• Suppose we give reward points out as before but with an additional bonus of 100 points if the sale is for more than $500.

• Pseudocode:

If sale <= 300 then reward = dollars /25

otherwise if sale <= 500 then reward = dollars/25 + 50

otherwise reward = dollars/25 + 150

Page 8: 1 Fall 2009ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.

8Fall 2009 ACS-1903

Flowchart

Sale > 300

Reward points =50 + (Dollars / 25)

Reward points =Dollars / 25

truefalse

Reward points =150 + (Dollars / 25)

Sale > 500truefalse

Page 9: 1 Fall 2009ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.

9Fall 2009 ACS-1903

Java Code

if ( sale <= 300 ) rewardPoints = dollars / 25;else if (sale <=500 ) rewardPoints = 50 + dollars / 25; else rewardPoints = 150 + dollars /25;

see RewardPoints.java

Page 10: 1 Fall 2009ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.

10Fall 2009 ACS-1903

Relational Operators

• In most cases, the boolean expression, used by the if statement, uses relational operators.

Relational Operator Meaning

> is greater than

< is less than

>= is greater than or equal to

<= is less than or equal to

== is equal to

!= is not equal to

Page 11: 1 Fall 2009ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.

11Fall 2009 ACS-1903

Boolean Expressions

• A boolean expression is any variable or calculation that results in a true or false condition.

Expression Meaning

x > y Is x greater than y?

x < y Is x less than y?

x >= y Is x greater than or equal to y?

x <= y Is x less than or equal to y.

x == y Is x equal to y?

x != y Is x not equal to y?

Page 12: 1 Fall 2009ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.

12Fall 2009 ACS-1903

Boolean Expressions

if (x > y)

System.out.println(“X is greater than Y”);

if(x == y)

System.out.println(“X is equal to Y”);

if(x != y) {

System.out.println(“X is not equal to Y”);x = y;System.out.println(“However, now it is.”);

}

Example: AverageScoreWithScanner.java

Note the use of “{“ and “}”

There are 3 statements executed when x!=y is true

How should we modify this program to also find the largest of 3 scores?

Page 13: 1 Fall 2009ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.

13Fall 2009 ACS-1903

Comparing Strings

if (gender.equals("male"))

{

System.out.println("Hello Mr." + name);

}

else

{

System.out.println("Hello Ms." + name);

}

System.out.println("Your gross pay is $" + grossPay);

To compare two strings in Java you typically use equals() or equalsIgnoreCase()

See documentation for class String Now … using BlueJ

Page 14: 1 Fall 2009ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.

14Fall 2009 ACS-1903

Programming Style

• Our programming standard:

• The conditionally executed statement should be on the line after the if condition

• The conditionally executed statement should be indented one level from the if condition.

• It is suggested by some to always use curly braces

• when an if statement does not have the curly braces, it is ended by the first semicolon encountered after the if condition.

if(expression)

statement;

No semicolon here.Semicolon ends statement here.

Page 15: 1 Fall 2009ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.

15Fall 2009 ACS-1903

switch statement – an n-way decision structure

Value of integer or char expression is

case1 actions

=case1 =case2 =casen

case2 actions casen actions…

Text examples:

SwitchDemo.java PetFood.java