Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Boolean Type and Values | Boolean Expressions and Logical Conditions
Control Flow in Kotlin

bookBoolean Type and Values

Sveip for å vise menyen

Declaring Boolean Variables

In Kotlin, you declare a Boolean variable using the Boolean type. Boolean variables can hold one of two values: true or false.

Syntax for Declaration

Use the val keyword for a read-only (immutable) variable, or var for a mutable variable:

val isActive: Boolean = true
var isEnabled: Boolean = false
  • val isActive: Boolean = true; declares an immutable Boolean variable named isActive and sets its value to true;
  • var isEnabled: Boolean = false; declares a mutable Boolean variable named isEnabled and sets its value to false.

You can also let Kotlin infer the type:

val isVisible = true
var isComplete = false
  • Here, isVisible and isComplete are both Boolean variables, with their types inferred based on the assigned values.

Boolean variables are essential for controlling program flow, as they are used in conditions and logical expressions.

question mark

Which values can a variable of type Boolean hold in Kotlin?

Velg det helt riktige svaret

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 1

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Seksjon 1. Kapittel 1
some-alt