Why I should name the second loop the same name as the first one?


#1

Hello Guys, I would like to know why in The loop number 2 “Play loop” when I name it “play” it gives me an error and I should name it “index” as the one above? Yaa the two loop are connected together but why I should name it “Index” ??

 fun main(args: Array<String>) {
        var hello = Array<String> (4) {""}
        for (index in 0..3){
            print("index [ $index ] = ")
            hello[index] = readLine()!!

        }
        for (play in 0..3) {
            println("Array [ $play]=" +hello[index] )

        }
    }

#2

You shouldn’t name it index, but you should use the same variable name in all places in your loop. The code you’ve posted has hello[index], and you should change it to hello[play].


#3

Aaah, It works! Thank You!