Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning <less than <=less than or equal >greater than >=greater than.

Similar presentations


Presentation on theme: "© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning <less than <=less than or equal >greater than >=greater than."— Presentation transcript:

1 © 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning <less than <=less than or equal >greater than >=greater than or equal Equality OperatorMeaning ==equal != not equal

2 © 2005 Lawrenceville Press Slide 2 Chapter 5 The if Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition is true  The condition is a Boolean expression  For example, the statement if (x == 5) { y = 20; } assigns the value 20 to y only if x is equal to 5.

3 © 2005 Lawrenceville Press Slide 3 Chapter 5 Roundoff Error  Occurs when a floating point number cannot be exactly represented in binary notation by the computer  Can cause semantic errors in an if statement. For example, the condition in the statement if ((4.80 * 100 - 480) == 0) { System.out.println("Zero."); } does not evaluate to true because 4.80 can be represented exactly in binary.

4 © 2005 Lawrenceville Press Slide 4 Comparing Objects Objects can be tested for equality, but not relationship. x.equals(y) Is true if the objects that x and y represent are “equal.” Every Java class supports the equals method. The definition of equals varies from class to class.

5 © 2005 Lawrenceville Press Slide 5 String comparisons Strings are objects, so don’t use == operator Use:str1.compareTo(str2);Returns: <0 if str1 < str2 <0 if str1 < str2 ==0 if str1==str2 >0 if str1 > str2 >0 if str1 > str2

6 © 2005 Lawrenceville Press Slide 6 How Strings really compared… Java uses lexicogrpahic order From the word lexicography – the process of compiling a dictionary. ‘a’ comes before ‘m’ ‘1’ comes before ‘7’ ‘ab’ comes before ‘above’ ‘ab’ comes before ‘above’ In the ASCII code ‘1’ comes before ‘A’ which comes before ‘a’

7 © 2005 Lawrenceville Press Slide 7 Quick Review! 1. To determine whether one number is greater than or equal to another, we would use a ________ type operator. 2. The == and != symbols are called _________ operators. 3. Why is it not a good idea to test whether two doubles are equal? 4. What method is used to test whether two objects are equal? 5. What method is used to test whether two Strings are equal?

8 © 2005 Lawrenceville Press Slide 8 Chapter 5 The if-else Statement Contains an else clause that is executed when the if condition evaluates to false. For example, the statement if (x == 5) { y = 20; } else { y = 10; } assigns the value 20 to y if x is equal to 5 or the value 10 if x is not equal to 5.

9 © 2005 Lawrenceville Press Slide 9 Chapter 5 Nested if-else Statements  Should be indented to make the logic clear.  Nested statement executed only when the branch it is in is executed. For example, the statement if (x == 5) { y = 20; } else { if (x > 5) { y = 10; } else { y = 0; } } evaluates the nested if-else only when x is not equal to 5.

10 © 2005 Lawrenceville Press Slide 10 Chapter 5 The if-else if Statement  Used to decide among three or more actions.  Conditions must be properly ordered for the statement to evaluate as expected. For example, the statement if (x < 5) { y = 20; } else if (x < 10) { y = 40; } else if (x < 15) { y = 80; } would give very different results if the conditions were ordered differently.

11 © 2005 Lawrenceville Press Slide 11 Chapter 5 The switch Statement  Used to decide among three or more actions.  Uses an expression that evaluates to an integer.  The break statement moves program execution to the next statement after the switch.  The default code is optional and is executed when none of the previous cases are met: switch (numLegs) { case 2: System.out.println("human"); break; case 4: System.out.println("beast"); break; case 8: System.out.println("insect"); break; default: System.out.println("???"); break; }

12 © 2005 Lawrenceville Press Slide 12 Chapter 5 The Random Class  Part of the java.util package  A Random object can generate random integers (nextInt()) or random floating point numbers (nextDouble()). For example, Random r = new Random; int numLessThan10; numLessThan10 = r.nextInt(10);//0-9  A random integer in a range is generated by using the formula: r.nextInt(highNum – lowNum + 1) + lowNum

13 © 2005 Lawrenceville Press Slide 13 Chapter 5 Compound Boolean Expressions  More than one Boolean expression in a single condition.  Formed using the logical And (&&), logical Or (||), or logical Not (!) operators. Precedence: !, &&, || Precedence: !, &&, ||

14 © 2005 Lawrenceville Press Slide 14 Chapter 5 And Truth Table And Exp1Exp2Result True False TrueFalse

15 © 2005 Lawrenceville Press Slide 15 Chapter 5 Or Truth Table Or Exp1Exp2Result True FalseTrue FalseTrue False

16 © 2005 Lawrenceville Press Slide 16 Chapter 5 Not Truth Table Not ExpResult TrueFalse True

17 © 2005 Lawrenceville Press Slide 17 Compiler Ambiguity a + b * c a + b * c Could mean: (a + b) * c (a + b) * cOr a + (b * c) a + (b * c) Java compiler resolves ambiguity in expressions by rules of precedence and associativity. We know that the correct interpretations of a+b*c is…

18 © 2005 Lawrenceville Press Slide 18 Okay AP students… a + (b * c) a + (b * c) We can force the other interpretation by using parentheses.

19 © 2005 Lawrenceville Press Slide 19 Chapter 5 The Math Class  Part of the java.lang package  Methods include: abs(num)returns the absolute value of num pow(num1, num2)returns num1 raised to the num2 power sqrt(num)returns the square root of num, where num is a positive number

20 © 2005 Lawrenceville Press Slide 20 Random Numbers Linear Congruential Method – Uses a formula to compute the random list of numbers. This method does not produce truly random numbers, because at some point the numbers repeat. In Programming – random numbers are referred to ask pseudorandom. The Random class resides in the java.util package. Some of the random class methods are: nextInt( ) returns the next random integer in the the random number generator’s sequence. nextInt (n) returns the next random integer (inclusive) from 0 to n. nextDouble ( ) returns the next random double from 0.0 to 1.0.

21 © 2005 Lawrenceville Press Slide 21 Implementation of random import java.util.Random; import java.util.Random; Public static void main (String[] args) { Random rnumber = new Random( ); System.out.println(“The first random number is :”+r.nextInt(100); System.out.println(“The second random number is :”+r.nextInt(100); }

22 © 2005 Lawrenceville Press Slide 22 Chapter 5 Flowchart Symbols decision

23 © 2005 Lawrenceville Press Slide 23 Chapter 5 The RPS Flowchart

24 © 2005 Lawrenceville Press Slide 24 Flowchart decision possibilities yes no

25 © 2005 Lawrenceville Press Slide 25 Flowchart decision possibilities yes no

26 © 2005 Lawrenceville Press Slide 26 Flowchart decision possibilities yes no Common exit point

27 © 2005 Lawrenceville Press Slide 27 HOMEWORK The Case of the Dangling ELSE if (n <= max) if (n > 0) sum += n; else sum +=max; ------------------------------------------------------------- 1 st run n = 0; max = 4 2 nd run n=8; max = 6 What is the value of max after each run? DO NOT ASK ME FOR HELP!


Download ppt "© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning <less than <=less than or equal >greater than >=greater than."

Similar presentations


Ads by Google