2

I recently started learning Kotlin and the one thing I noticed is the for-loop syntax of Kotlin is different from traditional for-loop syntax and for me it is a bit confusing...I tried to search it on google but didn't get my answer.

How would I duplicate the following Java for loop?

for (int i = 0; i <= 100; i++) {
  System.out.println(i);
}
spierce7
  • 14,797
  • 13
  • 65
  • 106
Hammad Hassan
  • 81
  • 2
  • 5
  • 1
    Possible duplicate of [Does Kotlin have an "enumerate" function like Python?](https://stackoverflow.com/questions/46825787/does-kotlin-have-an-enumerate-function-like-python) – s1m0nw1 Mar 09 '18 at 05:29
  • 2
    What do you mean by "different from traditional for-loop syntax"? Do you mean different than C `for` loop? Kotlin's implementation is not dissimilar to many other contemporary languages (Swift, F#, etc.). Did you see the docs at https://kotlinlang.org/docs/reference/basic-types.html, which shows how to use `for` loops to iterate through both ranges and collections? I'd suggest you start your research at https://kotlinlang.org. – Rob Mar 09 '18 at 05:50
  • 1
    Hey Rob Thanks for your reply,What i mean by "traditional for-loop syntax is" that the for loop used in many popular programming languages like C,C++,Java etc is different then kotlin..And i see the docs but still there is some confusion for me like If i want to print all the even numbers from 0-100 how would i do that Thanks for being helpful :) – Hammad Hassan Mar 09 '18 at 06:00
  • 2
    Please add an example of what confuses you – guenhter Mar 09 '18 at 06:13
  • There is nothing to explain unless you ask. So please, extend your question with snippets of code you want to discuss and describe what is it that confuses you about them. – voddan Mar 09 '18 at 08:58
  • You should be looking at the Kotlin reference material for this question, not asking questions on the Internet. – user207421 Mar 11 '18 at 01:11
  • 1
    I disagree. It's fine to ask a question like this. @s1m0nw1 I also don't the example above is a duplicate of this question. I think a lot of people could be helped by this question. – spierce7 Mar 11 '18 at 01:14

2 Answers2

15

Here is a Java for loop to iterate 100 times:

for (int i = 0; i <= 100; i++) {
  System.out.println(i);
}

Here is the Kotlin equivalent:

for (i in 0..100) {
  println(i)
}

Here is a Java for loop that will iterate through a list:

for (int i = 0; i < list.size(); i++) {
  Object item = list.get(i);

  // Do something with item
}

Kotlin equivalent:

for (i in list.indices) {
  val item = list[i]

  // Do something with item
}

Here is another Kotlin equivalent for iterating a list:

for (i in 0 until list.size) {
  val item = list[i]

  // Do something with item
}

Java for-each loop:

for (Object item : list) {
  // Do something with item
}

Kotlin for-each loop:

for (item in list) {
  // Do something with item
}
spierce7
  • 14,797
  • 13
  • 65
  • 106
0
val scanner = Scanner(System.`in`)

var nos = Array<Int>(5) { 0 }

for (i in 1..3) {
    nos[i] = scanner.nextInt()
}

println("Given values $nos")

Here, you can see i in 1..3 and you do not need to declare var i : Int = 1 as it'll be declared for you in the loop. Nor do you need the i = i+1 inside the loop for that matter.

esu
  • 2,220
  • 8
  • 19
  • 30