1k likes | 1.21k Views
Chapter 3 Decision Structures. Contents. The if Statement The if-else Statement The if-else-if Statement Nested if Statement Logical Operators Comparing String Objects More about Variable Declaration and Scope. Contents (Cont’d). The Conditional Operators
E N D
Contents • The if Statement • The if-else Statement • The if-else-if Statement • Nested if Statement • Logical Operators • Comparing String Objects • More about Variable Declaration and Scope
Contents (Cont’d) • The Conditional Operators • The Switch Statement • Creating Objects with the DecimalFormat Class • The printf Method
1. The if Statement • Problem: • Write a program to calculate user’s average of 3 test scores. If the average is greater than 95, the program congratulates the users on obtaining a high score.
1. The if Statement (Cont’d) • Simple decision structure logic
1. The if Statement (Cont’d) if(BooleanExpression) statement; • The BooleanExpression must be a boolean expression • A boolean expression is either true or false • If the BooleanExpression is true, the very next statement is executed. Otherwise, it is skipped. • The statement is conditionally executed because it only executes under the condition that the expression in the parentheses is true.
Using Relational Operators to Form Conditions • Typically, the boolean expression is formed with a relational operator (binary operator.
Using Relational Operators to Form Conditions (Cont’d) • Assuming that a is 4, b is 6, and c is 4 • b>=atrue • c<=a true • a>=5 false • c==6 false • b!=6 false
Programming Style and the if Statement • Two important style rules • The conditionally executed statement should appear on the line after the if statement. • The conditionally executed statement should be indented on level from the if statement. if(value>32) System.out.println(“Invalid number”); if(value>32) System.out.println(“Invalid number”);
Be Careful with Semicolons No semicolon here if(BooleanExpression) statement; int x = 0, y = 10; if(x > y); System.out.println(x + “is greater than “ + y); Semicolon goes here It will always execute.
Multiple Conditionally Executed Statements • Enclosing a group of statements by braces if(sales > 5000) { bonus = 500.0; commissionRate = 0.12; dayOff += 1; } These three statements are executed if sales is greater than 5000
Comparing Characters • Using the relational operators to test character data as well as number • Assuming ch is a char variable: • Compare ch to the character ‘A’: if(ch==‘A’) System.out.println(“The letter is A.”); • Compare the ch is not equal to ‘A’: if(ch!=‘A’) System.out.println(“Not the letter is A.”);
Comparing Characters (Cont’d) • In Unicode, letters are arranged in alphabetic order: • ‘A’ comes before ‘B’, the numeric code of ‘A’ (65) is less than the code of ‘B’ (66). • ‘A’ < ‘B’ true • In Unicode, the uppercase letters come before the lowercase letter.
2. The if-else statement • Problem • Write a program to get two numbers and divide the first number by the second number.
Logic of the if-else Statement (Cont’d) • The if-else statement will execute one group of statement if its boolean expression is true, or another group if its boolean expression is false. if(BooleanExpression) statement or block else statement or block
3. The if-else-if Statement • Problem • Write a program to ask the user to enter a numeric test score. Display a letter grade (A, B, C, D, or F) for the score. • score < 60 F • 60 <= score < 70 D • 70 <= score < 80 C • 80 <= score < 90 B • 90 <= score <= 100 A • score > 100 Invalid score
Logic of the if-else-if Statement Logic of the if-else-if Statement
3. The if-else-if Statement • The if-else-if statement is a chain of if-else statements. Each statement in the chain performs its test until one of the tests is found to be true. if(BooleanExpression) statement or block else if(BooleanExpression) statement or block // //Put as many else if statement as needed here // else statement or block
4. Nested if Statement • Problem • Write a Java program to determine whether a bank customer qualifies for a loan. To qualify, a customer must earn at least $30,000 per year, and must have been on his or her current job for at least two years.
4. Nested if Statement • Input • User’s annual salary • Number of years at the current job
4. Nested if Statement (Cont’d) • An if statement appears inside another if statement, it is considered nested. • The rule for matching else clauses with if clauses is this: • An else clause goes with the closet previous if clause that doesn’t already have its own else clause.
5. Logical Operators • Logical operators connect two or more relational expressions into one or reverse the logic of an expression. • Java provides • two binary logical operators • && : AND • || : OR • one unary logical operator • ! : NOT
The && Operator • The && performs short-circuit evaluation • If the expression on the left side of the && operator is false, the expression on the right side will not be checked.
The && Operator (Cont’d) • A different version of the LoanQualifier program
The || Operator • The || performs short-circuit evaluation • If the expression on the left side of the || operator is true, the expression on the right side will not be checked.
The || Operator • Problem • Write a Java program to determine whether a bank customer qualifies for a loan. To qualify, a customer must earn at least $30,000 per year, OR must have been on his or her current job for at least two years.
The || Operator (Cont’d) • Input • User’s annual salary • Number of years at the current job
The ! Operator • The ! Operator performs a logical NOT operation • !(x > 1000) x <= 1000
The Precedence and Associativity of Logical Operators • The logical operators have orders of precedence and associativity. • The precedence of the logical operators, from highest to lowest ! && || • !(x > 2) • Applies the ! operator to the expression x > 2 • !x > 2 • Causes a compiler error
The Precedence and Associativity of Logical Operators (Cont’d) • The && and || operators rank lower in precedence than the relational operators (a > b) && (x < y)a > b && x < y (x == y) || (b > a) x == y || b > a • The logical operators evaluate their expression from left to right • a < b || y == z • a < b is evaluated before y == z • a < b || y == z && m > j • y == z is evaluated first because the && operator has higher precedence than || • Is equivalent to the following (a < b) || ((y == z) && (m > j))
The Precedence and Associativity of Logical Operators (Cont’d)
Checking Numeric Ranges with Logical Operators • Determining whether a numeric value is within a specific range of values • Using the && operator x >= 20 && x <= 40 • Determining whether a numeric value is outside a specific range of values • Using the || operator x < 20 || x > 40
6. Comparing String Objects • We cannot use relational operators to compare String objects. Instead we must use a String method. String name1 = “Marks”; String name2 = “Mary”; • name1 == name2 will be false because the variables name1 and name2 reference different objects.
6. Comparing String Objects if(name1 == name2)