Techoceanhub logo

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 languages and they are more or less the same in functionality but syntax can vary.There are different types of loops 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 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.

Share:

Categories

Archives

Related Posts

Array Destructuring

Array Destructuring Array destructuring is a powerful syntax introduced in ES6 that allows you to unpack elements from an array

Read More »

Explore our Affiliated partner links

hostgator website plan

Hostgator

Best Website hosting plans & Best for 24/7 support.

hostinger webhosting plan

Hostinger

Reasonable website hosting packages for small business & 24/7 Email support

Elementor pro

Design Worpdress website quickly using Elementor Builder and AI

buy elementor website templates

Buy Elementor Website Template

Buy ready made Elementor Website Template from us & Get Free Elementor pro licence for 1 year .