Left Index

Kotlin Return and Jump

In this article we learn to terminate a loop using break. Like other language Kotlin also have break, continue and return.


Syntax for break:

Syntax:
for(item in Collection) {
	// if meet your condition than terminate this loop
	if(condition) {
		break;
	}
}

Let's have an example:

Example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	for(item in 1..5) {
		// if meet your condition than terminate this loop
		if(item == 3) {
			break;
		}
		
		print("${item}, ")
	}
}

When the value of i is equal to 3, expression i == 3 inside if is evaluated to true, and break is executed. This terminates the for loop.

Output:

Output:
1, 2, 

Kotlin Labeled break Expression

We learn about break, that terminates nearest loop, suppose there are nested loop and want to terminate specified loop we use labeled break. 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.


Let's try to understand with example:

Example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	myloop@ for(item in 1..10) {
		for(item1 in 20 downTo 10 step 2) {
			if(item1 == 16 && item == 3) {
				break@myloop
			}
			
			println("First loop: ${item}, second loop: ${item1}, ")
		}
		
		println()
	}
}

Here, when (item == 3 and item1 == 16) expression is evaluated to true, break@myloop is executed which terminates the loop marked with label myloop@.


Output above snippet:

Output:
First loop: 1, second loop: 20, 
First loop: 1, second loop: 18, 
First loop: 1, second loop: 16, 
First loop: 1, second loop: 14, 
First loop: 1, second loop: 12, 
First loop: 1, second loop: 10, 

First loop: 2, second loop: 20, 
First loop: 2, second loop: 18, 
First loop: 2, second loop: 16, 
First loop: 2, second loop: 14, 
First loop: 2, second loop: 12, 
First loop: 2, second loop: 10, 

First loop: 3, second loop: 20, 
First loop: 3, second loop: 18, 

Kotlin return: Like other language(C, C#, C++ or java etc...) Kotlin have return expression that will terminate whole function from anywhere in function block, for example:

Example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	demoReturn()
	
	demoReturn1()
}

fun demoReturn() {
	for(item in 1..10) {
		for(item1 in 20 downTo 10 step 2) {
			if(item1 == 16 && item == 2) {
				return;
			}
			
			println("First loop: ${item}, second loop: ${item1}, ")
		}
		
		println()
	}
}


fun demoReturn1() {
	// loop 1
	while(true) {
		// loop 2
		while(true) {
			// loop 3
			while(true) {
				// loop 4
				while(true) {
					return;
				}
				println("Loop 3");
			}
			println("Loop 2");
		}
		
		println("Loop 1");
	}
}

Output:
First loop: 1, second loop: 20, 
First loop: 1, second loop: 18, 
First loop: 1, second loop: 16, 
First loop: 1, second loop: 14, 
First loop: 1, second loop: 12, 
First loop: 1, second loop: 10, 

First loop: 2, second loop: 20, 
First loop: 2, second loop: 18,