Kotlin for loop using Array In the following example we have declared an arraymyArrayand we are displaying the elements of the array using for loop. packagebeginnersbook fun main(args:Array<String>){val myArray=arrayOf("ab","bc","cd","da")for(strinmyArray){println(str)}} Output: ab ...
Kotlin – Infinite While loop Examples In the following, we cover examples for each of the looping statements, break and continue statements. 1. For Loop Example – Iterate over elements of a list In the following program, for loop is used to print each item of a list. ...
If the body of the loop contains only one statement (like above example), it's not necessary to use curly braces { }. fun main(args: Array<String>) { for (i in 1..5) println(i) } It's possible to iterate through a range using for loop because ranges provides an iterator. To ...
Often when you work with arrays, you need to loop through all of the elements.To loop through array elements, use the for loop together with the in operator:Example Output all elements in the cars array: val cars = arrayOf("Volvo", "BMW", "Ford", "Mazda") for (x in cars) { ...
Given that you can mix Java and Kotlin code there are two folders: one for each language. The structure of a Java project is peculiar: in a Java project, the hierarchy of directories matched the package structure (i.e., the logical organization of the code). For example, if a Java ...
for loop while loop do..while loop Let’s start by looking at the repeat statement. 2. Repeat The repeat statement is the most basic of all the Kotlin loops. If we wish to simply repeat an operation n times, we can use repeat. For example, let’s print Hello World two times: repea...
1. For loop in kotlin Kotlin for loop is used to iterate the program several times. It iterates through ranges, arrays, collections, or anything that provides for iterate. The syntax of kotlin for loops is as follows Syntax: for(Item in collection) ...
Let's see one more example where the loop will iterate through another range, but this time it will step down instead of stepping up as in the above example:Open Compiler fun main(args: Array<String>) { for (item in 5 downTo 1 step 2) { println(item) } } ...
In this tutorial, you shall learn how to iterate over a range in Kotlin, using For loop statement, with example programs. Kotlin – For i in range To iterate over a range of numbers in Kotlin, we can use use For loop. The syntax to iterate over the range[a, b]using For loop is ...
for (i in 1..5) { if(i==4) break; println(i) } label 使用label可以指定要跳出的是哪一个循环 这里声明loop1 和 loop2, 首先每次内层j = 5的时候跳出内存循环 fun loopEx() { loop1@ for (i in 1..2) { println("---外层i--- $i") loop2@ for (j in 4 .. 6) { if (j=...