For Loop with Multivariable in Kotlin





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















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
}









share|improve this question























  • you need just one variable for such a loop

    – Maxim
    Nov 16 '18 at 18:35


















2















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
}









share|improve this question























  • you need just one variable for such a loop

    – Maxim
    Nov 16 '18 at 18:35














2












2








2








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
}









share|improve this question














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
}






kotlin






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 14:39









mosyonalmosyonal

134




134













  • you need just one variable for such a loop

    – Maxim
    Nov 16 '18 at 18:35



















  • you need just one variable for such a loop

    – Maxim
    Nov 16 '18 at 18:35

















you need just one variable for such a loop

– Maxim
Nov 16 '18 at 18:35





you need just one variable for such a loop

– Maxim
Nov 16 '18 at 18:35












2 Answers
2






active

oldest

votes


















5














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
}





share|improve this answer
























  • yeah I know these, I think maybe there is a way but I can't find it so I asked here. Thank you!

    – mosyonal
    Nov 16 '18 at 15:16











  • The Java to Kotlin conversion tool converts it to a while loop too.

    – Zoe
    Nov 16 '18 at 17:15











  • i < 6 is redundant here. actually it's clear from the first code block

    – Maxim
    Nov 16 '18 at 18:38













  • @Maxim I know, thanks. I'm showing how to rewrite the code in the general case when there is no such dependency between the two loop variables.

    – yole
    Nov 17 '18 at 8:59











  • @yole ah now I see

    – Maxim
    Nov 17 '18 at 19:03



















0














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!)






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339952%2ffor-loop-with-multivariable-in-kotlin%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    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
    }





    share|improve this answer
























    • yeah I know these, I think maybe there is a way but I can't find it so I asked here. Thank you!

      – mosyonal
      Nov 16 '18 at 15:16











    • The Java to Kotlin conversion tool converts it to a while loop too.

      – Zoe
      Nov 16 '18 at 17:15











    • i < 6 is redundant here. actually it's clear from the first code block

      – Maxim
      Nov 16 '18 at 18:38













    • @Maxim I know, thanks. I'm showing how to rewrite the code in the general case when there is no such dependency between the two loop variables.

      – yole
      Nov 17 '18 at 8:59











    • @yole ah now I see

      – Maxim
      Nov 17 '18 at 19:03
















    5














    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
    }





    share|improve this answer
























    • yeah I know these, I think maybe there is a way but I can't find it so I asked here. Thank you!

      – mosyonal
      Nov 16 '18 at 15:16











    • The Java to Kotlin conversion tool converts it to a while loop too.

      – Zoe
      Nov 16 '18 at 17:15











    • i < 6 is redundant here. actually it's clear from the first code block

      – Maxim
      Nov 16 '18 at 18:38













    • @Maxim I know, thanks. I'm showing how to rewrite the code in the general case when there is no such dependency between the two loop variables.

      – yole
      Nov 17 '18 at 8:59











    • @yole ah now I see

      – Maxim
      Nov 17 '18 at 19:03














    5












    5








    5







    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
    }





    share|improve this answer













    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
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 16 '18 at 14:42









    yoleyole

    61.7k11159149




    61.7k11159149













    • yeah I know these, I think maybe there is a way but I can't find it so I asked here. Thank you!

      – mosyonal
      Nov 16 '18 at 15:16











    • The Java to Kotlin conversion tool converts it to a while loop too.

      – Zoe
      Nov 16 '18 at 17:15











    • i < 6 is redundant here. actually it's clear from the first code block

      – Maxim
      Nov 16 '18 at 18:38













    • @Maxim I know, thanks. I'm showing how to rewrite the code in the general case when there is no such dependency between the two loop variables.

      – yole
      Nov 17 '18 at 8:59











    • @yole ah now I see

      – Maxim
      Nov 17 '18 at 19:03



















    • yeah I know these, I think maybe there is a way but I can't find it so I asked here. Thank you!

      – mosyonal
      Nov 16 '18 at 15:16











    • The Java to Kotlin conversion tool converts it to a while loop too.

      – Zoe
      Nov 16 '18 at 17:15











    • i < 6 is redundant here. actually it's clear from the first code block

      – Maxim
      Nov 16 '18 at 18:38













    • @Maxim I know, thanks. I'm showing how to rewrite the code in the general case when there is no such dependency between the two loop variables.

      – yole
      Nov 17 '18 at 8:59











    • @yole ah now I see

      – Maxim
      Nov 17 '18 at 19:03

















    yeah I know these, I think maybe there is a way but I can't find it so I asked here. Thank you!

    – mosyonal
    Nov 16 '18 at 15:16





    yeah I know these, I think maybe there is a way but I can't find it so I asked here. Thank you!

    – mosyonal
    Nov 16 '18 at 15:16













    The Java to Kotlin conversion tool converts it to a while loop too.

    – Zoe
    Nov 16 '18 at 17:15





    The Java to Kotlin conversion tool converts it to a while loop too.

    – Zoe
    Nov 16 '18 at 17:15













    i < 6 is redundant here. actually it's clear from the first code block

    – Maxim
    Nov 16 '18 at 18:38







    i < 6 is redundant here. actually it's clear from the first code block

    – Maxim
    Nov 16 '18 at 18:38















    @Maxim I know, thanks. I'm showing how to rewrite the code in the general case when there is no such dependency between the two loop variables.

    – yole
    Nov 17 '18 at 8:59





    @Maxim I know, thanks. I'm showing how to rewrite the code in the general case when there is no such dependency between the two loop variables.

    – yole
    Nov 17 '18 at 8:59













    @yole ah now I see

    – Maxim
    Nov 17 '18 at 19:03





    @yole ah now I see

    – Maxim
    Nov 17 '18 at 19:03













    0














    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!)






    share|improve this answer




























      0














      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!)






      share|improve this answer


























        0












        0








        0







        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!)






        share|improve this answer













        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!)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 17:17









        giddsgidds

        1,483128




        1,483128






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339952%2ffor-loop-with-multivariable-in-kotlin%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python