ELEVATE YOUR BUSINESS WITH

Limitless customization options & Elementor compatibility let anyone create a beautiful website with Valiance.

Control Flow in Kotlin

SELECT * FROM `itio_tutorial_master` WHERE `tutorial_menu`='25' AND `tutorial_submenu`='80' AND `tutorial_status`=1 LIMIT 1

Control Flow in Kotlin

📌 Control Flow in Kotlin

Control flow in Kotlin refers to the order in which statements are executed in a program. Kotlin provides several control flow statements for making decisions and repeating actions.

🔎 Types of Control Flow Statements

  1. Conditional Statements

    • if and else

    • when (Similar to switch in other languages)

  2. Looping Statements

    • for Loop

    • while Loop

    • do-while Loop

  3. Jump Statements

    • break

    • continue

    • return


✅ 1. If-Else Statement

The if-else statement is used for decision-making.

📌 Syntax

kotlin

val number = 10if (number > 0) { println("Positive number")} else { println("Negative number or zero")}

Output:

typescript

Positive number


📌 If-Else as an Expression

In Kotlin, if-else can return a value and be used as an expression.

kotlin

val a = 15val b = 10val max = if (a > b) a else bprintln("Max: $max")

Output:

makefile

Max: 15

✅ 2. When Statement

The when statement in Kotlin is a more readable alternative to if-else-if chains. It works like a switch statement in other languages.

📌 Example

kotlin

val day = 3val dayName = when (day) { 1 -> "Monday" 2 -> "Tuesday" 3 -> "Wednesday" 4 -> "Thursday" else -> "Invalid Day"}println(dayName)

Output:

mathematica

Wednesday

📌 When with Multiple Conditions

kotlin

val grade = 'B'when (grade) { 'A', 'B' -> println("Good job!") 'C' -> println("Can do better") else -> println("Needs improvement")}

Output:

nginx

Good job!


✅ 3. For Loop

A for loop is used to iterate through a range, an array, or a collection.

📌 Example: Using Ranges

kotlin

for (i in 1..5) { println(i) // Output: 1 to 5}

📌 Example: Using until

kotlin

for (i in 1 until 5) { println(i) // Output: 1 to 4}

📌 Example: Iterating over Arrays

kotlin

val fruits = arrayOf("Apple", "Banana", "Cherry")for (fruit in fruits) { println(fruit)}

Output:

nginx

AppleBananaCherry


✅ 4. While Loop

The while loop runs as long as the condition is true.

📌 Example:

kotlin

var i = 1while (i <= 5) { println(i) i++}

Output:


12345

✅ 5. Do-While Loop

A do-while loop runs at least once because the condition is checked after the code block executes.

📌 Example:

kotlin

do { println(i) i++} while (i <= 5)

Output:


12345


✅ 6. Break and Continue

  • break → Exits the loop when a condition is met.

  • continue → Skips the current iteration and moves to the next.

📌 Break Example

kotlin

for (i in 1..10) { if (i == 5) { println("Breaking at $i") for (i in 1..5) { if (i == 3) { println("Skipping $i") continue } println(i)}

Output:


12Skipping 345


✅ Conclusion

  • If-else is great for simple conditions.

  • When is a cleaner alternative to multiple if-else-if statements.

  • For loops are efficient for iterating through ranges and collections.

  • While and Do-While loops are used for repeated execution with condition checks.

  • Break and Continue provide control over loop flow.

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql