Kotlin ‘For’ loop

This post was originally posted at https://agrawalsuneet.github.io/blogs/kotlin-for-loop/ and reposted on Medium on 12th Jan 2018.
Oh no !!!
  1. Simple for loop in java that iterates from some number to some number incrementing one on each loop pass.
for (int i = 0; i <= 10; i++){
System.out.print(i);
}
for (i in 0..10) {
print(i)
}
  • no need to declare the data type of variable
  • if iterating over a range, we can use in variable
  • the lower and upper (including) limit can be defined on both the sides of .. operator.
oh !!!
for (int j = 0; j < 10; j++) {
System.out.print(j); //this will print only up to 9
}
for (j in 0..9) {
print(j)
}
for (j in 0 until 10) {
print(j)
}
Oh I see
for (int i = 0; i <= 10; i += 2) {
System.out.print(i);
}
for (i in 0..10 step 2) {
print(i)
}
for (int i = 10; i > 0; i--) {
System.out.print(i);
}
for (i in 10 downTo 1) {
print(i)
}
Java
for (int i = 10; i > 0; i -= 3) {
System.out.print(i);
}
Kotlin
for (i in 10 downTo 1 step 3) {
print(i)
}
for (int k = 2; k <= 256; k *= 2) {
System.out.print(k);
}
var k = 2
while (k <= 256) {
print(k)
k *= 2
}
int[] arr = new int[5];
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}
val arr = IntArray(5)
for (i in arr.indices) {
print(arr[i])
}
int[] arr = new int[5];
for (int item: arr) {
System.out.print(item);
}
for(item in arr){
print(item)
}
List<Integer> arrayList = new ArrayList<>();
for (int i = 0; i < arrayList.size(); i++) {
System.out.print(arrayList.get(i));
}
List<Integer> vector = new Vector<>();
for (int i = 0; i < vector.size(); i++) {
System.out.print(vector.get(i));
}
val arrayList = ArrayList<Int>()
for (i in arrayList.indices) {
print(arrayList[i])
}
val vector = Vector<Int>()
for (i in vector.indices) {
print(vector[i])
}
for (int item : arrayList) {
System.out.print(item);
}
for (int item : vector) {
System.out.print(item);
}
for (item in arrayList) {
print(item)
}
for (item in vector) {
print(item)
}
for ((i, value) in arr.withIndex()) {
println(“the element at $i is $value”)
}

--

--

--

https://agrawalsuneet.github.io/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Java & Lombok, a tale of two islands

Road to ZIO[n]: Chapter 4, the Persistence layer

Do I need a platform team?

Do you still need a Tech Radar in 2021?

Make your own powerful Multiphysics FEA package

How to become a good programmer

8 million Tokel ($TKL) Airdrop For Komodo Holders

A Solidity series to get you from O to HERO in no time [Part 8]

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Suneet Agrawal

Suneet Agrawal

https://agrawalsuneet.github.io/

More from Medium

3 Best Practices for Using Named Arguments in Kotlin

Kotlin Collections & Operations

Kotlin Programming Language: Everything You Need to Know

Kotlin Coroutines, putting together the pieces