Starting Out With Java Chapter 4

Download Report

Transcript Starting Out With Java Chapter 4

if-else-if Statements
• if-else-if statements can become very
complex.
• Imagine the following decision set.
if it is very cold, wear a heavy coat,
else, if it is chilly, wear a light jacket,
else, if it is windy wear a windbreaker,
else, if it is hot, wear no jacket.
Starting Out With Java 5
Control Structures to Objects
Copyright © 2005,
Pearson Addison-Wesley. All rights
reserved.
Chapter 3
Slide #1
if-else-if Statements
if (expression)
statement or block
else
if (expression)
statement or block
// Put as many else ifs as needed here
else
statement or block
• Care must be used since else statements match up with the
immediately preceding unmatched if statement.
• Example: TestResults.java
Starting Out With Java 5
Control Structures to Objects
Copyright © 2005,
Pearson Addison-Wesley. All rights
reserved.
Chapter 3
Slide #2
if-else-if Flowchart
Starting Out With Java 5
Control Structures to Objects
Copyright © 2005,
Pearson Addison-Wesley. All rights
reserved.
Chapter 3
Slide #3
Nested if Statements
• If an if statement appears inside of another
if statement (single or block) it is called a
nested if statement.
• The nested if is only executed if the if
statement it is in results in a true condition.
• Nested if statements can get very complex,
very quickly.
• Example: LoanQualifier.java
Starting Out With Java 5
Control Structures to Objects
Copyright © 2005,
Pearson Addison-Wesley. All rights
reserved.
Chapter 3
Slide #4
Nested if Statement Flowcharts
No
“You must earn
at least…”
Yes
Salary >=
30000?
No
yearsOnJob
>= 2
“...you must have
been on job 2 years…”
Starting Out With Java 5
Control Structures to Objects
Copyright © 2005,
Pearson Addison-Wesley. All rights
reserved.
Yes
“You qualify....
Chapter 3
Slide #5
if-else Matching
This else
matches
with this
if.
if (employed == 'y')
{
if (recentGrad == 'y')
{
System.out.println("You qualify for the special
interest rate.");
}
else
{
This else
matches
with this
if.
System.out.println("You must be a recent college
graduate to qualify.");
}
}
else
{
System.out.println("You must be employed to
qualify.");
Copyright © 2005,
Starting Out With Java 5
}
Control Structures to Objects
Pearson Addison-Wesley. All rights
reserved.
Chapter 3
Slide #6
Logical Operators
• Java provides two binary logical operators
(&& and ||) that are used to combine
boolean expressions.
• Java also provides one unary (!) logical
operator to reverse the truth of a boolean
expression.
Starting Out With Java 5
Control Structures to Objects
Copyright © 2005,
Pearson Addison-Wesley. All rights
reserved.
Chapter 3
Slide #7
Logical Operators
Operator
Meaning
Effect
AND
Connects two boolean expressions into one. Both
expressions must be true for the overall
expression to be true.
OR
Connects two boolean expressions into one. One
or both expressions must be true for the overall
expression to be true. It is only necessary for one
to be true, and it does not matter which one.
NOT
The ! operator reverses the truth of a boolean
expression. If it is applied to an expression that is
true, the operator returns false. If it is applied to
an expression that is false, the operator returns
true.
&&
||
!
Starting Out With Java 5
Control Structures to Objects
Copyright © 2005,
Pearson Addison-Wesley. All rights
reserved.
Chapter 3
Slide #8
The && Operator
• The logical AND operator (&&) takes two
operands that must both be boolean expressions.
• The resulting combined expression is true iff (if
and only if) both operands are true.
• Example: LogicalAnd.java
Expression 1
Expression 2
Expression1 && Expression2
true
false
false
false
true
false
false
false
false
true
true
true
Starting Out With Java 5
Control Structures to Objects
Copyright © 2005,
Pearson Addison-Wesley. All rights
reserved.
Chapter 3
Slide #9
The || Operator
• The logical OR operator (||) takes two operands
that must both be boolean expressions.
• The resulting combined expression is false iff (if
and only if) both operands are false.
• Example: LogicalOr.java
Expression 1
Expression 2
Expression1 || Expression2
true
false
true
false
true
true
false
false
false
true
true
true
Starting Out With Java 5
Control Structures to Objects
Copyright © 2005,
Pearson Addison-Wesley. All rights
reserved.
Chapter 3
Slide #10
The ! Operator
• The ! operator performs a logical NOT operation.
• If an expression is true, !expression will be false.
if (!(temperature > 100))
System.out.println(“Below the maximum temperature.");
• If temperature
evaluates to false, then the
output statement will be run.
> 100
Expression 1
!Expression1
true
false
false
true
Starting Out With Java 5
Control Structures to Objects
Copyright © 2005,
Pearson Addison-Wesley. All rights
reserved.
Chapter 3
Slide #11
Order of Precedence
Order of
Precedence
Operators
1
(unary negation) !
2
*/%
3
+-
4
< > <= >=
5
== !=
6
&&
Logical AND
7
||
Logical NOT
8
Starting Out With Java 5
Control Structures to Objects
= += -=
*= /= %=
Description
Unary negation, logical NOT
Multiplication, Division, Modulus
Addition, Subtraction
Less-than, Greater-than, Less-than or
equal to, Greater-than or equal to
Is equal to, Is not equal to
Assignment and combined assignment
operators.
Copyright © 2005,
Pearson Addison-Wesley. All rights
reserved.
Chapter 3
Slide #12
Comparing String Objects
• In most cases, you cannot use the relational
operators to compare two String objects.
• Reference variables contain the address of the
object they represent.
• Unless the references point to the same object, the
relational operators will not return true.
• Example: StringCompare.java
• Example: StringCompareTo.java
Starting Out With Java 5
Control Structures to Objects
Copyright © 2005,
Pearson Addison-Wesley. All rights
reserved.
Chapter 3
Slide #13
Ignoring Case in String
Comparisons
• In the String class the equals and
compareTo methods are case sensitive.
• In order to compare two String objects that
might have different case, use:
• equalsIgnoreCase, or
• compareToIgnoreCase
• Example: SecretWord.java
Starting Out With Java 5
Control Structures to Objects
Copyright © 2005,
Pearson Addison-Wesley. All rights
reserved.
Chapter 3
Slide #14
void Methods and ValueReturning Methods
• A void method is one that simply performs a
task and then terminates.
System.out.println(“Hi!”);
• A value-returning method not only performs a
task, but also sends a value back to the code
that called it.
int number = Integer.parseInt(“700”);
Starting Out With Java 5
Control Structures to Objects
Copyright © 2005 Pearson Addison-Wesley.
All rights reserved.
Chapter 5
Slide #15
Two Parts of Method Declaration
header
public static void displayMesssage()
{
System.out.println(“Hello”);
body
}
Starting Out With Java 5
Control Structures to Objects
Copyright © 2005 Pearson Addison-Wesley.
All rights reserved.
Chapter 5
Chapter
Slide #165
Slide #16
Parts of a Method Header
Method
Modifiers
Return
Type
Method
Name
Parentheses
public static void displayMessage ()
{
System.out.println(“Hello”);
}
Starting Out With Java 5
Control Structures to Objects
Copyright © 2005 Pearson Addison-Wesley.
All rights reserved.
Chapter 5
Slide #17
Passing 5 to the
displayValue Method
displayValue(5);
The argument 5 is copied into the
parameter variable num.
public static void displayValue(int num)
{
System.out.println(“The value is “ + num);
}
The method will display
Starting Out With Java 5
Control Structures to Objects
The value is 5
Copyright © 2005 Pearson Addison-Wesley.
All rights reserved.
Chapter 5
Slide #18
Passing Multiple Arguments
The argument 5 is copied into the num1 parameter.
The argument 10 is copied into the num2 parameter.
showSum(5,10);
NOTE: Order matters!
public static void showSum(double num1, double num2)
{
double sum;
//to hold the sum
sum = num1 + num2;
System.out.println(“The sum is “ + sum);
}
Starting Out With Java 5
Control Structures to Objects
Copyright © 2005 Pearson Addison-Wesley.
All rights reserved.
Chapter 5
Slide #19