What you NEED to know about WHEN Expressions - Android KOTLIN

Pubblicato il: 01 gennaio 1970
sul canale di: 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  


In questa pagina del sito puoi guardare il video online What you NEED to know about WHEN Expressions - Android KOTLIN della durata di ore minuti seconda in buona qualità , che l'utente ha caricato tutorialsEU - Android 01 gennaio 1970, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 303 volte e gli è piaciuto 10 spettatori. Buona visione!