














































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of selection statements, equality operators, relational operators, and logical operators in java programming. It covers topics such as forming conditions, if/else statements, comparing floating-point numbers, comparing objects, and the switch statement. It also explains the use of logical operators and short-circuit evaluation.
Typology: Assignments
1 / 54
This page cannot be seen from the preview
Don't miss anything!















































Flow of Control Part 1:
Selection
Forming Conditions
-^
if/else
Statements
Comparing Floating-Point Numbers
-^
Comparing Objects– The
equals
Method
String
Comparison Methods
The Conditional Operator ( ?: )
-^
The
switch
Statement
Used to determine if values of two expressions areequal or not equal
-^
Result is
true
or
false
is not equal to
binary
!=
is equal to
binary
= =
Meaning
Type (numberof operands)
Equalityoperators
If
int
variable
age
holds the value 32:
( age == 32 )
evaluates to
true
( age != 32 )
evaluates to
false
Use the equality operators only with primitive
types and object references, not to compareobject data!
-^
Used to compare the values of two expressions
-^
Result is
true
or
false
is greater than orequal to
binary
=
is greater than
binary
is less than orequal to
binary
<=
is less than
binary
<
Meaning
Type (number ofoperands)
RelationalOperators
If
int
variable
age
holds value 32:
( age < 32 )
evaluates to
false
( age <=
32 )
evaluates to
true
( age > 32 )
evaluates to
false
( age >=
32 )
evaluates to
true
-^
The NOT operator (! ) inverts the value of its operand. Ifthe operand is
true
, the result will be
false
; and if the
operand is
false
, the result will be
true
.
-^
The AND operator ( && ) takes two
boolean
expressions
as operands; if both operands are
true,
the result will be
true
, otherwise it will be
false
.
-^
The OR operator ( || ) takes two
boolean
expressions as
operands. If both operands are
false
, the result will be
false
; otherwise it will be
true
.
false
false
true
false
false
true
false
true
true
false
true
false
false
false
true
true
true
false
true
true
a || b
a && b
!a
b
a For operator precedence, see Appendix B
Suppose we have three
ints x, y,
and
z,
and we want
to test if
x
is less than both
y
and
z
. A common
error is to express the condition this incorrect way:
x < y && z
// compiler error
Each operand of a logical operator must be a boolean
expression. This is correct:
x < y
&&
x < z
DeMorgan's Laws: 1.
NOT( A AND B ) = ( NOT A ) OR ( NOT B )
NOT( A OR B ) = ( NOT A ) AND ( NOT B )
-^
Thus to find an equivalent expression:–^
change
&&
to
||
-^
change
||
to
&&
-^
negate each operand expression
These expressions are equivalent:
( age <= 18 || age >= 65 )!( age > 18 && age < 65 )!( age > 18 ) || !( age < 65 )
Used when program should perform an operationfor one set of data, but do nothing for all otherdata
-^
Syntax: if ( condition ){
// true block// executed if condition is true }
-^
Curly braces are optional if true block contains only onestatement
Indent the true block of the
if
statement for clarity
Line up the open and closing curly braces underthe "i" in
if
See Example 5.2 PassingGrade.java