1

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);
}
4
  • 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. Commented Jul 21, 2017 at 9: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": kotlinlang.org/docs/reference/control-flow.html#for-loops ... Use kotlin's while instead Commented Jul 21, 2017 at 9: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) Commented Jul 21, 2017 at 9: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. Commented Jul 21, 2017 at 9:44

4 Answers 4

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
  }
Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

0

Here is the kotlin code equivalent to for loop.

var i = 0.0
while (i < 10.0)
{
  println("value is:" + i)
  i += 1.0
}

Comments

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 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.