250 likes | 384 Vues
if statement formats. if( condition ) statement (s) if ( condition ) statement(s) 1 else statement(s). Comparing Values: Relational Operators. Relational operators compare values. Multiple Alternatives: Sequences of Comparisons. if ( condition1 ) statement1 ;
E N D
if statement formats if(condition) statement(s) if (condition) statement(s)1 else statement(s)
Comparing Values: Relational Operators • Relational operators compare values
Multiple Alternatives: Sequences of Comparisons • 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: Nested Branches • if (condition1) • { • if (condition1a) • statement1a; • else • statement1b; • } • else • statement2;
Using Boolean Expressions: The Boolean Operators • && and • || or • ! not • if (0 < amount && amount < 1000) . . . • if (input.equals("S") || input.equals("M")) . . .
while loop false Expression true body statement 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 Will be true or false Control variable Changed as last statement
Calculating the Growth of an Investment • Invest $10,000, 5% interest, compounded annually
ch06/invest1/InvestmentRunner.java 01: /** 02: This program computes how long it takes for an investment 03: to double. 04: */ 05: public class InvestmentRunner 06: { 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: }
while (balance < targetBalance) { years++; double interest = balance * rate / 100; balance = balance + interest; } Loop Loop Body
publicclass Investment 06:{ 07:publicInvestment(doubleaBalance,doubleaRate) 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
ch06/invest1/Investment.java (cont.) : /** 45: Gets the number of years this investment has accumulated interest. 47: @return the number of years since the start of the investment 48: */ 49: public intgetYears() 50: { 51: return years; 52: } 54: private double balance; 55: private double rate; 56: private int years; 57: }
boolean variable or expression: • boolean expression • returns an integer value;
Comparing Values: Relational Operators • Relational operators compare values
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--; }
TheforStatement for (initialization; condition; update) {statement (s) }
TheforStatement for (inti = 1; i <= n; i++) { double interest = balance * rate / 100; balance = balance + interest; }
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) false 0 < myState.compareTo(yourState) true myState.equals(“Texas”) true 0 > myState.compareTo(“texas”) true
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.