1 Fall 2009ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The...
-
Upload
bryce-marshall -
Category
Documents
-
view
219 -
download
0
Transcript of 1 Fall 2009ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The...
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
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
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)
4Fall 2009 ACS-1903
Flowchart
Sale > 300
Reward points =50 + (Dollars / 25)
Reward points =Dollars / 25
truefalse
5Fall 2009 ACS-1903
if statement
if ( sale <= 300 )
rewardPoints = dollars / 25;
else
rewardPoints = 50 + dollars / 25;
see RewardPoints50.java
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 {}
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
8Fall 2009 ACS-1903
Flowchart
Sale > 300
Reward points =50 + (Dollars / 25)
Reward points =Dollars / 25
truefalse
Reward points =150 + (Dollars / 25)
Sale > 500truefalse
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
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
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?
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?
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
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.
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