คืนค่าการตั้งค่าทั้งหมด
คุณแน่ใจว่าต้องการคืนค่าการตั้งค่าทั้งหมด ?
ลำดับตอนที่ #4 : Java Ⅱ - Booleans and Conditions
Booleans
and Comparison Operators
Booleans
First, we'll learn a new data type
called boolean. A
boolean can have only two values, true and false. Be careful not to use quotes for true and
false!
Comparison
Operators
Comparison operators compare two
values, then return true or false. x == y
judges whether x and y have the same value. The result will be true if they do, and false if they don't. x != y is the opposite. Make sure not to confuse = (assign) and ==
(compare).
Printing
Booleans
You can print booleans as well. However,
you shouldn't enclose true and false in quotes, so be careful! As comparison
operators return boolean values, they also get printed as true or false.
Comparison
Operators
Just like math, you can use < and >
to compare numbers. You can also use >=
and <= if you want to make them
inclusive.
Logical
Operators
&&
(AND)
Logical operators are used to express
relations like AND, OR, and NOT. && is used to express AND , and it can be used to
combine conditions. For example, condition1
&& condition2 will return true only
when both condition1 and
condition2 are true.
|
| (OR)
||
is used for OR, and
it can also be used to combine conditions. For instance, condition1 || condition2 will return true if either condition1 or
condition2 is true. Take a look at the examples below!
!
(NOT)
! is used for NOT,
and it can be used to negate a condition. For example, the condition !(x >= 30) becomes true when x is smaller than 30 and
false when x is greater than or equal to 30.
Logical
Operators Review
Let's review logical
operators using the two simple examples below. &&
results in true only when both values are true while ||
becomes true if either one of them is true. Make sure to remember them to
master logical operators!
ความคิดเห็น