Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Produce output, that counts by 10, from 1000 to 0 using a while loop (Kotlin)

The title speaks for itself. I know how to create a while loop count down from 1000 to 0, that counts down by one:

fun main(args: Array<String>) {
var i = 1000
while (i > 0) {
println(i) 
i--
}}

BUT, I need to count down by ten: 1000, 990, 980. I’m struggling to wrap my head around it for whatever reason. Willing to lend me a hand?

(Please provide explanation. I don’t just want the answer, as I want to understand! I appreciate you all!)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If the decreasing interval is 10, your code i-- needs to be modified to i -= 10

i-- is equivalent to i = i -1, subtracting 1 from the original, which does not meet your requirements.


fun main() {
    var i = 1000
    while (i >= 0) {
        println(i)
        i -= 10
    }
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading