Dart
논리 연산자
논리 연산자
안녕하세요.
이번엔 논리 연산자에 대해서 알아보겠습니다.
논리연산자의 종류에는 &&(and), ||(or0, !(not) 이 있습니다.
하나씩 알아봅시다.
논리 연산자 &&(and) 사용하기
&&(and)
&&는 값이 모두 참이면 참입니다.
무슨 말인가 싶죠?
(true && false)
위의 식에서는 왼쪽은 true이며 오른쪽은 false입니다.
&&가 사용되어 위의 식은 둘다 true가 아니므로 false를 반환합니다.
반대로
(true && true)
위의 식은 둘다 true이므로 true를 반환합니다.
해봅시다.
void main() { bool myBoolA = true; bool myBoolB = true; print(myBoolA && myBoolB); }
결과는 바로 아래에서 확인할 수 있습니다.

이번엔 값 하나를 false를 대입해 볼게요.
void main() { bool myBoolA = true; bool myBoolB = false; print(myBoolA && myBoolB); }
결과는 바로 아래에서 확인할 수 있습니다.

참고로 &&를 여러번 사용해서 다음과 같이도 사용할 수 있습니다.
void main() { bool myBoolA = true; bool myBoolB = true; bool myBoolC = true; print(myBoolA && myBoolB && myBoolC); }
위의 경우 myBoolA, myBoolB, myBoolC 값 모두 true여야 true를 반환합니다. 하나라도 false면 false를 반환합니다.
결과는 바로 아래에서 확인할 수 있습니다.

false 값 하나를 더 추가해서 결과를 볼게요.
void main() { bool myBoolA = true; bool myBoolB = true; bool myBoolC = true; bool myBoolD = false; print(myBoolA && myBoolB && myBoolC && myBoolD); }
결과는 바로 아래에서 확인할 수 있습니다.

하나라도 false면 false입니다.
논리 연산자 ||(or) 사용하기
||(or)
||(or)는 값 중 하나라도 true면 true입니다.
무슨 말인가 싶죠?
(true || false)
위의 식에서는 왼쪽은 true이며 오른쪽은 false입니다.
or가 사용되어 위의 식은 하나라도 true가 있으므로 true를 반환합니다.
해봅시다.
void main() { bool myBoolA = true; bool myBoolB = false; print(myBoolA || myBoolB); }
결과는 바로 아래에서 확인할 수 있습니다.

둘 다 값을 false로 하면 하나도 true가 없으므로 false를 반환합니다.
void main() { bool myBoolA = false; bool myBoolB = false; print(myBoolA || myBoolB); }
결과는 바로 아래에서 확인할 수 있습니다.

값이 하나라도 true이면 true를 반환하기 때문에 false가 아무리 많아도 단 하나만 true가 있으면 true를 반환합니다.
void main() { print(true || false || false || false); }
결과는 바로 아래에서 확인할 수 있습니다.

논리 연산자 !(not) 사용하기
!(not)
!(not)은 다음에 오는 값의 반대 값을 반환합니다.
즉 not true면 false, not false면 true를 반환합니다.
해봅시다.
void main() { bool myBoolA = true; print(!myBoolA); }
결과는 바로 아래에서 확인할 수 있습니다.

//=$langList['bottomThankyou'][$langMode]?>