You are here
Home > Kotlin

Kotlin: Control structure

control structure

Control structure

Same as all other languages kotlin also has control structure like when expression it is same as switch-case, if-else, if-if-else-if.

When Expression

  • The switch statement is supported by all other languages like c, java but kotlin not support switch statement.
  • Kotlin use powerful form of the switch statement, When expression similar to switch it accept argument and series of conditions.
  • Kotlin support two types of when expression:

1) when with argument
2) when with-out argument

When with argument

  • It is same as switch it accepts the argument and compare it to the number of constants.
  • It has final block else, when any case is not match the else block is executed.

Example:
fun main(args:Array<String>) {
var day= “Friday”;
when(day)
{
“Monday”->{ println(“Today is monday”);}
“Tuesday”->{println(“Today is tuesday”);}
“Wednesday”->{println(“Today is wednesday”);}
“Thursday”->{println(“Today is Thursday”);}
“Friday”->{println(“Today is friday”);}
“saturday”->{println(“Today is saturday”);}
else->{println(“Today is sunday”);}
}
}
output:

Explanation:
In this example we declare a day variable and initialize it with Friday and the case expression is true.

When without argument

  • This is the second form of when expression. It is the replacement of the if….else clause.

Example:
fun whenwithoutarg(a:Int,b:Int)
{
when{
a>b-> println(“a is grater than b”);
a<b-> println(“a is less than b”);
else->println(“a and b are equals”);
}
}
fun main(args:Array<String>)
{
var a=10;
var b=20;
whenwithoutarg(a,b)
}
output:

If- else statement

  • If-else statement is used to take right action if condition is true and if false to take other action.
  • It is not only statement it is also called ternary operator (?:). If condition is true the part after the question-mark (?) will be executed otherwise part after the column (:) will be executed.

Syntax:
If(condition)
{
//true block
}
Else
{
//false block
}
Example:
fun main(args:Array<String>) {
print(“Enter your percentage:”);
var p:Double= readLine()!!.toDouble();
if(p>90.0)
{
println(“your grade is A”);
}
else
{
println(“your grade is B”);
}
}
output:

If-else ladder statement

  • If-ifelse-else is used to check conditions if all condition fails the else part will be executed.

Example:
fun main(args:Array<String>) {
print(“Enter your percentage:”);
var p:Double= readLine()!!.toDouble();
if(p>=90.0)
{
println(“your grade is A”);
}
else if(p>80 && p<90)
{
println(“your grade is B”);
}
else if(p>50 && p<=80)
{
println(“your grade is c”);
}
else if(p>30 && p<=50)
{
println(“your grade is D”);
}
else
{
println(“you are fail”);
}
}
output:

Keep Visiting TechwithR.com for more upcoming top technical articles and also see my new blog Mag4info. and for kotlin tutorial also see my blog kotlin category.

You can also by SQL 70-761 book write by me from Amazon.

You can also by SQL 70-761 book write by me from Amazon.
You can also by Kotlin Book write by me from Amazon.

Leave a Reply

Top