What is difference between When and If-Else in Kotlin?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is difference between When and If-Else in Kotlin??

I didn't clearly understand the difference between if-else statements and when statements in Kotlin. What does they do different? They seemed to do the same thing for me.

2nd Aug 2019, 10:39 PM
Plaban Kr. Mondal
Plaban Kr. Mondal - avatar
2 Answers
+ 2
when can only have statements, that can only have 1 statement, that returns any value, this will allow you to strictly assign a when statement to a variable. I think when is only a bit similar to switch.
3rd Aug 2019, 12:22 PM
Seb TheS
Seb TheS - avatar
0
No I was asking about when not while😅 .. while is there but I'm asking for the difference between when and if-else. Maybe you are familiar with switch. In the community course of Kotlin says that when is equivalent to Switch in other languages.
3rd Aug 2019, 5:45 AM
Plaban Kr. Mondal
Plaban Kr. Mondal - avatar