1 karel_part3_ifelse_hw conditional statements a mathematical look relational operators < less...

33
karel_part3_ifElse_HW 1 Conditional Statements A Mathematical look • Relational operators < less than <= less than or equal to > greater than >= greater than or equal to == equals != not equals

Upload: warren-floyd

Post on 13-Dec-2015

223 views

Category:

Documents


1 download

TRANSCRIPT

karel_part3_ifElse_HW 1

Conditional StatementsA Mathematical look

• Relational operators< less than

<= less than or equal to

> greater than

>= greater than or equal to

== equals

!= not equals

karel_part3_ifElse_HW 2

Conditional StatementsSome Examples

• Relational operators: return a Boolean value– That is, returns true or false. For example7 < 8 returns true17 <= 8 returns false7 > 8 returns false17 >= 8 returns trueMath.pow(5, 0) == 1 returns true17 != 17 returns false

karel_part3_ifElse_HW 3

Boolean Operators&& logical AND

– a && b is true only if both a is true and b is true, otherwise a && b is false

|| logical OR– a || b is true either a is true and/orb is true, otherwise a || b is false.

! Logical NOT– !a is true if a is false and!a is false if a is true

karel_part3_ifElse_HW 4

Order of Operations

1. ! and parenthesis.

2. &&

3. ||

Ties (two or more of same operator) are broken left to right

karel_part3_ifElse_HW 5

Order of Operations1. true && false || true =

= true

2. !false && true == true

3. !(false || !true) == true

4. !true && (!true || !false) == false

karel_part3_ifElse_HW 6

Order of Operations

1) Arithmetic (*, /, +, etc.)

2) Comparison (>, <, >=, ==, etc.)

3) Logical (!, &&, ||)

karel_part3_ifElse_HW 7

Mathematical Operators

* multiplication/ division+ addition- subtraction% modulus (aka remainder)Math.pow(a, b) a^b

karel_part3_ifElse_HW 8

Mathematical OperatorsOrder of Operations

• These first: Left to right* multiplication/ division% modulus (aka remainder)Math.pow(a, b) a^b

• These next: left to right+ addition- subtraction

karel_part3_ifElse_HW 9

Mathematical Operatorssome more

• What is modulus you ask?– % (Modulus) returns the remainder after dividing by its argument. For example

o 21 % 10 returns 1 because after dividing 21 by 10, 1 is the remainder!

o 33 % 5 returns 3 because after dividing 33 by 5, 3 is the remainder!

o 100 % 10 returns 0 because after dividing 100 by 10, 0 is the remainder!

karel_part3_ifElse_HW 10

Mathematical Operatorssome more

• What is Math.pow(a, b) you ask?– Math.pow(a, b) returns the value of a^b or a raised to the bth power. For example:

– Math.pow(2, 3) returns• 8 because 2^3 is 8

– Math.pow(2, 4) returns• 16 because 2^4 is 16

– Math.pow(121, 0) returns• 1 because 121^0 is 1

– Math.pow(-5, 3)• returns -125 because (-5)^3 is -125

karel_part3_ifElse_HW 11

Mathematical OperatorsExamples

• Evaluate the following

o 2 + 5 * 11 =57

o Math.pow(2, 5) % 11 =32 % 11 = 10

o 100 / 2 / 5 * 3 =50 / 5 * 3 = 10 * 3 = 30

o Math.pow(-3, 3) + 11 + 50 % 13 =-27 + 11 + 11 = -5

karel_part3_ifElse_HW 12

Mathematical OperatorsDid we mention Integer Math?

• Integer Math (NO decimals points)12 / 51 + 2 * 9 / -4

• double (or Real) math (At least one decimal point) 12. / 71.3 + 2. / -4.

• Mix (double and int) math (At least one decimal point) 12 / 7 + 2.5 * 41.3 + 2 * 9 / -4

karel_part3_ifElse_HW 13

Mathematical OperatorsDid we mention Integer Math?

• Integer Math12 / 5

• Is different from double (Real) math12. / 7

• Integer math is when all values are int(egers). All fractional parts are discarded!– Therefore: 12 / 5 = 2– And 7 * 3 / 5 = 21 / 5 = 4 – And 7 * (3 / 5) = 7 * 0 = 0

karel_part3_ifElse_HW 14

Mathematical OperatorsAnd Real Math?

• double (or Real) math– 12. / 7 = 1.714285714…– 5.5 * 2 = 11.0– 5.5 * 2 % 3. = 2.0– Math.pow(3, 3) = 27.0 Special note: math.pow always returns a double

– Math.pow(3, 3) / 2 = 13.5

karel_part3_ifElse_HW 15

Mathematical OperatorsMixed Math –int and double

• These are a little tricky• 1.3 + 2 * 9 / -4 = -2.7– Why you ask?Everything is Integer Math until–1.3 is added. Order of operations forces 2*9/-4= -4 before adding 1.3

• 12 / 7 + 2.5 * 4= 1 + 10. = 11.

• 1. / 5 + 2. * 7 / (2 – 1./3)= 0.2 + 14. / (5/3.) = .2 + (14.*3/5.)= .2 + 42./5. = .2 + 8.4 = 8.6

• Math.pow(2, 14/5. + 13/5. + 3./5)= Math.pow(2, 2.8 + 2.6 + .6) = 2^(6) = 64.0

karel_part3_ifElse_HW 16

Mathematical OperationsYou try!

• 10. + 21. / 5= 10.+ 4.2 = 14.2

• 12 + Math.pow(4, 1/2)= 12 + Math.pow(4,0) = 12 + 1. = 13.

• 12 + Math.pow(4, 3/2. + 1./2)= 12 + Math.pow(4,2) = 12 + 16. = 28.

• 12 + 68 % 6 * 3= 12 + 2 * 3 = 12 + 6 = 18

• 7 * -5 + 11 * 9 % 5= -35 + 99 % 5 = -35 + 4 = -31

• 7 + 1 / Math.pow(3, 2)= 7 + 1 / 9. = 7.111111

karel_part3_ifElse_HW 17

(Terminal) Output

• System.out.println(someStringsomeString);/ / outputs someStringsomeString to terminal/ / ln: next output is on a new line

• System.out.print(someStringsomeString);/ / outputs someStringsomeString to terminal/ / next output is on same line

karel_part3_ifElse_HW 18

System.out.print(ln)

System.out.print(“The ”);System.out.print(“cat in the ”);System.out.print(“hat”);

/ / generates The cat in the hat

System.out.print(“One ”);System.out.println(“Fish”);System.out.print(“Two ”);System.out.println(“Fishies”);

/ / generates One Fish/ / Two Fishies

karel_part3_ifElse_HW 19

String Concatenation

“The ” + “Cat” = “The Cat”

1 + “ Fish ” + 2 = “1 Fish 2”

“ Fish ” + 2 + “!” = “Fish 2!”

karel_part3_ifElse_HW 20

System.out.print(ln)Example

System.out.print(1 + “cat in ”);System.out.println(1 + “ large”);System.out.print(“red”+“hat”); generates/*Line 1*/ 1cat in 1 large/*Line 2*/ redhat

karel_part3_ifElse_HW 21

System.out.print(ln)Example

System.out.print(123 + “ + ”);System.out.print(456 + “ =”);System.out.println(“ “ + 579);System.out.println(5 + “ + 2”);System.out.println(“ = 1” + 56); generates/*Line 1*/ 123 + 456 = 579/*Line 2*/ 5 + 2/*Line 3*/ = 156

karel_part3_ifElse_HW 22

System.out.print(ln)Escape sequences?

• What if We want to output a string with “, ‘ or \?

• Use Escape sequences as follows:– \” to output “

System.out.print(“he said \”NO\”!”);Generates: he said ”NO”!

System.out.print(“\”they\’ve rallied\” again”);Generates: ”they’ve rallied” again

System.out.print(“use \\\\ to to output \\“);Generates: use \\ to to output \

Special Note: use \n to start a new line!System.out.print(“\\ it\’s time\“new\nline why”);

Generates: \ it’s time“new line why

karel_part3_ifElse_HW 23

Conditionals Revisited

•Nested ifif( booleanExpression ) if ( anotherBooleanExpression ) System.out.println(“true”);

• Outputs: true if and only ifbooleanExpression = trueanotherBooleanExpression = true

karel_part3_ifElse_HW 24

Conditionals Revisited

• Nested ifif( booleanExpression ) System.out.println(“true”);else if ( anotherBooleanExpression ) System.out.println(“false”);

• Outputs: true only ifbooleanExpression = true

• Outputs: false only ifbooleanExpression = falseanotherBooleanExpression = true

karel_part3_ifElse_HW 25

Conditionals Revisited

•Dangling elseif( booleanExpression ) if ( anotherBooleanExpression ) System.out.println(“true”)

else System.out.println(“false”)

• The question is, to which if is the else attached?

• The answer is the most recent unused else.

karel_part3_ifElse_HW 26

Conditionals Revisited•Dangling else

if( booleanExpression ) if ( anotherBooleanExpression ) System.out.println(“true”) else System.out.println(“false”)

• Outputs: true only ifbooleanExpression = true

anotherBooleanExpression = true

• Outputs: false only ifbooleanExpression = trueanotherBooleanExpression = false

karel_part3_ifElse_HW 27

Conditionals Revisited

•Dangling elseif( booleanExpression ) if ( anotherBooleanExpression ) System.out.println(“true”)else

System.out.println(“false”)

• How is the if structured to have the else associated with the first if– Use brackets!– Indenting will not work!– This is how I will trick you

karel_part3_ifElse_HW 28

Conditionals Revisited

•Dangling elseif( booleanExpression )

{ if ( anotherBooleanExpression ) System.out.println(“true”)

}else System.out.println(“false”)

karel_part3_ifElse_HW 29

Conditionals Revisited• Nested if’s

if( booleanExpression ) if ( anotherBooleanExpression ) System.out.println(“true - true”) else System.out.println(“true - false”)else if ( differentBooleanExpression ) System.out.println(“false - true”) else System.out.println(“false - false”)

• Outputs: true - true ifbooleanExpression = trueanotherBooleanExpression = true

karel_part3_ifElse_HW 30

Conditionals Revisited• Nested if’s

if( booleanExpression ) if ( anotherBooleanExpression ) System.out.println(“true - true”) else System.out.println(“true - false”)else if ( differentBooleanExpression ) System.out.println(“false - true”) else System.out.println(“false - false”)

• Outputs: true - false ifbooleanExpression = trueanotherBooleanExpression = false

karel_part3_ifElse_HW 31

Conditionals Revisited• Nested if’s

if( booleanExpression ) if ( anotherBooleanExpression ) System.out.println(“true - true”) else System.out.println(“true - false”)else if ( differentBooleanExpression ) System.out.println(“false - true”) else System.out.println(“false - false”)

• Outputs: false - true ifbooleanExpression = false differentBooleanExpression = true

karel_part3_ifElse_HW 32

Conditionals Revisited• Nested if’s

if( booleanExpression ) if ( anotherBooleanExpression ) System.out.println(“true - true”) else System.out.println(“true - false”)else if ( differentBooleanExpression ) System.out.println(“false - true”) else System.out.println(“false - false”)

• Outputs: false - false ifbooleanExpression = falsedifferentBooleanExpression = false

karel_part3_ifElse_HW 33

WOW

That was a lot!