if statement if (amount

Post on 21-Dec-2015

244 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

if Statement

if (amount <= balance) balance = balance - amount;

if/else Statementif (amount <= balance)

balance = balance - amount;else

balance = balanceOVERDRAFT_PENALTY;

Syntax if (boolean expression) //use for 1 way decision

statement;

if (boolean expression) //use for 2 way decision

statement1;

else

statement2;

• Boolean expression is an expression which evaluates to true or false

• Only 1 statement allowed where statement indicated.

Relational Operators

evaluates to true if ….

a < b a is less than b

a > b a is greater than b

a <= b a is less than or equal to b

a >= b a is greater than or equal to b

a != b a is not equal to b

a == b a is equal to b

Block Statement

if (amount <= balance){

double newBalance = balance - amount; balance = newBalance;

}

Note: block allows more than one ‘statement’ to be combined, to form a new ‘statement’

Equality Testing vs. Assignment

The = = operator tests for equality:if (x = = 0) . . // if x equals zero

The = operator assigns a value to a variable:

x = 0; // assign 0 to x

Don't confuse them.

Comparing Floating-Point Numbers

Consider this code: double r = Math.sqrt(2);

double d = r * r -2; if (d == 0) System.out.println( "sqrt(2)squared minus 2 is 0"); else System.out.println( "sqrt(2)squared minus 2 is not 0 but " + d);

It prints:sqrt(2)squared minus 2 is not 0 but 4.440892098500626E-16

Don't use == to compare floating-point numbers

Comparing Floating-Point Numbers Two numbers are close to another if

|x - y| <= ε

ε is a small number such as 10-14

Example:

if ( Math.abs(x-y) < .0000000000001)

statement ;

String Comparison

Don't use = = for strings!if (input = = "Y") // WRONG!!!

Use equals method:if (input.equals("Y"))

= = tests identity, equals tests equal contents

Case insensitive test ("Y" or "y")if (input.equalsIgnoreCase("Y"))

Lexicographic Comparison if s and t are strings,

if (s < t) also does not make sense

s.compareTo(t) < 0 means: s comes before t in the dictionary

"car"comes before "cargo" "cargo" comes before "cathode"

All uppercase letters come before lowercase: "Hello" comes before "car"

Lexicographic Comparison

Object Comparison = = tests for identical object equals for identical object content Rectangle cerealBox = new

Rectangle(5, 10, 20, 30);Rectangle oatmealBox = new Rectangle(5, 10, 20, 30);Rectangle r = cerealBox;

cerealBox != oatmealBox, but cerealBox.equals(oatmealBox)

cerealBox == r Caveat: equals must be defined for the class (chap 11)

Object Comparison

The null Reference

null reference refers to no object at all

Can be used in tests: if (account == null) . . .

Use ==, not equals, to test for null

showInputDialog returns null if the user hit the cancel button:String input = JOptionPane.showInputDialog("...");if (input != null) { ... }

null is not the same as the empty string ""

Multiple Alternatives

• if (condition1) statement1;else if (condition2) statement2;else if (condition3) statement3;else statement4;

• The first matching condition is executed.

• Order matters.

Write a method which accepts a double value (a grade average), and returns the letter grade which would be awarded.

Assume 90 and above -- an A 80 and under 90 -- a B 70 and under 80 -- a C 65 and under 70 -- a D under 65 -- an F

Nested Branches• Branch inside another branch if (condition1)

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

Write a method accepts a grade point average parameter, and returns the String “Congratulations” if the GPA is 3.5 and above, but returns “Sorry” if the GPA is below 2.0. The method will return “Provide References” is the GPA does not fall into either of the above two categories.

The boolean Type

George Boole (1815-1864): pioneer in the study of logic

value of expression amount < 1000 is true or false.

boolean type: set of 2 values, true and false

Predicate Method

return type boolean

Example public boolean isOverdrawn(){

return balance < 0;

}

Use in conditionsif (myaccount.isOverdrawn())

statement;

Boolean Operators

! not

&& and (short circuited)

|| or (short circuited)

A ! A

true false

false true

Truth Tables

A B A && B

true true true

true false falsefalse Any false

A B A || B

true Any truefalse true truefalse false false

Write a method called ‘even’ which accepts one int parameter and returns true if that value is even. Otherwise false is returned.

Write a method which accepts three int parameters and returns the largest of the three.

Write an application which asks the user for 3 int values and outputs the parity (even or odd) of the sum of the three, and the largest.

private boolean FirstLetterIsAVowel() { char first = word.charAt(0);

return (first==‘a’ || first==‘e’ || first==‘i’|| first == ‘o’ || first == ‘u’ || first == ‘y’ || first == ‘A’ || first == ‘E’ || first == ‘I’ || first == ‘O’ || first == ‘U’ || first == ‘Y’);}

Problem: For the Pig Latin algorithm on the right , write a method that tests if first letter is vowel.

if (the first letter of word is a vowel) return word as it iselse modify word and return it

private boolean FirstLetterIsAVowel() { char first = word.charAt(0); if (first == ‘a’ || first == ‘e’ || first == ‘i’ || first == ‘o’ || first == ‘u’ || first == ‘y’ || first == ‘A’ || first == ‘E’ || first == ‘I’ || first == ‘O’ || first == ‘U’ || first == ‘Y’)

return true; else return false; }

First variant of the test

Second variant of the test

private boolean FirstLetterIsAVowel(){ string vowels = “aeiouyAEIOUY”; char first = word.charAt(0);

return (vowels.indexOf(first) > -1);}

Third variant of the test

public void translateWord() { if (FirstLetterIsAVowel() ) return (word); else return(word.substring(1) + word.charAt(0) + “ay”);}

Method translateWord()

Boolean Variables

boolean married;

married = input.equals("M"); //set to boolean value

if (married == true)

statement;

else

statement;

Switch statement

switch ( test expression ) {

case expression1:

statement1;

case expression2:

statement2;

…..

default:

default statement;

}

** matches and executes down

** expressions must be of primitive non-decimal type

switch ( test expression ) {

case expression1:

statement1;

break; //to avoid fall thru

case expression2:

statement2;

break;

default:

default statement;

}

Write a method called “twelveDays” which accepts one in parameter, (a value which will be between 1 and 12 inclusive), and returns a string containing the item given on that day in the song “The Twelve Days of Christmas”.

Write an application which prints out the lyrics to this song, using the method twelveDays.

top related