0

How can I make it in kotlin using for loop?

for (double i = 0; i < 10.0; i += 0.25) {
    System.out.println("value is:" + i);
}
Agent_L
  • 4,960
  • 28
  • 30
  • 4
    You'd be ill-advised to do this in any language that uses a binary floating point type. Use an integral type and divide by 4 at the point of use. – Bathsheba Jul 21 '17 at 09:34
  • I don't think you can make it in kotlin's `for loop`, since the docs says "for iterates through anything that provides an iterator": https://kotlinlang.org/docs/reference/control-flow.html#for-loops ... Use kotlin's `while` instead – samAlvin Jul 21 '17 at 09:38
  • 1
    @Bathsheba: 0.25 works well in binary, though. I'd still avoid floats in loop variables, because adding 0.25 may be a no-op if the base number is huge, and then you end up with an infinite loop. That cannot happen with integers (an integer counting loop will always terminate, even though it can take a very long time if you wrap around at the end) – Thilo Jul 21 '17 at 09:43
  • 1
    Indeed 0.25 is a dyadic rational, so adding this particular constant successively from 0 will work well. A comment in the source code on the lines of "I know what I'm doing" wouldn't go amiss. – Bathsheba Jul 21 '17 at 09:44

4 Answers4

3

You should use the Intellij plugin for converting Java code for Kotlin. It's pretty neat (unless you have complex code using lambdas) This is what it converts to for your given question:

  var i = 0.0
  while (i < 10.0) {
      println("value is:" + i)
      i += 0.25
  }
aygavras
  • 170
  • 2
  • 11
1

Here is the kotlin code equivalent to for loop.

var i = 0.0
while (i < 10.0)
{
  println("value is:" + i)
  i += 1.0
}
Chirag
  • 56,621
  • 29
  • 151
  • 198
1

Kotlin for loop only support iterate the arrays.Please refer https://kotlinlang.org/docs/reference/control-flow.html

It's achievable in different way

var length:Double = 10.0 
var increment:Double = 0.25
for (index in Array((length/increment).toInt(), { i -> (i * increment) })) 
println(index)
Bala
  • 26
  • 3
0

I'm not sure if this syntax is new, but natural numbers may be used to iterate values like so.

(0..(10*4)).map {
    it / 4.0 as Double
}.forEach {
    println(it)
}

I'm avoiding iteration on IEEE 754 floating points, as that could potentially cause errors. It may be fine in this case, but the same may not be true depending on environment, context, and language. It's best to avoid using floating point except in cases where the numbers are expected to have arbitrary precision, i.e. uses real numbers or continuity.

This syntax may be used within a for loop as well if you don't need to store the result. However, for loops should be avoided for the most part if you want immutability (which you should).

for (i in 0..10) {
  println("Project Loom is a lie! $i")
}
  • 1
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Pouria Hemi Dec 05 '20 at 05:00