Tina Comston Comp-111 Week 7 Franklin University |
||||
Week 7 | ||||
Preparation
|
||||
Guided
Learning Activity |
||||
Assignment
Operators, conditions, and decision structures Java supports the following operators: < less than > greater than == equality (notice the double equal sign) A single equal sign assigns a value, a double equal sign tests for equality. <= less than or equal>= greater than or equal ! not, reverses the result != not equal These operators are used when comparing two values. The result of which is a boolean value, either true or false. The result of the comparison is typically referred to as a "condition". Conditions are used in decision structures. A decision structure is also referred to as if statement The format of a decision structure/if statement is: The
example given above is a single decision structure. The
statements are only executed if the condition is true. Another
form is the dual decision structure. This type of structure has
statements to execute when the condition is true, but also has
statements to execute when the condition is false.if (condition) { statements to execute } for example: if (x < y) { System.out.println("y is greater than x"); } When reviewing the format of the if statement take note of the following:
if (condition) YouTube video on comparisons https://www.youtube.com/watch?v=6O_lwNnWFR0{ statements to execute when true } else { statements to execute when false } for example: if (x < y ) { System.out.println("Y is greater than X"); } else { System.out.println("X is greater than Y"); } Note that:
Object Comparison Because
objects are user-defined, you cannot use the == comparison operator to
verify if two objects contain the same value. The == operator
will check to see if two object variables contain the exact same
object, meaning they point to the same location in memory, but they
will not validate that the two objects contain the same value. For example: String tmpName = "Joe"; String newName = "Joe"; The above String objects both contain the same value, but they are two completely different objects. If you were to have the following comparison if (tmpName == newName) The condition would equate to false. These are not the same object.If you want to check to see if they have the same value, you have to use a method written to compare the values. The String class has 3 such methods:
And, Or, Not And (&&) The
&& is used to join two conditions together. When using
the && operator, both conditions must equate to true for the
entire expression to equate to true. Or (||)For example: if (x < y && y < z) In the above expression, both x must be less than y and y must be less than z for the condition to equate to true The
|| operator is also used to join two conditions together. When
using the || operator, if either condition equates to true the entire
expression equates to true. For example: if (x < y || y < z) In the above expression, if x is less than y OR y is less than z, the condition equates to true. Not (!) The
! is used to reverse a condition. A condition that would equate
to true is reversed and becomes false and likewise a condition that
would equate to false is reversed and becomes true. For example if (! (x < y)) In this expression you would first determine if x is less than y and the reverse the result. YouTube video on logical/boolean operators https://www.youtube.com/watch?v=2o0jEUhOqaw
|
||||
Reflection
Paper
|
||||
Lab 2 - due this week
|