Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 karel_part3_ifElse_HW Conditional Statements A Mathematical look Relational operators < less than <= less than or equal to > greater than >= greater.

Similar presentations


Presentation on theme: "1 karel_part3_ifElse_HW Conditional Statements A Mathematical look Relational operators < less than <= less than or equal to > greater than >= greater."— Presentation transcript:

1

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

3 2 karel_part3_ifElse_HW Conditional Statements Some Examples Relational operators: return a Boolean value –That is, returns true or false. For example 7 < 8 returns true 17 <= 8 returns false 7 > 8 returns false 17 >= 8 returns true Math.pow(5, 0) == 1 returns true 17 != 17 returns false

4 3 karel_part3_ifElse_HW 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/or b is true, otherwise a || b is false. ! Logical NOT –!a is true if a is false and !a is false if a is true

5 4 karel_part3_ifElse_HW Order of Operations 1.! and parenthesis. 2.&& 3.|| Ties (two or more of same operator) are broken left to right

6 5 karel_part3_ifElse_HW Order of Operations 1.true && false || true = =true 2.!false && true = =true 3.!(false || !true) = =true 4.!true && (!true || !false) = =false

7 6 karel_part3_ifElse_HW Order of Operations 1)Arithmetic (*, /, +, etc.; use PEMDMAS) M is modulus 2) Comparison (>, =, ==, etc.) 3) Logical (!, &&, || ; order given on previous slide)

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

9 8 karel_part3_ifElse_HW Mathematical Operators Order 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

10 9 karel_part3_ifElse_HW Mathematical Operators some more What is modulus you ask? –% (Modulus) returns the remainder after dividing by its argument. For example o21 % 10 returns 1 because after dividing 21 by 10, 1 is the remainder! o33 % 5 returns 3 because after dividing 33 by 5, 3 is the remainder! o100 % 10 returns 0 because after dividing 100 by 10, 0 is the remainder!

11 10 karel_part3_ifElse_HW Mathematical Operators some more What is Math.pow(a, b) you ask? –Math.pow(a, b) returns the value of a^b or a raised to the b th 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

12 11 karel_part3_ifElse_HW Mathematical Operators Examples Evaluate the following o2 + 5 * 11 = 57 oMath.pow(2, 5) % 11 = 32 % 11 = 10 o100 / 2 / 5 * 3 = 50 / 5 * 3 = 10 * 3 = 30 oMath.pow(-3, 3) + 11 + 50 % 13 = -27 + 11 + 11 = -5

13 12 karel_part3_ifElse_HW Mathematical Operators Did we mention Integer Math? Integer Math (NO decimals points) 12 / 5 1 + 2 * 9 / -4 double (or Real) math (At least one decimal point) 12. / 7 1.3 + 2. / -4. Mix (double and int) math (At least one decimal point) 12 / 7 + 2.5 * 4 1.3 + 2 * 9 / -4

14 13 karel_part3_ifElse_HW Mathematical Operators Did we mention Integer Math? Integer Math 12 / 5 Is different from double (Real) math 12. / 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

15 14 karel_part3_ifElse_HW Mathematical Operators And 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

16 15 karel_part3_ifElse_HW Mathematical Operators Mixed 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

17 16 karel_part3_ifElse_HW Mathematical Operations You 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

18 17 karel_part3_ifElse_HW (Terminal) Output someStringSystem.out.println(someString); someString // outputs someString to terminal // ln: next output is on a new line someStringSystem.out.print(someString); someString // outputs someString to terminal // next output is on same line

19 18 karel_part3_ifElse_HW 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

20 19 karel_part3_ifElse_HW String Concatenation “The ” + “Cat” = “The Cat” 1 + “ Fish ” + 2 = “1 Fish 2” “ Fish ” + 2 + “!” = “ Fish 2!”

21 20 karel_part3_ifElse_HW 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

22 21 karel_part3_ifElse_HW 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

23 22 karel_part3_ifElse_HW 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

24 23 karel_part3_ifElse_HW Conditionals Revisited Nested if if( booleanExpression ) if ( anotherBooleanExpression ) System.out.println(“true”); Outputs: true if and only if booleanExpression = true anotherBooleanExpression = true

25 24 karel_part3_ifElse_HW Conditionals Revisited Nested if if( booleanExpression ) System.out.println(“true”); else if ( anotherBooleanExpression ) System.out.println(“false”); Outputs: true only if booleanExpression = true Outputs: false only if booleanExpression = false anotherBooleanExpression = true

26 25 karel_part3_ifElse_HW Conditionals Revisited Dangling else if( 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 ifs.

27 26 karel_part3_ifElse_HW Conditionals Revisited Dangling else if( booleanExpression ) if ( anotherBooleanExpression ) System.out.println(“true”) else System.out.println(“false”) Outputs: true only if booleanExpression = true anotherBooleanExpression = true Outputs: false only if booleanExpression = true anotherBooleanExpression = false

28 27 karel_part3_ifElse_HW Conditionals Revisited Dangling else if( 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

29 28 karel_part3_ifElse_HW Conditionals Revisited Dangling else if( booleanExpression ) { if ( anotherBooleanExpression ) System.out.println(“true”); } else System.out.println(“false”);

30 29 karel_part3_ifElse_HW 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 if booleanExpression = true anotherBooleanExpression = true

31 30 karel_part3_ifElse_HW 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 if booleanExpression = true anotherBooleanExpression = false

32 31 karel_part3_ifElse_HW 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 if booleanExpression = false differentBooleanExpression = true

33 32 karel_part3_ifElse_HW 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 if booleanExpression = false differentBooleanExpression = false

34 33 karel_part3_ifElse_HW WOW That was a lot!


Download ppt "1 karel_part3_ifElse_HW Conditional Statements A Mathematical look Relational operators < less than <= less than or equal to > greater than >= greater."

Similar presentations


Ads by Google