Download presentation
Presentation is loading. Please wait.
1
1 Fall 2008ACS-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 Conditional Operator The switch Statement Variable Declaration and Scope DecimalFormat Class printf Focus for now on the variations of the if statement, logical operators, comparing strings
2
2 Fall 2008ACS-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 spent 26 101 199 300 301 500 Reward Points 1 4 7 12 62 70
3
3 Fall 2008ACS-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)
4
4 Fall 2008ACS-1903 Flowchart Sale > 300 Reward points = 50 + (Dollars / 25) Reward points = Dollars / 25 true false
5
5 Fall 2008ACS-1903 if statement if ( sale <= 300 ) rewardPoints = dollars / 25; else rewardPoints = 50 + dollars / 25; see RewardPoints50.java
6
6 Fall 2008ACS-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 you want for a “then” clause or an “else” clause, you can create a compound statement using curly braces {}
7
7 Fall 2008ACS-1903 if statements Can we rewrite the previous code without an else clause?
8
8 Fall 2008ACS-1903 if statements 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
9
9 Fall 2008ACS-1903 Flowchart Sale > 300 Reward points = 50 + (Dollars / 25) Reward points = Dollars / 25 true false Reward points = 150 + (Dollars / 25) Sale > 500 true false
10
10 Fall 2008ACS-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
11
11 Fall 2008ACS-1903 Relational Operators In most cases, the boolean expression, used by the if statement, uses relational operators. Relational OperatorMeaning > 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
12
12 Fall 2008ACS-1903 Boolean Expressions A boolean expression is any variable or calculation that results in a true or false condition. ExpressionMeaning 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?
13
13 Fall 2008ACS-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.javaAverageScoreWithScanner.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?
14
14 Fall 2008ACS-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
15
15 Fall 2008ACS-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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.