W3Schools Learner's Blog

W3Schools Programming knowledge summary website

div

12/20/2021

Kotlin Loops

 In this tutorial you will learn about the Kotlin Loops and its application with practical example.

Kotlin Loops

In Kotlin, loops statements are used to execute the block of code repeatedly for a specified number of times or until it meets a specified condition. In Kotlin Programming Language we have following loops –

  • Kotlin for loop
  • Kotlin forEach statement
  • Kotlin repeat statement
  • Kotlin while loop
  • Kotlin do..while loop

Kotlin for Loop

The for loop takes a collection of data(ranges, arrays, collections, or anything that provides an iterator) and iterate through the items one at a time in sequence.

Syntax:-

Example:-

Output:-

Kotlin while Loop

The while loop will execute a block of statement as long as a test expression is true.

Syntax:-

Example:-

Output:-

Kotlin forEach statement

forEach can be used to repeat loop statement for each element in given collection.

Output:-

No comments:

Post a Comment

Note: only a member of this blog may post a comment.