In any programming language, we need control statements to control the flow of the program based on the output of a condition. For example, if a number is even then display “even number” but if the number is odd then display “odd number”. To achieve this in programming, we need to use control statements that check whether a condition is satisfied or not, if it does then do this, if not then skip this. In kotlin we use expressions to control flow in the program. In this tutorial, we will learn several types of expressions used in Kotlin.
1. If expression
2. If..else expression
3. If..else if..else expression
4. Nested expressions
1. Kotlin – If expression
If expression is pretty simple to understand. Lets have a look at the syntax of if expression –
if(condition){ // Statements that need to be executed if condition is true ... }
Here we have a condition in the if expression, if the condition returns true then the statements inside the body of if expression are executed, if the condition returns false then they are completely ignored. Lets take an example to understand this:
If expression example
In this example, if the given number is even then we are displaying “Even Number” in the output, else we are skipping the statements inside “if”.
fun main(args: Array<String>) { val number = 100 // if expression if (number%2 == 0) println("Even Number") println("Out Of If statement") }
Output:
Even Number Out Of If statement
Lets change the value of variable “number”.
fun main(args: Array<String>) { val number = 101 if (number%2 == 0) println("Even Number") println("Out Of If statement") }
Output:
Out Of If statement
2. Kotlin – If..Else expression
If..Else expression is used when we need to perform some action if a condition is true and we need to perform a different action when a condition is false. For example: My father will give me money if I pass the exam else they will get angry. If I have to write this in programming then I would do it like this –
fun main(args: Array<String>) { // Marks out of 100 val marks = 90 if (marks < 30) { println("Father will get angry") } else { println("Father will give me money") } }
Output:
Father will give me money
Since the condition returned false, the statement inside “if” was skipped and the statement inside “else” was executed.
Syntax of if..else expression:
if(condition){ // Statements that will be executed if the condition returns true ... } else{ // Statements that will be executed if the condition returns false ... }
If..else expression example
In this example, we are checking a number to see whether it is positive or negative.
fun main(args: Array<String>) { // Traditional usage val num = -101 if (num < 0) { println("Negative number") } else { println("Positive number") } println("Out of if else statement") }
Output:
Negative number Out of if else statement
3. Kotlin – if..else if..else ladder expression
In this expression we have one “if” block, one “else” block and one or more “else if” blocks. This is used for checking multiple conditions.
If..else if..else expression example
In this example, we have a number and we are checking whether it’s a negative number, single digit number, two digit number or multiple digit number. We are checking these multiple conditions using if..else if..else expression. When none of the condition returns true then the statements inside the “else” block gets executed.
fun main(args: Array<String>) { val num = 99 if(num<0) println("Number is Negative") else if (num>0 && num<10) println("Single digit number") else if (num>=10 && num <100) println("Double digit number") else println("Number has 3 or more digits") }
Output:
Double digit number
4. Kotlin – Nested expression
When one expression is present inside another expression body then it is called nesting of expressions. For example if an “if expression” is present inside another “if” then it is called “nested if” expression.
For example:
fun main(args: Array<String>) { val num = 101 if(num<0) println("Negative Number") else { //Nested expression if(num%2 == 0) println("Even Number") else println("Odd Number") } }
Output:
Odd Number
Leave a Reply