Intro to Java Web-Notes_Part24

Intro to Java Web-Notes_Part24 - Pretty fast Keep your eyes...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Pretty fast! Keep your eyes on the road. 01' You still have to pay attention. Even at slower speeds. Relational and Logical Operators The boolean expressions in i ' and 1' "—el se statements often involve the use of relational and logical operators. You are already familiar with arithmetic operators, such as *, /, %, + and -. Expressions of involving these operators yield numeric results (e.g., 3 * 10 yields 33). Relational and logical operators are used to form expressions that yield one of the two boolean values of either true or false (in Java the boolean values of true and fal se must be written in all lowercase letters). Relational Operators Here are the six relational operators: <— Less than or equal to E E Here are some examples of some expressions involving relational operators, and their resulting boolean values: 100 l= 62 true 3 >= 20 :a' 5 > 5 ‘a' SE SE ll >= ll true For these examples, assume that the int variable hours has been declared and currently has the value of 15: Expression hours > 40 ‘a' hours <= 30 :rue hours > 15 :a' Logical Operators The three logical operators: & & , | | and ! represent the actions of AND, OR and NOT, respectively. The && and H operators are used to combine boolean expressions, the ! operator is used to reverse a boolean value. The & & operator takes two boolean operands -- if both operands are true, then the entire expression is true -- otherwise it is false: ‘se && :rue ‘se && false The | | operator takes two boolean operands -- if either or both of the operands is true, then the entire expression is true -- the expression is only false if both operands are false: true false true false The relational and logical operators are often used together to express a complex condition for an 1' ' or 1' "—el se statement. For example, a payroll program might only give a bonus to workers who have been with the company for two or more years, and have worked at least eighty hours in the last month: if (years >: 2 && hours > 80) paycheck +: bonus; Since the & & operator is used here, the worker only gets the bonus if both conditions are true (years >= 2 and hours > 80). If either condition is false then the entire expression is false, and the statement to add in a bonus amount is skipped. The next example simulates a game in which a player rolls two dice (six-sided dice). The sum of the two dice yields a value from 2 to 12. If the player rolls either a 7 or an 11 on the first roll of the dice then they win the game: if (sum == ll sum == ll) System.out.println("You winl"); Notice the difference between the H and the & & operators. For the years >= 2 & & hours > 8 0 expression to be true both operands need to be true (years >2 2 and hours > 8 O for the paycheck to get a bonus). But with the sum == | | sum == ll expression just one of the operands needs to be true (if sum is sum == 7 0r sum 2: l 1 then you win). Now that we've learned some new operators, we can update the precedence table. The table includes an indication of the type of operator (arithmetic, logical, relational) and its associativity. Operators toward the top of the table have a higher precedence that operators that are found lower in the table. Operator Precedence Table ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern