Download - if statement formats
if statement formats
if(condition) statement (s)
if (condition) statement(s)1 else statement(s)
• Relational operators compare values
Java Math Notation Description
> > Greater than
>= ≥ Greater than or equal
< < Less than
<= ≤ Less than or equal
== = Equal
!= ≠ Not equal
Comparing Values: Relational Operators
if (condition1) statement1; else if (condition2) statement2; . . . else statement4;
• Order matters
if (n1 >= 0) str = “n1 is greater than 0"; else if (n1 > = 3.5) str = “Never tested! Why??"; . . .
Multiple Alternatives: Sequences of Comparisons
if (condition1) { if (condition1a) statement1a; else statement1b; } else statement2;
Multiple Alternatives: Nested Branches
• && and
• || or
• ! not
• if (0 < amount && amount < 1000) . . . • if (input.equals("S") || input.equals("M")) . . .
Using Boolean Expressions: The Boolean Operators
Sorting Three Numbers
Looping
false
true
bodystatement
Expression
while loop
while (boolean value or expression){ statement(s);}
While example: (sentinel controlled loop)
final int LIMIT = 10;int count = 1;while (count <= limit){ System.out.println (count); count = count + 1;}
Control variable
Control variableChanged as last statement
Will be true or false
Year Balance
0 $10,000
1 $10,500
2 $11,025
3 $11,576.25
4 $12,155.06
5 $12,762.82
• Invest $10,000, 5% interest, compounded annually
Calculating the Growth of an Investment
01: /**02: This program computes how long it takes for an investment03: to double.04: */05: public class InvestmentRunner06: {07: public static void main(String[] args)08: {09: final double INITIAL_BALANCE = 10000;10: final double RATE = 5;11: Investment invest = new Investment(INITIAL_BALANCE, RATE);12: invest.waitForBalance(2 * INITIAL_BALANCE);13: int years = invest.getYears();14: System.out.println("The investment doubled after "15: + years + " years");16: } 17: }
ch06/invest1/InvestmentRunner.java
while (balance < targetBalance) { years++; double interest = balance * rate / 100; balance = balance + interest;
}
Loop Body
Loop
public class Investment06: {07: public Investment(double aBalance, double aRate)14: {15: balance = aBalance;16: rate = aRate;17: years = 0;18: }
public void waitForBalance(double targetBalance)26: {27: while (balance < targetBalance)28: {29: years++; 30: double interest = balance * rate / 100;31: balance = balance + interest;32: }33: } 39: public double getBalance()40: {41: return balance;42: } 44
: /**45: Gets the number of years this investment has accumulated interest.47: @return the number of years since the start of the investment48: */49: public int getYears()50: {51: return years;52: }54: private double balance;55: private double rate;56: private int years;57: }
ch06/invest1/Investment.java (cont.)
boolean variable or expression:
boolean expression
returns an integer value;
• Relational operators compare values
Java Math Notation Description
> > Greater than
>= ≥ Greater than or equal
< < Less than
<= ≤ Less than or equal
== = Equal
!= ≠ Not equal
Comparing Values: Relational Operators
In class lab
Write a method to:◦Receive an int number in a method called count
◦Using a while loop, print asterisks across the console, the number of asterisks to be the passed number
◦Upload to the DropBox, WhileLoop
Nested Loops
The body of the loop contains another loop
◦Each time through the outer loop, the inner loop goes through its full set of iterations
Nested loop design Example
X=6;while (x >=4) { y = 3; while (y <=6) { y++; System.out.println(y +
“ “ + x); } x--; }
for (initialization; condition; update)
{ statement (s)
}
The for Statement
for (int i = 1; i <= n; i++) { double interest = balance * rate / 100; balance = balance + interest; }
The for Statement
22
String methodsreview!
Method Parameter Returns Operation Performed
Name Type
equals String boolean
compareTo String int
Tests for equality of string contents.
Returns 0 if equal, a positive integer if the string in the parameter comes before the string associated with the method and a negative integer if the parameter comes after it.
String myState; String yourState;
myState = “Texas”;yourState = “Maryland”;
EXPRESSION VALUE
myState.equals(yourState) false0 < myState.compareTo(yourState) true
myState.equals(“Texas”) true
0 > myState.compareTo(“texas”) true
24
More String Methods
Method Parameter Returns Operation Performed
Name Type
toLowerCase none String
toUpperCase none String
Returns a new identical
string, except the
characters are all
lowercase.
Returns a new identical
string, except the
characters are all
uppercase.