if statement formats

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

Upload: fran

Post on 07-Jan-2016

43 views

Category:

Documents


0 download

DESCRIPTION

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 ; - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: if  statement formats

if statement formats

if(condition) statement (s)

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

Page 2: if  statement formats

• 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

Page 3: if  statement formats

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

Page 4: if  statement formats

if (condition1) { if (condition1a) statement1a; else statement1b; } else statement2;

Multiple Alternatives: Nested Branches

Page 5: if  statement formats

• &&  and

• ||  or

• !   not

• if (0 < amount && amount < 1000) . . . • if (input.equals("S") || input.equals("M")) . . .

Using Boolean Expressions: The Boolean Operators

Page 6: if  statement formats

Sorting Three Numbers

Page 7: if  statement formats

Looping

Page 8: if  statement formats

false

true

bodystatement

Expression

while loop

while (boolean value or expression){ statement(s);}

Page 9: if  statement formats

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

Page 10: if  statement formats

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

Page 11: if  statement formats

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

Page 12: if  statement formats

while (balance < targetBalance) { years++; double interest = balance * rate / 100; balance = balance + interest;

}

Loop Body

Loop

Page 13: if  statement formats

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

Page 14: if  statement formats

: /**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.)

Page 15: if  statement formats

boolean variable or expression:

boolean expression

returns an integer value;

Page 16: if  statement formats

• 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

Page 17: if  statement formats

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

Page 18: if  statement formats

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

Page 19: if  statement formats

Nested loop design Example

X=6;while (x >=4) { y = 3; while (y <=6) { y++; System.out.println(y +

“ “ + x); } x--; }

Page 20: if  statement formats

for (initialization; condition; update)

{ statement (s)

}

The for Statement

Page 21: if  statement formats

for (int i = 1; i <= n; i++) { double interest = balance * rate / 100; balance = balance + interest; }

The for Statement

Page 22: if  statement formats

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.

Page 23: if  statement formats

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

Page 24: if  statement formats

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.