'If...Else' is somewhat similar to Decision Making.
Everyone has to make some kind of Decision in everyday life. Be it choosing a shirt or going to office by Bus or Cab.
Even in case of Kotlin, it has to make Decision.
And to make a Decision, 'If...Else' is used by Kotlin.
There are three Decision Making statements used by Kotlin :
Let us see them in detail.
Let us understand 'if' statement with the below example:
Now, if your friend has to write the same thing in Kotlin. How would he write it?
Let us see with the below example.
fun main() { var price = 120 if (price < 150) { println("Buy the apples") } }
So, your friend writes the same thing in Kotlin for the same statement, "If the price of Apple is less than 150 then buy the apples".
if (price < 150) { println("Buy the apples") }
Quite Simple! Right?
It works in two steps :
if (price < 150) { println("Buy the apples") }
Now, that we have seen the 'if' statement. Let us look at the syntax of 'if' statement in detail.
if (price < 150) { println("Buy the apples") }
The above 'if' statement begins with a 'if', followed by the actual condition (i.e. price < 150) then the 'if' block starts with a brace '{' and ends with a brace '}'.
if (price < 150) { ... }
Now, if the above condition (i.e. price < 150) is 'true'. The 'print(...)' statement is executed.
There can be lot of statements inside the 'if' statements. What I meant is, there can be a lot of print statements.
if (price < 150) { println("Buy the apples") println("The apples should be of good quality") println("Take the apples in a carry bag") }
But the below statement is incorrect.
fun main() { var price = 120 if (price < 150) { println("Buy the apples") } }
All we have done is, placed the starting brace, '{' in the next line of the 'if' declaration.
if (price < 150) { println("Buy the apples") }
And we get the error message,
That states, the starting brace '{' needs to be right after 'if' and not in the next line of 'if'.
So the moral of the story is, the way to declare 'if' block is,
if (price < 150) { println("Buy the apples") }
And not,
if (price < 150) { println("Buy the apples") }
So, in the above example we have seen your friend is happy because the price of apple is less than 150.
But what if the price of apple is more than 150. Say, the price of apple is 180. And he doesn't know what to do..
fun main() { var price = 180 if (price < 150) { println("Buy the apples") } }
And there is no output. Because the price of apple is 180 which is more than 150.
So, your friend comes back to you and you tell him to use 'else' statement with 'if'.
So, you tell your friend,
So, he writes the same thing in Kotlin using 'else' statement.
fun main() { var price = 180; if (price < 150) { println("Buy the apples"); } else { println("Do not buy apples"); } }
So, to implement the line,
"If the price of Apple is less than 150 then buy the apples else do not buy the apples".
We have used the 'else' statement along with 'if'.
if (pric< 150) { println("Buy the apples"); } else { println("Do not buy apples"); }
The syntax of 'else' statement is almost same as 'if'. Else statement uses the 'else' keyword right after the ending brace, '}' of 'if', followed by a starting brace '{'
And the statements inside 'else' statement, should also be inside the starting brace '{' and the ending brace '}'.
} else { println("Do not buy apples"); }
Now, your friend is happy. But you figured out that the shopkeeper is selling two types of apples for a fixed price.
And you give the new instruction to your friend.
So, this time you tell your friend,
And he writes the same in Kotlin.
fun main() { var price = 200 if (price == 100) { println("Buy 1st kind of apple") } else if (price == 200) { println("Buy 2nd kind of apple") } else { println("Do not buy apples") } }
So, in the else part, if we need to provide a condition (i.e. price == 200).
We need to use 'elif'.
} else if (price == 200) { println("Buy 2nd kind of apple") }
And the syntax of 'else if' is just like 'if' and 'else' statement.