Kotlin jumb statements example

Published by inkskull on

Checkout the jumb statements in kotlin

WHAT IS KOTLIN?

Kotlin is a general-purpose, statically typed, and open-source programming language. It runs on JVM and can be used anywhere Java is used today. It can be used to develop Android apps, server-side apps and much more. Google accepetd the kotlin as the officill language for android.

HISTORY OF KOTLIN

Kotlin was developed by JetBrains team. A project was started in 2010 to develop the language and officially, first released in February 2016. Kotlin was developed under the Apache 2.0 license.

FEATURES OF KOTLIN

  • Concise: Kotlin reduces writing the extra codes. This makes Kotlin more concise.
  • Null safety: Kotlin is null safety language. Kotlin aimed to eliminate the NullPointerException (null reference) from the code.Interoperable.
  • Interoperable: Kotlin easily calls the Java code in a natural way as well as Kotlin code can be used by Java.
  • Smart cast: It explicitly typecasts the immutable values and inserts the value in its safe cast automatically.
  • Compilation Time: It has better performance and fast compilation time.
  • Tool-friendly: Kotlin programs are build using the command line as well as any of Java IDE.
  • Extension function: Kotlin supports extension functions and extension properties which means it helps to extend the functionality of classes without touching their code.

There are three jump expressions in Kotlin.

  • break
  • continue
  • return

Break

A break expression is used for terminate the nearest enclosing loop.

Syntax

  1. for(..){
  2. //body of for
  3. if(checkCondition){
  4. break;
  5. }
  6. }

Above for loop terminates its loop when ifcondition execute break expression.

Kotlin break example

  1. fun main(args: Array<String>) {
  2. for (i in 1..5) {
  3. if (i == 3) {
  4. break
  5. }
  6. println(i)
  7. }
  8. }

Kotlin Labeled break Expression

Labeled is the form of identifier followed by the @ sign, for example abc@, test@. To make an expression as label, we just put a label in front of expression.

Kotlin labeled break expression is used to terminate the specific loop. This is done by using break expression with @ sign followed by label name (break@loop).

Kotlin labeled break example

  1. fun main(args: Array<String>) {
  2. loop@ for (i in 1..3) {
  3. for (j in 1..3) {
  4. println(“i = $i and j = $j”)
  5. if (i == 2)
  6. break@loop
  7. }
  8. }
  9. }

0 Comments

Leave a Reply

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

Social Media Auto Publish Powered By : XYZScripts.com