Kotlin Loop: for loop + while loop + break & continue example

kotlin-loop-for-loop-while-loop-break-continue-example-feature-image

In the tutorial, Grokonez will show you how to work with Kotlin Loop statements: for, while, do while and break & continue with Kotlin loops.

I. Kotlin For loop

We use for statement to iterate through an iterator collection.

1. for statement with Array & List collection

The syntax is as follows:


for(item in anArray){
	println(item)
}

...
for(element in aList){
	println(element)
}

For working with index, we can use:

// with Array
for(i in anArray.indices){
	println("customer with index = $i is " + anArray[i])
}

... or ...
for((index, item) in anArray.withIndex()){
	println("the item at $index is $item")
}

// with List
for(index in aList.indices){
	println("element with index = $index is " + aList.get(index))
}

... or ...
for((index, element) in aList.withIndex()){
	println("the element at $index is $element")		
}

Practice:


val simpleArray = arrayOf(1, 2, 3, 4, 5)
for(item in simpleArray){
	println(item)
}
// print on console
// ->
/*
	1
	2
	3
	4
	5
*/

val customerArray = arrayOf(Customer("Craig", 45),
								Customer("Amos", 23),
								Customer("Jack", 20))

for(cust in customerArray){
	println(cust)
}
// print on console
// ->
/*
	Customer(name=Craig, age=45)
	Customer(name=Amos, age=23)
	Customer(name=Jack, age=20)
*/

for(i in customerArray.indices){
	println("customer with index = $i is " + customerArray[i])
}
// print on console
// ->
/*
	customer with index = 0 is Customer(name=Craig, age=45)
	customer with index = 1 is Customer(name=Amos, age=23)
	customer with index = 2 is Customer(name=Jack, age=20)
*/

for((index, customer) in customerArray.withIndex()){
	println("the customer at $index is $customer")
}
// print on console
// ->
/*
	the customer at 0 is Customer(name=Craig, age=45)
	the customer at 1 is Customer(name=Amos, age=23)
	the customer at 2 is Customer(name=Jack, age=20)
*/

// work with Kotlin List
val simpleList = listOf(5, 6, 7, 8)
for(element in simpleList){
	println(element)
}
// print on console
// ->
/*
	5
	6
	7
	8 
*/

val customerList = listOf(Customer("Smith", 26),
							Customer("Peter", 43),
							Customer("Mary", 27))

for(customer in customerList){
	println(customer)
}
// print on console
// ->
/*
	Customer(name=Smith, age=26)
	Customer(name=Peter, age=43)
	Customer(name=Mary, age=27) 		
*/

for(index in customerList.indices){
	println("customer with index = $index is " + customerList.get(index))
}
// print on console
// ->
/*
	customer with index = 0 is Customer(name=Smith, age=26)
	customer with index = 1 is Customer(name=Peter, age=43)
	customer with index = 2 is Customer(name=Mary, age=27)
*/

for((index, customer) in customerList.withIndex()){
	println("the customer at $index is $customer")		
}
// print on console
//->
/*
	the customer at 0 is Customer(name=Smith, age=26)
	the customer at 1 is Customer(name=Peter, age=43)
	the customer at 2 is Customer(name=Mary, age=27)
*/

2. for statement with Map collection

The syntax is as follows:


for(item in aMap){
	println("item.key = " + item.key + " & item.value = " + item.value)
}

Practice:


val simpleHashMap = hashMapOf(1 to "one", 2 to "two", 3 to "three" )
for(item in simpleHashMap){
	println("item.key = " + item.key + " & item.value = " + item.value)
}
// print on console
// ->
/*
	item.key = 1 & item.value = one
	item.key = 2 & item.value = two
	item.key = 3 & item.value = three
*/

val customerHashMap = hashMapOf("Smith" to Customer("Smith", 26),
							"Peter" to Customer("Peter", 43),
							"Mary" to Customer("Mary", 27))

for(entry in customerHashMap){
	println("key = " + entry.key + " with customer = " + entry.value)
}
// print on console
// ->
/*
	key = Smith with customer = Customer(name=Smith, age=26)
	key = Peter with customer = Customer(name=Peter, age=43)
	key = Mary with customer = Customer(name=Mary, age=27)
*/

II. Kotlin While loops


// use while loops
println("--------------------------use while--------------------------")
var index = 0
while(index < customerArray.size){
	println(customerArray.get(index++))
}
// print on console
// ->
/*
	Customer(name=Craig, age=45)
	Customer(name=Amos, age=23)
	Customer(name=Jack, age=20)
*/

println("--------------------------use do-while--------------------------")
index = 0;
do{
	println(customerList.get(index))
	index++;
}while(index < customerList.size)
// print on console
// ->
/*
	Customer(name=Smith, age=26)
	Customer(name=Peter, age=43)
	Customer(name=Mary, age=27)
*/

III. Kotlin Break & Continue statement


val simpleArray = arrayOf(1, 2, 3, 4, 5)
...

for(item in simpleArray){ // simpleArray 1,2,3,4,5
	if(item == 4) break;
	println(item)
}
// print on console
// ->
/*
	1
	2
	3
*/

println("--------------------------control [for] statement with [continue]--------------------------")
for(item in simpleArray){ // //simpleArray 1,2,3,4,5
	if(item < 4) continue
	println(item)
}
// print on console
// ->
/*
	4
	5
*/

IV. Full Sourcecode


package com.javasampleapproach.kotlinforloops

data class Customer(
	val name: String,
	val age: Int
)

fun main(args : Array) {
	// work with Kotlin Array
	println("--------------------------iterates through an Array--------------------------")
	val simpleArray = arrayOf(1, 2, 3, 4, 5)
	for(item in simpleArray){
		println(item)
	}
	// print on console
	// ->
	/*
		1
		2
		3
		4
		5
	*/
	
	val customerArray = arrayOf(Customer("Craig", 45),
									Customer("Amos", 23),
									Customer("Jack", 20))
	
	for(cust in customerArray){
		println(cust)
	}
	// print on console
	// ->
	/*
		Customer(name=Craig, age=45)
		Customer(name=Amos, age=23)
		Customer(name=Jack, age=20)
	*/
	
	for(i in customerArray.indices){
		println("customer with index = $i is " + customerArray[i])
	}
	// print on console
	// ->
	/*
		customer with index = 0 is Customer(name=Craig, age=45)
		customer with index = 1 is Customer(name=Amos, age=23)
		customer with index = 2 is Customer(name=Jack, age=20)
	*/
	
	for((index, customer) in customerArray.withIndex()){
		println("the customer at $index is $customer")
	}
	// print on console
	// ->
	/*
		the customer at 0 is Customer(name=Craig, age=45)
		the customer at 1 is Customer(name=Amos, age=23)
		the customer at 2 is Customer(name=Jack, age=20)
	*/
	
	// work with Kotlin List
	println("--------------------------iterates through a List--------------------------")
	val simpleList = listOf(5, 6, 7, 8)
	for(element in simpleList){
		println(element)
	}
	// print on console
	// ->
	/*
		5
		6
		7
		8 
 	*/
	
	val customerList = listOf(Customer("Smith", 26),
								Customer("Peter", 43),
								Customer("Mary", 27))
	
	for(customer in customerList){
		println(customer)
	}
	// print on console
	// ->
	/*
		Customer(name=Smith, age=26)
		Customer(name=Peter, age=43)
		Customer(name=Mary, age=27) 		
 	*/
	
	for(index in customerList.indices){
		println("customer with index = $index is " + customerList.get(index))
	}
	// print on console
	// ->
	/*
		customer with index = 0 is Customer(name=Smith, age=26)
		customer with index = 1 is Customer(name=Peter, age=43)
		customer with index = 2 is Customer(name=Mary, age=27)
 	*/
	
	for((index, customer) in customerList.withIndex()){
		println("the customer at $index is $customer")		
	}
	// print on console
	//->
	/*
		the customer at 0 is Customer(name=Smith, age=26)
		the customer at 1 is Customer(name=Peter, age=43)
		the customer at 2 is Customer(name=Mary, age=27)
 	*/
	
	// work with Map
	println("--------------------------iterates through a Map--------------------------")
	val simpleHashMap = hashMapOf(1 to "one", 2 to "two", 3 to "three" )
	for(item in simpleHashMap){
		println("item.key = " + item.key + " & item.value = " + item.value)
	}
	// print on console
	// ->
	/*
		item.key = 1 & item.value = one
		item.key = 2 & item.value = two
		item.key = 3 & item.value = three
	*/
	
	val customerHashMap = hashMapOf("Smith" to Customer("Smith", 26),
								"Peter" to Customer("Peter", 43),
								"Mary" to Customer("Mary", 27))
	
	for(entry in customerHashMap){
		println("key = " + entry.key + " with customer = " + entry.value)
	}
	// print on console
	// ->
	/*
		key = Smith with customer = Customer(name=Smith, age=26)
		key = Peter with customer = Customer(name=Peter, age=43)
		key = Mary with customer = Customer(name=Mary, age=27)
	*/
	
	// use while loops
	println("--------------------------use while--------------------------")
	var index = 0
	while(index < customerArray.size){
		println(customerArray.get(index++))
	}
	// print on console
	// ->
	/*
		Customer(name=Craig, age=45)
		Customer(name=Amos, age=23)
		Customer(name=Jack, age=20)
	*/
	
	println("--------------------------use do-while--------------------------")
	index = 0;
	do{
		println(customerList.get(index))
		index++;
	}while(index < customerList.size)
	// print on console
	// ->
	/*
		Customer(name=Smith, age=26)
		Customer(name=Peter, age=43)
		Customer(name=Mary, age=27)
	*/
	
	println("--------------------------control [for] statement with [break]--------------------------")
	for(item in simpleArray){ // simpleArray 1,2,3,4,5
		if(item == 4) break;
		println(item)
	}
	// print on console
	// ->
	/*
		1
		2
		3
	*/
	
	println("--------------------------control [for] statement with [continue]--------------------------")
	for(item in simpleArray){ // //simpleArray 1,2,3,4,5
		if(item < 4) continue
		println(item)
	}
	// print on console
	// ->
	/*
		4
		5
	*/
}


By grokonez | January 5, 2018.

Last updated on April 14, 2021.



Related Posts


Got Something To Say:

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

*