BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Kotlin continue Expression with examples

Last Updated: February 25, 2019 by Chaitanya Singh | Filed Under: Kotlin Tutorial

The continue construct skips the current iteration of the loop and jumps the control to end of the loop for the next iteration. The continue is usually used with if else expression to skip the current iteration of the loop for a specified condition. In this guide, we will lean Continue construct and Continue Labels.

Kotlin Continue For loop Example

fun main(args : Array<String>){

    for (n in 1..5){
        println("before continue, Loop: $n")
        if(n==2||n==4)
            continue

        println("after continue, Loop: $n")

    }
}

Output:
Kotlin continue example

As you can see in the output that println("after continue, Loop: $n") statement didn’t execute for the loop iterations n==2 and n==4 because on these iterations we have used the continue before this statement which skipped the iteration for these values of n.

However you can observe that println("before continue, Loop: $n") statement executed on every iteration because it is executed before Continue is encountered.

Based on this you can conclude that continue skips the iteration but it is not capable of skipping the statements that are encountered before continue. As soon as continue is encountered, the control jumps to the end of the loop skipping the rest of the statements.

Lets take another example

Kotlin continue example: displaying even numbers

fun main(args : Array<String>){

    for (n in 1..10){
        if(n%2!=0)
            continue

        println("$n")

    }
}

Output:
Kotlin continue example

Continue Label

Till now we have learned how continue works. Lets learn about continue labels. These labels are cool basically they give us more control when we are dealing with nested loops.

Lets take an example where we first do not use continue label and then we will take the same example with continue label.

Nested loop example without continue label
In this example we have a nested for loop and we are not using label. When we do not use labels we do not have any control and as soon as the continue is encountered the current iteration is skipped for the inner loop.

fun main(args : Array<String>){

    for (x in 'A'..'D'){
        for (n in 1..4){
            if (n==2||n==4)
                continue

            println("$x and $n")
        }

    }
}

Output:

A and 1
A and 3
B and 1
B and 3
C and 1
C and 3
D and 1
D and 3

Nested loop example with continue label
You can see in the above example output that for n value 2 and 4 the iteration is skipped for the inner loop. Lets say we want to skip the iteration for the outer for loop, we can do so with the help of continue labels.

fun main(args : Array<String>){

    myloop@ for (x in 'A'..'D'){
        for (n in 1..4){
            if (n==2||n==4)
                continue@myloop

            println("$x and $n")
        }

    }
}

Output:

A and 1
B and 1
C and 1
D and 1

Basically what happened here is that as soon as the n value reached 2 the control jumped to the end of outer loop because of the label and it happened for the each iteration. The syntax of the continue label is pretty straight forward as you just have to give a name to label followed by @ symbol and the same name needs to be appended with the continue statement as shown in the above example.

❮ PreviousNext ❯

Top Related Articles:

  1. Kotlin Interfaces with examples
  2. Kotlin Abstract Class with examples
  3. Kotlin Keywords, Soft Keywords and Identifiers
  4. Kotlin Constructors with examples
  5. Kotlin while Loop with examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Kotlin Tutorial

  • Kotlin Tutorial
  • Kotlin in Eclipse IDE
  • Kotlin in IntelliJ
  • First Kotlin Program
  • Kotlin Keywords
  • Kotlin Variables
  • Kotlin Type Casting
  • Kotlin operators
  • Kotlin Input/Output
  • Kotlin Comments

Kotlin String

  • Kotlin String

Kotlin Array

  • Kotlin Array
  • Kotlin Range

Control Flow

  • Kotlin if expression
  • Kotlin when expression
  • Kotlin for loop
  • Kotlin while loop
  • Kotlin do-while loop
  • Kotlin continue
  • Kotlin break

Kotlin Function

  • Kotlin Function
  • Kotlin Recursion
  • Kotlin default and named arguments
  • Lambda functions
  • Higher Order Function

Exception Handling

  • Exception Handling
  • Kotlin try catch
  • Multiple catch blocks
  • Nested try catch
  • Throw keyword
  • Kotlin try expression

Kotlin OOP

  • Class and Objects
  • Kotlin Constructors
  • Kotlin Inheritance
  • Visibility Modifiers
  • Kotlin abstract class
  • Kotlin Interfaces
  • Nested and Inner Class
  • Kotlin Data Class
  • Kotlin Sealed Class

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap