Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Logical Operators | Boolean Expressions and Logical Conditions
Control Flow in Kotlin

bookLogical Operators

メニューを表示するにはスワイプしてください

Combining Boolean Expressions with Logical Operators

You can combine multiple Boolean expressions in Kotlin using logical operators. These operators let you build more complex conditions by connecting simple true/false statements.

Logical Operators in Kotlin

  • AND (&&): Returns true only if both expressions are true;
  • OR (||): Returns true if at least one expression is true;
  • NOT (!): Reverses the value of a Boolean expression.

Example: Checking Multiple Conditions

Suppose you want to check if a number is positive and even. You can combine the two checks using the && operator:

package com.example

fun main() {
    val number = 8
    val isPositive = number > 0
    val isEven = number % 2 == 0
    val result = isPositive && isEven
    println(result) // Prints: true
}

Example: Using OR (||)

You can also check if a number is either negative or zero:

package com.example

fun main() {
    val number = -1
    val isNegative = number < 0
    val isZero = number == 0
    val result = isNegative || isZero
    println(result) // Prints: true
}

Example: Using NOT (!)

To reverse a Boolean value, use the ! operator:

package com.example

fun main() {
    val isSunny = false
    val stayIndoors = !isSunny
    println(stayIndoors) // Prints: true
}

You can use these operators to create flexible, readable conditions without relying on if/else statements.

question mark

Which logical operator in Kotlin returns true only if both expressions are true?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  3

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  3
some-alt