-
Conditional OperatorsJava 2022. 2. 5. 14:13
자바의 조건문에서 주로 쓰이는 연산자들에 대해 정리하였습니다.
< Introduction to Conditional Operators >
Conditional operators help simplify expressions containing complex boolean relationships by reducing multiple boolean values to a single value: true or false.
There's three conditional operators&lt; Conditional Operators &gt;
< And: && >
Checks if both provided expressions are truthy.
Example)
String mood = "sleepy"; int tirednessLevel = 6; if (mood.equals("sleepy") && tirednessLevel > 8) { System.out.println("Time to sleep"); } else { System.out.println("Not bet time yet"); }
< Or: || >
Checks if either provided expressions are truthy.
+. short-circuited evaluation.
The compiler can determine the truth value of a logical expression by only evaluating the first boolean operand.
it only works with && and ||
Example : || (or)
if (1 > 0 || 2 / 0 == 7) { System.out.println("No errors here!"); }
: Second operand is not executed.
In && expression, If the first operand in the expression is false, the entire value will be false.
< Logical NOT: ! >
Switches the truthiness and falsiness of a value.
!false // true !true // false
< Combining Conditional Operators >
The order of evaluation when it comes to conditional operators is as follows:
- Conditions placed in parentheses - ( )
- NOT - !
- AND - &&
- OR - ||
Example)
true && !(false || !true) // first : () true && !(false || false) true && !false // second : ! true && true // third : && true
'Java' 카테고리의 다른 글
ArrayLists (0) 2022.02.06 Array (0) 2022.02.05 Conditional (0) 2022.02.05 Class : Method (0) 2022.02.04 Class : Introduction (0) 2022.02.03