Connect 4 Techs
  • Home
  • Excel
  • Data Analysis
  • Finance
  • MBA
    • Marketing
  • Kotlin
  • Gadgets
    • Apple
    • Samsung
    • Canon
    • Tech News
  • About
  • Contact
No Result
View All Result
Connect 4 Techs
  • Home
  • Excel
  • Data Analysis
  • Finance
  • MBA
    • Marketing
  • Kotlin
  • Gadgets
    • Apple
    • Samsung
    • Canon
    • Tech News
  • About
  • Contact
No Result
View All Result
Connect 4 Techs
No Result
View All Result
Home Kotlin

Kotlin When & While Loop, Break & Continue (Lesson 8 PDF)

Ahmed Elhozayen by Ahmed Elhozayen
October 11, 2026
in Kotlin
0
Kotlin When & While Loop, Break & Continue (Lesson 8 PDF)

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.

Kotlin

You might also like

Kotlin Class Functions & Inheritance (Lesson 13 PDF)

Kotlin Classes and Objects & Constructors (Lesson 12 PDF)

Kotlin Functions & Classes (Lesson 11 PDF)

Kotlin When & While Loop, Break & Continue (Lesson 8 PDF)

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 When & While Loop, Break & Continue statements in Kotlin. We will start by discussing what these statements are and how they work.

We will then explore the different types of When & While Loop, Break & Continue statements in Kotlin. Finally, we will look at some examples of how to use these statements in Kotlin.

Kotlin When & While Loop, Break & Continue (Lesson 8 PDF) – page 2
Kotlin When & While Loop, Break & Continue (Lesson 8 PDF) – page 3
Kotlin When & While Loop, Break & Continue (Lesson 8 PDF) – page 4
Kotlin When & While Loop, Break & Continue (Lesson 8 PDF) – page 5

The Content Of The Lesson

The content of this lesson is divided into the following sections:

  1. What are When & While Loop, Break & Continue statements?
  2. When statement
  3. While Loop statement
  4. Break statement
  5. Continue statement
  6. Examples

Why This Lesson

This lesson is important because When & While Loop, Break & Continue 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 When & While Loop, Break & Continue statements in Kotlin, you will be able to write more efficient and effective code.

when, while, break and continue: Examples

This lesson’s PDF covers Kotlin’s when expression and while loops. Here are clear examples with the output each one prints.

The when expression

when replaces long if…else if chains and Java’s switch statement.

fun main() {
    val day = 5
    val name = when (day) {
        1 -> "Monday"
        2 -> "Tuesday"
        3 -> "Wednesday"
        4 -> "Thursday"
        5 -> "Friday"
        6, 7 -> "Weekend"
        else -> "Invalid day"
    }
    println(name)   // Friday
}

You can match several values (6, 7), ranges and types:

fun grade(score: Int) = when (score) {
    in 90..100 -> "A"
    in 80..89 -> "B"
    in 70..79 -> "C"
    else -> "Needs work"
}

fun describe(x: Any) = when (x) {
    is Int -> "A number"
    is String -> "Text of length ${x.length}"
    else -> "Something else"
}

when without an argument works like a cleaner if-else chain: when { temp > 30 -> "Hot"; else -> "OK" }.

while and do-while loops

fun main() {
    var count = 1
    while (count <= 3) {
        println("Count: $count")
        count++
    }

    var input: Int
    do {
        input = (1..6).random()
        println("Rolled $input")
    } while (input != 6)
}

while checks the condition first, so the body may never run. do…while always runs the body at least once, which suits “try until success” logic like the dice roll above.

break and continue

fun main() {
    var i = 0
    while (i < 10) {
        i++
        if (i % 2 == 0) continue   // skip even numbers
        if (i > 7) break           // stop the loop completely
        println(i)
    }
}
// Output: 1 3 5 7 (each on its own line)
  • continue skips the rest of the current iteration and moves to the next one.
  • break exits the loop immediately.

Avoid infinite loops

Make sure something inside a while loop changes the condition. Forgetting count++ above would print “Count: 1” forever. In IntelliJ, stop a runaway program with the red Stop button.

Practice exercise

Keep doubling a number starting from 1 and print each value until it exceeds 100.

fun main() {
    var n = 1
    while (n <= 100) {
        println(n)
        n *= 2
    }
}
// Output: 1 2 4 8 16 32 64

Frequently Asked Questions

Is when better than if-else in Kotlin?

For three or more branches, usually yes: it is easier to read, and when it is used as an expression on an enum or sealed class, the compiler checks that every case is covered.

Does when need an else branch?

When used as an expression, yes, unless the compiler can prove all cases are covered (for example all values of an enum).

Continue the course

← Previous: Lesson 7: Booleans & If…Else  |  Next: Lesson 9: Arrays →

  1. Lesson 1: Getting Started
  2. Lesson 2: Syntax & Output
  3. Lesson 3: Comments & Variables
  4. Lesson 4: Data Types
  5. Lesson 5: Operators
  6. Lesson 6: Set Operations & Grouping
  7. Lesson 7: Booleans & If…Else
  8. Lesson 8: When, While, Break & Continue (you are here)
  9. Lesson 9: Arrays
  10. Lesson 10: For Loops & Ranges
  11. Lesson 11: Functions & Classes
  12. Lesson 12: Classes, Objects & Constructors
  13. Lesson 13: Class Functions & Inheritance

Conclusion

In conclusion, When & While Loop, Break & Continue statements are essential parts of any programming language. In this lesson, we covered the different types of When & While Loop, Break & Continue statements in Kotlin. We also looked at some examples of how to use these statements in Kotlin.

By understanding When & While Loop, Break & Continue 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:

From Here

Tags: AndroidJavaJVMKotlinProgrammingtechnology
Previous Post

Kotlin Booleans & Kotlin If … Else (Lesson 7 PDF)

Next Post

 Kotlin Arrays (Lesson 9 PDF)

Ahmed Elhozayen

Ahmed Elhozayen

Ahmed Elhozayen writes and curates the guides on Connect4Techs Tech. He works in the IT field and focuses on practical, step-by-step resources for Excel, data analysis, finance, MBA studies and Kotlin programming, so readers can apply what they learn the same day.

Related Posts

Kotlin Class Functions & Inheritance (Lesson 13 PDF)
Kotlin

Kotlin Class Functions & Inheritance (Lesson 13 PDF)

by Ahmed Elhozayen
October 11, 2026
Kotlin Classes and Objects & Constructors (Lesson 12 PDF)
Kotlin

Kotlin Classes and Objects & Constructors (Lesson 12 PDF)

by Ahmed Elhozayen
October 11, 2026
Kotlin Functions & Classes (Lesson 11 PDF)
Kotlin

Kotlin Functions & Classes (Lesson 11 PDF)

by Ahmed Elhozayen
October 11, 2026
Kotlin For Loop & Kotlin Ranges (Lesson 10 PDF)
Kotlin

Kotlin For Loop & Kotlin Ranges (Lesson 10 PDF)

by Ahmed Elhozayen
October 11, 2026
 Kotlin Arrays (Lesson 9 PDF)
Kotlin

 Kotlin Arrays (Lesson 9 PDF)

by Ahmed Elhozayen
October 11, 2026
Next Post
 Kotlin Arrays (Lesson 9 PDF)

 Kotlin Arrays (Lesson 9 PDF)

Recommended

20 Essential Excel Formulas Every Data Analyst Must Know in 2026

20 Essential Excel Formulas Every Data Analyst Must Know in 2026

May 18, 2026
Finance Excel Guidelines- A Useful Guide for Accountants

Finance Excel Guidelines: A Useful Guide for Accountants

October 11, 2026

Categories

  • apple
  • Canon
  • Data Analysis
  • Excel
  • Finance
  • ios
  • Kotlin
  • Marketing
  • MBA
  • Samsung
  • Software Tutorials
  • Technology

Don't miss it

20 Essential Excel Formulas Every Data Analyst Must Know in 2026
Data Analysis

20 Essential Excel Formulas Every Data Analyst Must Know in 2026

May 18, 2026
Excel guide for data analytics learners
Excel

Excel for Data Analysis PDF

October 11, 2026
The Ultimate Guide to Financial Modeling PDF
Excel

The Ultimate Guide to Financial Modeling PDF

October 11, 2026
The Ultimate Excel Handbook PDF
Excel

The Ultimate Excel Handbook PDF

October 11, 2026
𝟓𝟎 𝐄𝐱𝐜𝐞𝐥 𝐒𝐡𝐨𝐫𝐭𝐜𝐮𝐭 𝐊𝐞𝐲
Excel

50 Excel Shortcut Key PDF: Streamline Your Workflow

March 21, 2025
Most Useful Excel Keyboard Shortcuts For Windows and Mac OS
Excel

Most Useful Excel Keyboard Shortcuts for Windows and Mac OS PDF

March 21, 2025
Connect4Techs Tech

Practical, free guides on Excel, data analysis, finance, business and programming — written to help you learn faster and work smarter.

Follow on Facebook

Topics

  • Excel
  • Kotlin
  • MBA
  • Finance
  • Data Analysis
  • Apple
  • Technology
  • Software Tutorials

Latest articles

  • 20 Essential Excel Formulas Every Data Analyst Must Know in 2026May 18, 2026
  • Excel for Data Analysis PDFAug 31, 2025
  • The Ultimate Guide to Financial Modeling PDFApr 19, 2025

Company

  • About us
  • Contact
  • Privacy Policy
  • Terms of Use
  • IT Certification PDFs
  • AI Tools & News (Connect4AI)
© 2026 Connect4Techs. All rights reserved. Privacy · Terms · Contact
No Result
View All Result
  • Home
  • Excel
  • Data Analysis
  • Finance
  • MBA
    • Marketing
  • Kotlin
  • Gadgets
    • Apple
    • Samsung
    • Canon
    • Tech News
  • About
  • Contact