Kotlin Conditional Statements
If StatementsSometimes we may want to run our code only when a condition holds true. For example, let's hypothetically assume that we run a bar and we only want to admit users who are 18 years old and above.
in the code below we're trying to access our code with a preset age of 16, since 16 is less than 18. if we run the code "Sorry you cannot Register" will be printed to the console
Please Pay Attention to the comment within the block of code and Practice them too!
in the code below we're trying to access our code with a preset age of 16, since 16 is less than 18. if we run the code "Sorry you cannot Register" will be printed to the console
Please Pay Attention to the comment within the block of code and Practice them too!
class KotlinConditionals { fun main(args: Array<String>) { val age: Int = 16 // << Preset Age if (age < 18){ println("Sorry you cannot Register") } else if (age < 21){ println("Well you can Register") // this will run only if age is between 18 to 20 } else{ println("Welcome! You can Register") // will run if age is 21 to infinity integer } println("Some code here") // Any code inserted here will always run irrespective of the conditionals } }
When Statements
Please Pay Attention to the comment within the block of code and Practice them too!class KotlinConditionals { fun main(args: Array<String>) { val x: Int = 1 when (x) { 1 -> print("it is ONE") // when x is 1 2 -> print("it is TWO") // this is a statement 3 -> { val charLength = "String".length // charLength = 6 print("character length of 'String' is $charLength") // this line will print 'character length of 'String' is 6' } 2 * 2 -> print("it is FOUR") // when x = 2 x 2 = 4 5 -> print("it is FIVE") "Hello!".length -> print("it is SIX") // when the length of 'Hello' is SIX, print ''it is SIX in 7..10 -> print("x is between 7 and 10") // when x is in the range of 7 to 10 print the statement // y..z indicates a range of integers between y and z in 10..20 -> print("x is between 10 and 20") !in 1000..2000 -> print("x is not between 1,000 and 2,000") // when x is not in the in the range of 1000 to 2000 print the msg // exclamation mark (!) negates a statement //... // is that not Beautiful? } } }
Kotlin Conditionals as Expressions
- An expression is any section of the code that evaluates to a value.
- A statement is a complete line of code that performs some action.
class KotlinConditionals { fun main(args: Array<String>) { val z: Int = 3 val ifExpression = if (z < 12) { "Hello World" } else { "Good Morning!" } println(ifExpression) //this will print 'Hello World' because 3 is less than 12 val whenExpression = when (z) { 1 -> "it is ONE" // it'll return 'it is ONE' to whenExpression 2 -> "it is TWO" 3 -> { "First Expression inside Block" // this expression will be ignored "Second Expression inside Block" // this expression will be returned because only last expressions are returned } 4 -> { println("This will be printed to the console") "This Line will be returned" } //5 -> 17 // this will cause an error and your code will not compile unless removed, hence all Expression within a kotlin conditional must be of the same type else -> "More than 5" // 'else' condition is necessary, as it must return something there buy eliminating a NullPointerException } println(whenExpression) // will print 'Second Expression Inside Block' because z = 3. } }
Comments
Post a Comment