Introduction to loops 

A loop is a cyclic code, routine, or statement that is defined once but may run several times, and can perform a set of instructions indefinitely or repeatedly (once for each of a collection of items, or until a condition is met). If we were asked to print numbers from 1 to 3 on the  screen, this could have been done easily by writing the code using the three println statements. If we were to print hundreds or thousands of numbers, for example, we would need a better solution for performing one task repeatedly for as long as required. Loops are a great solution for these situations, and they are dependent on three parts:

  • Start: Defining the beginning of the loop 
  • Body: Defining the code block to execute it on each iteration 
  • Controller: Defining when the loop should stop

In Kotlin there are two type of loops available:

  • Condition-controlled loop: 
    • While loop
    • Do while loop
  • Count-controlled loop:
    • For loop

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.34.39