Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For Loop with Multivariable in Kotlin

Tags:

kotlin

How can I do this java code in kotlin using just one for loop?

for(int i=0, j=0; i < 6 && j < 6; i++, j+=2) {
    // code here
}
like image 767
mosyonal Avatar asked Nov 16 '18 14:11

mosyonal


3 Answers

There is no way to iterate over multiple variables. In this case, the easiest thing you can do is:

for (i in 0..3) {
    val j = i * 2
}

In a more general case, you can rewrite this as a while loop:

var i = 0
var j = 0
while (i < 6 && j < 6) {
    // code here
    i++
    j += 2
}
like image 160
yole Avatar answered Oct 11 '22 13:10

yole


yole's answer is almost certainly the simplest and most efficient approach.

But one alternative you might look at is zipping sequences together, e.g.:

for ((i, j) in sequence{ yieldAll(0 until 6) }.zip(sequence{ yieldAll(0 until 6 step 2) })) {
    // code here
}

That's much more readable with a utility function, e.g.:

fun <T, U> seqs(it1: Iterable<T>, it2: Iterable<U>)
    = sequence{ yieldAll(it1) }.zip(sequence{ yieldAll(it2) })

for ((i, j) in seqs(0 until 6, 0 until 6 step 2)) {
    // code here
}

That's less efficient (initially creating Iterables, Ranges, and Sequences, and then a Pair for each iteration).  But it's the exact equivalent of the code in the question.  And because it defines each range in one place, it does at least make them very clear.

(I think this needs Kotlin 1.3.  There are probably simpler and/or more general ways of doing it; feel free to edit this if you can improve it!)

like image 35
gidds Avatar answered Oct 11 '22 14:10

gidds


I really appreciate gidds's answer

but there is another easier way to write for in loops for two variables

for ( (i, j) in (0..6).zip(0..6 step 2)){
    println("values are: $i, $j")
}

Above code run just fine as expected

you can also write for in loops easily for three variable. See the following code.

In this code I tried to implement a three variable series

// 1.2.3 + 2.3.4 + 3.4.5 + ... + n(n+1)(n+2)

fun main(args: Array<String>) {
    // taking int input from console
    val number: Int = readLine()!!.toInt()
    var sum: Int = 0

    // here second variable jPair becomes pair because of second nested zip
    for ( (i, jPair) in (1..number).zip((0..number + 1).zip(2..number + 2))) {
        println("values are: $i, ${jPair.first}, ${jPair.second}")

        sum += (i * jPair.first * jPair.second)
    }

    println("Series sum is: $sum")
}

Now, let's implement four variable for in loop

In the following code I am trying to implement a four variable series

// 1.3.5.7 + 3.5.7.9 + 5.7.9.11 + ... + n(n+2)(n+4)(n+6)

fun main(args: Array<String>) {
    // taking int input from console
    val number: Int = readLine()!!.toInt()
    var sum: Int = 0

    // here second variable iPair becomes pair because of first nested zip
    // here second variable jPair becomes pair because of second nested zip
    for ( (iPair, jPair) in ((1..number step 2).zip(3..number + 2 step 2)).zip((5..number + 4 step 2).zip(7..number + 6 step 2))) {
        println("values are: ${iPair.first}, ${iPair.second}, ${jPair.first}, ${jPair.second}")

        sum += (iPair.first * iPair.second * jPair.first * jPair.second)
    }

    println("Series sum is: $sum")
}

This way we can implement multiple variable for in loops easily.

like image 4
SHAH MD IMRAN HOSSAIN Avatar answered Oct 11 '22 15:10

SHAH MD IMRAN HOSSAIN