عملگرهای منطقی
جمعه, ۱۱ مرداد ۱۳۹۲، ۰۳:۱۳ ق.ظ
3 نوع عملگر منطقی وجود دارد:
عملگر |
معنی |
مثال |
&& |
AND منطقی |
(x >= 1) && (x <= 100) |
|| |
OR منطقی |
(x < 1) || (x > 100) |
! |
NOT منطقی |
(x == 8)! |
مثال:
// Return true if x is between 0 and 100 (inclusive)
(x >= 0) && (x <= 100) // AND (&&)
// Incorrect to use 0 <= x <= 100
// Return true if x is outside 0 and 100 (inclusive)
(x < 0) || (x > 100) // OR (||)
!((x >= 0) && (x <= 100)) // NOT (!), AND (&&)
// Return true if "year" is a leap year
// A year is a leap year if it is divisible by 4 but not by 100, or it is divisible by 400.
((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)
- ۹۲/۰۵/۱۱