Kotlin Loop

Loops allow us to repeat a certain piece of our code over a specified number of times.

For Loops

for loops should be used when we know how many iterations we need in advance.

Let’s say you want to print ‘Hello World’ one hundred times (iteration), we do not need to write the statement a hundred times in a hundred lines of code, loops would make it easy to achieve in 3 lines of code

While Loop

while loops should be used when we do not know how many iterations we need, example is when we want to read a text file line by line. The text file will vary from one application to the other, therefore we cannot know the exact number of lines in the file for different applications. Here while loops are suitable

You may not find yourself using this very often but if you come into a situation that you want to use something like this, now you know how you can do it!

Thanks for reading, do not forget to try out the code for yourself [button color=”red” size=”medium” link=”https://try.kotlinlang.org/#/Examples/Hello,%20world!/Simplest%20version/Simplest%20version.kt” icon=”” target=”true”]Try out Kotlin[/button]