What you NEED to know about WHEN Expressions - Android KOTLIN

Veröffentlicht am: 01 Januar 1970
auf dem Kanal: tutorialsEU - Android
303
10

Let me tell you everything you need to know about WHEN Expressions, are they really better than Switch Cases and If/Else statements? Want to learn everything about Kotlin? Get this!    • Android Complete Course  

Here are the links by the way!

Free download. https://page.tutorials.eu/android

Our Android Masterclass. https://bit.ly/3l6vG86

when defines a conditional expression with multiple branches. It is similar to the switch statement in C-like languages.

when matches its argument against all branches sequentially until some branch condition is satisfied.

when can be used either as an expression or as a statement. If it is used as an expression, the value of the first matching branch becomes the value of the overall expression. If it is used as a statement, the values of individual branches are ignored. Just like with if, each branch can be a block, and its value is the value of the last expression in the block.

The else branch is evaluated if none of the other branch conditions are satisfied.

If when is used as an expression, the else branch is mandatory, unless the compiler can prove that all possible cases are covered with branch conditions, for example, with enum class entries and sealed class subtypes).

In when statements, the else branch is mandatory in the following conditions:

when has a subject of an Boolean, enum, or sealed type, or their nullable counterparts.

branches of when don't cover all possible cases for this subject.

To define a common behavior for multiple cases, combine their conditions in a single line with a comma.

You can use arbitrary expressions (not only constants) as branch conditions.

You can also check a value for being in or !in a range or a collection.

Another option is checking that a value is or !is of a particular type. Note that, due to smart casts, you can access the methods and properties of the type without any extra checks.

when can also be used as a replacement for an if-elseif chain. If no argument is supplied, the branch conditions are simply boolean expressions, and a branch is executed when its condition is true.

The scope of variable introduced in when subject is restricted to the body of this when.

#kotlin #programming #android #tutorial #tutorialseu #learn #code

tutorialsEU offers you free video tutorials about programming and development for complete beginners up to experienced programmers.
This includes C#, Unity, Python, Android, Kotlin, Machine Learning, etc.
Stay tuned and subscribe to tutorialsEU: https://goo.gl/rBFh3x

Android:    / @tutorialseuandroid  
C#:    / @tutorialseuc  
Unity:    / @tutorialseuunity  
Facebook:   / tutorialseu-109380204093233  
LinkedIn:   / tutorialseu  
Discord:   / discord  


Auf dieser Seite können Sie das Online-Video What you NEED to know about WHEN Expressions - Android KOTLIN mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer tutorialsEU - Android 01 Januar 1970 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 303 Mal angesehen und es wurde von 10 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!