funmain(args:Array<String>){for(iin1..5) println(i) } It's possible to iterate through a range usingforloop because ranges provides an iterator. To learn more, visitKotlin iterators. Example: Different Ways to Iterate Through a Range ...
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 – continue The following tutorials cover some of the special use cases with loop statements. Kotlin – For i in range 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 – I...
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) { ...
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) ...
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 ...
No compatible source was found for this media. Q 2- What will be the last number printed by the following for loop? funmain(args:Array<String>){for(itemin6downTo1step2){println(item)}} A- 6 B- 5 C- 3 D- 2 Print Page
while loop is terminated. Flowchart of while Loop Example: Kotlin while Loop // Program to print line 5 times fun main(args: Array<String>) { var i = 1 while (i <= 5) { println("Line $i") ++i } } When you run the program, the output will be: Line 1 Line 2 Line 3 Line...
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=...
1. For Loop Loop a Map and filter it by keys or values. funmain(args:Array<String>){valitems = HashMap<String,Int>() items["A"] =10items["B"] =20items["C"] =30println("-- Example 1 -- \n$items");//for loopprintln("\n-- Example 1.1 -- ");for((k, v)initems) {...