T E C h O C E A N H U B

How to use Loops in Kotlin(for, when, while, do while, continue and break keywords)-Chapter-5

There are different loops and operators in every language and they are more or less the same in functionality but the syntax can vary. There are different types of looks like:-

                            if, when, for while, do-while, break, continue.

If the loop is used when the condition inside the if the statement is true, otherwise else statement is executed. Below is an example of an if loop.

 
   package adapter.manasvi.com.simpleproject
   import android.os.Bundle
   import android.widget.Toast
   import androidx.appcompat.app.AppCompatActivity
   import kotlinx.android.synthetic.main.activity_main.*
    class LoopActivity : AppCompatActivity() {
        var num1 = 5
        var num2=9
        override fun onCreate(savedInstanceState: Bundle?) {
           super.onCreate(savedInstanceState)
           setContentView(R.layout.activity_main)
           if(num2>num1) {
               textView.setText("num2 is greater than num1 " + num2)
            }    
         }
     }

When loop in Kotlin is used as replacement of switch statement. Below given example of a when loop.

    package adapter.manasvi.com.simpleproject
    import android.os.Bundle
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity
    import kotlinx.android.synthetic.main.activity_main.*
    class LoopActivity : AppCompatActivity() {
       var num1 = 5
       var num2=9
       override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            var numberProvided = when(num1) {
               1 -> "One"
               2 -> "Two"
               3 -> "Three"
               4 -> "Four"
               5 ->  textView.setText("In  fifth case")
               else -> "invalid number"
            }
             //textView.setText("num2 is greater than num1 ")
        }
     }

For loop is used when we want to print an iterate into a set of string or it can be a number. I have shown an example to elaborate on how for works and even I have added a break statement which breaks the for loop.

   package adapter.manasvi.com.loopactivity
   import androidx.appcompat.app.AppCompatActivity
   import android.os.Bundle
   import kotlinx.android.synthetic.main.activity_main.*
   import java.lang.Math.log
   class MainActivity : AppCompatActivity() {
       override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
           setContentView(R.layout.activity_main)
  
           val fruitName = listOf("Apple", "Banana", "Mango")
           for (name in fruitName) {
               if(name == "Banana"){
                 break
               }
              val fruitName = listOf("Apple", "Grapes", "Mango")
              textView.text= fruitName.toString()
          }
       }
    }

Last but not the least continue. The continue skips the current iteration of the enclosing loop, and the control of the program jumps to the end of the body of loop.

   package adapter.manasvi.com.loopactivity
   import android.os.Bundle
   import androidx.appcompat.app.AppCompatActivity
   import kotlinx.android.synthetic.main.activity_main.*
   class continueActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            val fruitName = listOf("Apple", "Banana", "Mango")
            for (name in fruitName) {
                 if (name.equals("Banana")) 
                    continue
                    textView.text = fruitName.toString() 
                 }
           }
      }

While loop is used to iterate a block of code unless the condition becomes false. Let us see a simple example of while loop.

  import android.os.Bundle
  import androidx.appcompat.app.AppCompatActivity
  import kotlinx.android.synthetic.main.activity_main.*
  class continueActivity : AppCompatActivity() {
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
 
          setContentView(R.layout.activity_main)
          var number = 1
          while (number <= 10) {
               textView.text= number.toString()
               number++;
           }
       }
   }

Now do-while loop, It is not that different from the while loop despite it runs the statement once beside it is false.

  package adapter.manasvi.com.loopactivity
  import android.os.Bundle
  import androidx.appcompat.app.AppCompatActivity
  import kotlinx.android.synthetic.main.activity_main.*
  class doActivity : AppCompatActivity() {
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
  
          setContentView(R.layout.activity_main)
          var num = 1
          do {
              textView.text= num.toString()
              num++
           }
          while (num<=10)
       }
    }

Feel free to try the above examples and do feel free to comment.

Copyright ©TechOceanhub All Rights Reserved.