Lesson 7: If condition as an expression in Kotlin language

Lesson 7: If condition as an expression in Kotlin language

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

If condition as an expression in Kotlin language. In Kotlin there exists the possibility that the conditional if structure returns a value, a characteristic not common to other programming languages.

We will see with a series of examples the syntax to use the if as expression.

Problem 1

Load two integer values, store the largest of them in another variable and display it.

Project15 – coding180project.kt

fun main (parameter: Array <String>) {
//coding180.com
     print ("Enter first value:")
     val value1 = readLine () !!. toInt ()
     print ("Enter second value:")
     val value2 = readLine() !!. toInt ()
     val greater = if (value1 > value2) value1 else value2
     println ("The greater between $value1 and $value2 is $greater")
 }
Lesson 7: If condition as an expression in Kotlin language - Lesson 7: If condition as an expression in Kotlin language - Lesson 7: If condition as an expression in Kotlin language - If condition as an expression in Kotlin language
If condition as an expression in Kotlin language

As we can see, we assign a variable named greater than the value returned by the if expression. If the condition of if is true returns the contents of the variable value1 and if it returns value2:

     val greater = if (value1> value2) value1 else value2

The Kotlin compiler infers that the largest variable must be of type Int since both value1 and value2 are Int.

The keys are not available because there is a single operation for both the true and the false.

The most common is to use an if as an expression where a value is returned requiring a single activity for the true and the false, but the language allows us to have more than one instruction per branch of the if.

Problem 2

Enter an integer value by keyboard. Store in another variable the square of that number if the value entered is even, in case it is odd to save the cube.
Also display a message indicating whether the square or cube is calculated.

Project16 – coding180project.kt

fun main (parameter: Array <String>  ) {
//coding180.com
     print ("Enter an integer value:")
     val value = readLine () !!. toInt ()
     val result = if (value% 2 == 0) {
         print ("square:")
         value * value
     } else {
         print ("Cube:")
         value * value * value
     }
     print (result)
 }

In this problem we do more than one activity by the branch of the true and false of if, so they are enclosed in braces.

It is important to keep in mind that the last line of each block of if is the one that is returned and stored in the result variable:

     val result = if (value% 2 == 0) {
         print ("square:")
         value * value
     } else {
         print ("Cube:")
         value * value * value
     }

We use the “%” operator (remainder of a division) to identify whether a variable is even or odd. The remainder of dividing a value by the number 2 is zero (eg 10% 2 is zero)

Keep in mind that it is not the same to do:

     val result = if (value% 2 == 0) {
         value * value
         print ("square:")
     } else {

Proposed problem

  • Load an integer value per keyboard from 1 to 99. Store in another variable the number of digits that has the value entered by keyboard.
    Show the number of digits of the number entered by keyboard.

Solution

 //Project17

About The Author

Related posts

Leave a Reply