Location>code7788 >text

Kotlin Boolean Tutorial: Deeper Understanding and Application Examples

Popularity:390 ℃/2024-08-05 15:36:30

Kotlin Boolean

In programming, you often need a data type that can only have two values, for example:

  • Yes / No
  • On/Off
  • True / False

For this purpose, Kotlin has a boolean data type that can be taken as thetrue maybefalse Value.

boolean

Boolean types can be used with theBoolean keyword declaration and can only taketrue maybefalse Value:

typical example

val isKotlinFun: Boolean = true
val isFishTasty: Boolean = false
println(isKotlinFun) // exports true
println(isFishTasty) // exports false

Like the other data types you learned about in previous chapters, the above example can be written without specifying a type, because Kotlin is smart enough to understand that the variable is of type Boolean:

typical example

val isKotlinFun = true
val isFishTasty = false
println(isKotlinFun) // output true
println(isFishTasty) // false

boolean expression

A boolean expression returns a boolean value:true maybefalse

You can use comparison operators such as greater than (>) operator to determine whether an expression (or variable) is true:

typical example

val x = 10
val y = 9
println(x > y) // returns true because 10 is greater than 9

Or simpler:

typical example

println(10 > 9) // returns true because 10 is greater than 9

In the following example, we use the value equal to (==) operator to evaluate the expression:

typical example

val x = 10
println(x == 10) // Returns true, because the value of x is equal to 10.

typical example

println(10 == 15) // Returns false, because 10 is not equal to 15.

The Boolean value of an expression is the basis for all Kotlin comparisons and conditions.

In the next chapter, you will learn more about conditionals.

Kotlin conditionals andIf..Else

Kotlin supports common logical conditions in math:

  • Less than:a < b
  • Less than or equal to:a <= b
  • Greater than:a > b
  • Greater than or equal to:a >= b
  • Equivalent:a == b
  • Not equal:a != b

You can use these conditions to perform different actions for different decisions.

Kotlin has the following conditional statements:

  • utilizationif Specifies that the condition of thetrue A piece of code to be executed when
  • utilizationelse Specifies that the condition of thefalse A piece of code to be executed when
  • utilizationelse if In the first condition forfalse When testing new conditions
  • utilizationwhen Specify multiple alternative code blocks to execute

NOTE: In contrast toJava Different.if..else It can be used in Kotlin as a statement or expression (to assign a value to a variable). See the example at the bottom of the page for a better understanding.

Kotlin if

utilizationif Specifies that the condition of thetrue A piece of code to be executed when the

grammatical

if (condition) {
  // The block to execute if the condition is true.
}

Attention.if are lowercase letters. Upper case letters (If maybeIF) will produce errors.

In the following example, we test two values to determine the20 Is it greater than18. If the condition istrue, print some text:

typical example

if (20 > 18) {
  println("20 is greater than 18")
}

We can also test variables:

typical example

val x = 20
val y = 18
if (x > y) {
  println("x is greater than y")
}

illustrative example

In the above example, we use two variablesx cap (a poem)y to testx Is it greater thany(Use> operators). Since thex be20y be18And we know that20 more than18So we print on the screen"x is greater than y."

Kotlin else

utilizationelse Specifies that the condition of thefalse A piece of code to be executed when the

vocabulary

if (condition) {
  // The block to execute if the condition is true.
} else {
  // The block to execute if the condition is false
}

typical example

val time = 20
if (time < 18) {
  println("Good day.")
} else {
  println("Good evening.")
}
// exports "Good evening."

illustrative example

In the example above, thetime20) is greater than18, so the condition isfalseSo we turn toelse conditions and print them on the screen"Good evening". Iftime less than18The program prints"Good day"

Kotlin else if

utilizationelse if In the first condition forfalse when specifying a new condition.

grammatical

if (condition1) {
  // The block to execute if condition1 is true.
} else if (condition2) {
  // Block to execute if condition1 is false and condition2 is true.
} else {
  // Block to execute if condition1 and condition2 are both false.
}

typical example

val time = 22
if (time < 10) {
  println("Good morning.")
} else if (time < 20) {
  println("Good day.")
} else {
  println("Good evening.")
}
// exports "Good evening."

illustrative example

In the example above, thetime22) is greater than10, so the first condition isfalseThe Inelse if The next condition in the statement is alsofalseSo we turn toelse condition becausecondition1 cap (a poem)condition2 all arefalseand print on the screen"Good evening"

However, iftime be14Our program will print"Good day"

Kotlin If..Else displayed formula

In Kotlin, you can also set theif..else statement is used as an expression (assigns a value to a variable and returns it):

typical example

val time = 20
val greeting = if (time < 18) {
  "Good day."
} else {
  "Good evening."
}
println(greeting)

utilizationif As an expression, you must also include theelse(Required).

Note: If theif There is only one statement, and you can omit the curly braces.{}

typical example

fun main() {
  val time = 20
  val greeting = if (time < 18) "Good day." else "Good evening."
  println(greeting)
}

Tip: This example is similar to theJava The "ternary operator" (abbreviated asif...else)。

ultimate

To make it easier for peeps on other devices and platforms to view past articles:

WeChat Search:Let us CodingFollow to get the latest articles tweeted

If you find it helpful, please like, favorite and follow it!