1 / 24

if statement formats

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 ;

fran
Download Presentation

if statement formats

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. if statement formats if(condition) statement(s) if (condition) statement(s)1 else statement(s)

  2. Comparing Values: Relational Operators • Relational operators compare values

  3. 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??"; • . . .

  4. Multiple Alternatives: Nested Branches • if (condition1) • { • if (condition1a) • statement1a; • else • statement1b; • } • else • statement2;

  5. Using Boolean Expressions: The Boolean Operators • &&  and • ||  or • !   not • if (0 < amount && amount < 1000) . . . • if (input.equals("S") || input.equals("M")) . . .

  6. Sorting Three Numbers

  7. Looping

  8. while loop false Expression true body statement while (boolean value or expression) { statement(s); }

  9. 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

  10. Calculating the Growth of an Investment • Invest $10,000, 5% interest, compounded annually

  11. 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: }

  12. while (balance < targetBalance) { years++; double interest = balance * rate / 100; balance = balance + interest; } Loop Loop Body

  13. 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

  14. 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: }

  15. boolean variable or expression: • boolean expression • returns an integer value;

  16. Comparing Values: Relational Operators • Relational operators compare values

  17. 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

  18. 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

  19. Nested loop design Example X=6; while (x >=4) { y = 3; while (y <=6) { y++; System.out.println(y + “ “ + x); } x--; }

  20. TheforStatement for (initialization; condition; update) {statement (s) }

  21. TheforStatement for (inti = 1; i <= n; i++) { double interest = balance * rate / 100; balance = balance + interest; }

  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.

  23. 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

  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.

More Related