Logical Operators
&& (and), || (or), and ! (not) — compose booleans into bigger conditions.
1. && — logical AND
&& is true only when both sides are true. Use it when every condition must hold:
val canVote = age >= 18 && citizen
val inLow = n >= 1 && n <= 10
Truth table:
| left | right | left && right |
|---|---|---|
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |
&& short-circuits — if the left is false, the right isn't even evaluated.
2. && — inRange
Implement inRange(n, low, high) returning true if n is between low and high (inclusive on both ends). Combine two comparisons with &&.
Starter code
fun inRange(n: Int, low: Int, high: Int): Boolean {
return false
}3. || — logical OR
|| is true when at least one side is true. Use it when any condition is enough:
val isWeekend = day == "Sat" || day == "Sun"
val flagged = banned || expired
Truth table:
| left | right | left || right |
|---|---|---|
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
Like &&, || short-circuits — if the left is true, the right isn't evaluated.
4. || — hasOdd
Implement hasOdd(a, b) returning true if either a or b is odd. A number is odd when n % 2 != 0.
Starter code
fun hasOdd(a: Int, b: Int): Boolean {
return false
}5. && vs || — when to use which
Both combine booleans, but they answer different questions:
| Operator | English | True when… |
|---|---|---|
&& | "and" / "both" | both sides are true |
|| | "or" / "either" | at least one side is true |
// "must satisfy both" → &&
val ready = signedIn && verified
// "satisfies at least one" → ||
val needsReview = unread || flagged
You'll often combine them. Use parentheses to make the intent obvious:
// classic Gregorian-leap-year rule:
// divisible by 4 AND not 100, OR divisible by 400
val leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0
6. && and || — leap years
Implement isLeapYear(year) using the Gregorian rule:
A year is a leap year if it's divisible by 4 and not divisible by 100, or if it's divisible by 400.
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0
Starter code
fun isLeapYear(year: Int): Boolean {
return false
}7. ! — logical NOT
! flips a boolean — !true is false, !false is true. Use it to invert a condition:
val anyone = !list.isEmpty() // not empty
val outOfRange = !(n in 1..10) // outside 1..10
! binds tightly, so wrap the expression you're negating in parentheses when in doubt:
!a == b // !a, then == b (probably not what you want)
!(a == b) // negation of equality (clearer)
Most of the time you'll write != or !ok directly — ! shines when you're flipping a method or expression that already returns a Boolean.
8. ! — isNotPositive
Implement isNotPositive(n) returning true if n is not positive (zero or negative). Use ! on a comparison: !(n > 0).
Starter code
fun isNotPositive(n: Int): Boolean {
return false
}Run this Kotlin lesson in your browser
SnipDojo ships an in-browser Kotlin editor with the exercises above already wired to tests. Sign in with Google to edit the starter code, run it, and track your progress toward the free certificate — no installs and no local toolchain.
Sign in to run this code →