Kotlin is a statically typed programming language that runs on the Java Virtual Machine (JVM). It was developed by JetBrains and is now an official language for Android development. Kotlin is designed to be concise, expressive, and safe. It is also interoperable with Java, which means that you can use Kotlin and Java code together in the same project.
Overview about the Lesson
This lesson is part of a series of lessons on Kotlin programming. In this lesson, we will cover the use of Booleans and If … Else statements in Kotlin. We will start by discussing what Booleans are and how they work. We will then explore the different types of Booleans in Kotlin, including nullable Booleans. Finally, we will look at some examples of how to use Booleans and If … Else statements in Kotlin.







The Content Of The Lesson
The content of this lesson is divided into the following sections:
- What are Booleans?
- Nullable Booleans
- If … Else statements
- Examples
Why This Lesson
This lesson is important because Booleans and If … Else statements are essential parts of any programming language. They allow us to make decisions based on conditions, which is essential for building complex applications. By understanding Booleans and If … Else statements in Kotlin, you will be able to write more efficient and effective code.
Booleans and if…else in Kotlin: Examples
The PDF explains Booleans and conditional logic. Here are the patterns you will use most, with their output.
Boolean values and expressions
fun main() {
val isLoggedIn = true
val age = 17
val isAdult = age >= 18
println(isLoggedIn) // true
println(isAdult) // false
}A Boolean is either true or false. Comparisons such as age >= 18 produce Booleans.
if, else if, else
fun main() {
val temperature = 31
if (temperature > 35) {
println("Very hot")
} else if (temperature > 25) {
println("Warm")
} else {
println("Cool")
}
}
// Output: WarmConditions are checked from top to bottom, and only the first matching branch runs.
if as an expression
In Kotlin, if returns a value, so it replaces Java’s ternary operator (? :), which Kotlin does not have:
fun main() {
val score = 72
val result = if (score >= 50) "Pass" else "Fail"
println(result) // Pass
val max = if (10 > 7) 10 else 7
println(max) // 10
}When if is used as an expression, the else branch is required.
Combining conditions
fun main() {
val hasTicket = true
val age = 15
if (hasTicket && age >= 12) println("Welcome in")
if (!hasTicket || age < 12) println("Sorry, no entry")
}
// Output: Welcome inCommon mistakes
- Using
=(assignment) instead of==(comparison). Kotlin reports an error, which saves you from a classic bug. - Comparing Booleans to true: write
if (isAdult), notif (isAdult == true). - Long else-if chains: when you have many branches, the
whenexpression in Lesson 8 is clearer.
Practice exercise
Print “Even” or “Odd” for a number using if as an expression.
fun main() {
val n = 7
val type = if (n % 2 == 0) "Even" else "Odd"
println("$n is $type") // 7 is Odd
}Frequently Asked Questions
Does Kotlin have a ternary operator?
No. Use if (condition) a else b, which is an expression and does the same job.
Can an if block return a value from several lines?
Yes. The value of the last expression in the block is returned: val x = if (ok) { log(); 1 } else { 0 }.
Continue the course
← Previous: Lesson 6: Set Operations & Grouping | Next: Lesson 8: When, While, Break & Continue →
- Lesson 1: Getting Started
- Lesson 2: Syntax & Output
- Lesson 3: Comments & Variables
- Lesson 4: Data Types
- Lesson 5: Operators
- Lesson 6: Set Operations & Grouping
- Lesson 7: Booleans & If…Else (you are here)
- Lesson 8: When, While, Break & Continue
- Lesson 9: Arrays
- Lesson 10: For Loops & Ranges
- Lesson 11: Functions & Classes
- Lesson 12: Classes, Objects & Constructors
- Lesson 13: Class Functions & Inheritance
Conclusion
In conclusion, Booleans and If … Else statements are essential parts of any programming language. In this lesson, we covered the different types of Booleans in Kotlin, including nullable Booleans. We also looked at some examples of how to use Booleans and If … Else statements in Kotlin. By understanding Booleans and If … Else statements in Kotlin, you will be able to write more efficient and effective code.
Download From Link
You can download the lesson from the following link:













