Loading, please wait...

A to Z Full Forms and Acronyms

Explain Control Flow in Kotlin | Kotlin Tutorial

Jan 26, 2022 #KotlinLanguage #Programming, 2872 Views
In this article, we will discuss the control statements used in Kotlin under which the following topics are covered - What are control statements?What are the If-else statement and its type?What is the when statement?What is the for loop statement?What is the while loop statement?What is the do-while loop statement?

Explain Control Flow in Kotlin | Kotlin Tutorial

In this article, we will discuss the control statements used in Kotlin under which the following topics are covered - 

  • What are control statements?
  • What are the If-else statement and its type?
  • What is the when statement?
  • What is the for loop statement?
  • What is the while loop statement?
  • What is the do-while loop statement?

What is the control statement?

Control statements are used to control the flow of a program. The statement controls the execution of another statement in the program. It is also known as a decision-making statement. 

There are multiple types of control statements:

  • if-else statement
  • if-else if-else ladder statement
  • Nested if statement
  • When statement
  • for loop
  • while loop
  • do-while loop statement

if statement and its type

If a statement is used to decide based on “True” and “False”. If the condition is true, it will execute the next statement, otherwise, the control will go to the “false” statement. 

Kotlin does not use if statement standalone. So, It is always used with the else statement. In kotlin, we assign the result into a variable. 

if-else statement

Syntax:

val variable = if(condition) {

   #set of statements

}

else

{

    #set of statements

}

 

if-else statement ladder

Syntax:

val return_variable = if(condition)

{

     #set of statements

}

else if(condition) {

   #set of statements

}

else {

   #set of statements

}

Nested if ladder

Syntax:

val resultant_var = if(condition) {

   if(condition){

       #set of statements

}

else

{

    #set of statements

}

    #set of statements

}else if(condition) {

    #set of statements

}

else{

    #set of statements

}

when statement

When the statement is the substitute of the switch statement in Kotlin language. It works exactly similar to the switch statement. It is a conditional statement that returns the value. 

Syntax:

val return_value = when(value) {

 

     # set of statements 

}

 

It can also be used without expression. 

Example:

when(value)

{

      # set of statements

}

Multiple statements can be used within the block of conditions. 

for loop statement

For loop is used to execute the set of statements repeatedly until the condition turns out to be false. It is used in all programming languages. 

Syntax:

for(items of collections)

{

    #body of for loop 

}

Collections can be arrays, ranges, and can be any other item that can be iterated. 

while loop statement

It executes the part of the program several times until the condition is satisfied. Once it turns out to be false. It works similarly to the way it works in other programming languages. 

Syntax:

while(condition)

{

    #set of statements

}

do-while loop statement

It works similar to the while statement but the only difference is it will execute the “do” block once then check the condition of the while loop. However, in the while statement it checks the condition then executes the statements. 

Syntax:

do{

   #set of statements

}

while(condition)

{

   #set of statements

}

A to Z Full Forms and Acronyms

Related Article