Lesson 6: Conditional if Statement in Kotlin Language

Lesson 6: Conditional if Statement 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

Conditional if Statement in Kotlin Language. When a decision is made, conditional structures appear. In our daily life we have situations where we have to decide.
Do I choose race A or race B?
Do I wear these pants?
To go to work, do I choose path A or path B?
When taking a course, do I choose the shift tomorrow, afternoon or evening?

Topic: Conditional if Statement in Kotlin Language.

Simple conditional structure

When the election is presented we have the option to carry out an activity or not to carry out any activity.

Graphic representation:

Lesson 6: Conditional if Statement in Kotlin Language - Lesson 6: Conditional if Statement in Kotlin Language - Lesson 6: Conditional if Statement in Kotlin Language - Conditional if Statement in Kotlin Language
Conditional if Statement in Kotlin Language.

We can observe: The rhombus represents the condition. There are two options that can be taken. If the true condition follows the path of the true, or the right, if the condition gives false follow the path of the left.

It is a SIMPLE CONDITIONAL structure because along the path of the true there are activities and by the way of the false there are no activities.

By the way of the true there may be several operations, inputs and outputs, and we will see that there may be other conditional structures.

Topic: Conditional if Statement in Kotlin Language.

Problem 1

Enter the salary of a person, if it exceeds 3000 pesos display a message on the screen indicating that you must pay taxes.

Project10 – Principal.kt

fun main (parameter: Array <String>) {
//coding180.com
     print ("Enter the employee's salary:")
     val salary = readLine () !!. toDouble ()
     if (salary > 3000) {
         println ("Must pay taxes")
     }
 }

The keyword “if” indicates that we are in the presence of a conditional structure; Then we have the condition in parentheses. Finally enclosed in braces the instructions of the branch of the true.

It is necessary that the instructions to execute in case the condition is true are enclosed in braces {}, with them we mark the beginning and the end of the block of the true.

Topic: Conditional if Statement in Kotlin Language.

But there are situations where if we have a single instruction by the branch of the real we can ignore the keys and make our code more concise:

     if (salary > 3000) 
         Println ("Must pay taxes")

In the problems hereafter we will not have the keys if we have only one instruction.

Running the program and we enter a salary above 3000 pesos. We can see how the message “Must pay taxes” appears on the screen, since the if condition is true.

Let’s run the program again and pay a salary less than or equal to 3000 pesos. No message should be displayed.

Topic: Conditional if Statement in Kotlin Language.

Compound conditional structure

When the election is presented we have the option to carry out one activity or another. That is to say we have activities for the true and for the false of the condition. The most important thing to keep in mind is that the activities of the branch of the true or false, the activities of the two branches are NEVER performed.

Graphic representation:

Lesson 6: Conditional if Statement in Kotlin Language - Lesson 6: Conditional if Statement in Kotlin Language - Lesson 6: Conditional if Statement in Kotlin Language -

In a compound conditional structure we have inputs, outputs, operations, both by the branch of the true as by the branch of the false.

Problem 2

Carry out a program that requests to enter two different integers and shows the largest of them (we assume that the program operator enters different values, our program does not validate this situation)

Project11 – 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 ()
     if (value1 > value2)
         print ("The highest value is $value1")
     else
         print ("The highest value is $value2")
 }

Topic: Conditional if Statement in Kotlin Language.

The input of value1 and value2 is made by keyboard. To know which variable has a higher value we ask if the content of value1 is greater (>) than the content of value2 in an if, if the answer is true we print the contents of value1, in case the condition is false, the instruction is executed Followed by the keyword else where we show the content of value2:

     if (value1 > value2)
         print ("The highest value is $value1")
     else
         print ("The highest value is $value2")

As we can see, value1 and value2 are never printed simultaneously.

The keys are optional because we have a single activity for each branch of the if, alternatively we can write:

     if (value1 > value2) {
         print ("The highest value is $value1")
     }
     else {
         print ("The highest value is $value2")
     }

Operators

Only variables, constant values and relational operators should be available in a condition.

Relational Operators in Kotlin:

 > (greater than)	
 < (less than)
 > = (Greater than or equal to)
 <= (Less than or equal to)
 == (same, equal to)
 != (not equal to)
 + (More)
 - (less)
 * (Product)
 / (division)
 % (Remainder of a division) Eg: x = 13 % 5 {save 3}

It should be noted that when setting a condition we must select which relational operator fits the question.

Examples:

	 A number is multiplied by 10 if it is not equal to 0. (!=)
	 Two numbers are entered showing a warning if they are the same.  (==)

The problems that can be presented are infinite and the correct choice of the operator is only reached with intensive practice in problem-solving.

Topic: Conditional if Statement in Kotlin Language.

Problem 3

Two integer values are keyed in. If the first is less than the second calculate the addition and subtraction, then show them, but calculate the product and division.

Project12 – Principal.kt

fun main (parameter: Array <String>) {
//coding180.com 
   print ("Enter the first value:")
    val value1 = readLine () !!. toInt ()
    print ("Enter the second value:")
    val value2 = readLine () !!. toInt ()
    if (value1 < value2) {
        val sum = value1 + value2
        val subtract = value1 - value2
        println ("The sum of the two values ​​is: $sum")
        println ("The subtraction of the two values ​​is: $subtract")
    } else {
        val product = value1 * value2
        val division = value1 / value2
        println ("The product of the two values ​​is: $product")
        println ("The division of the two values ​​is: $division")
    }
}

In this problem we have several activities by the real branch of if so the keys are mandatory:

if (value1 <value2) {
         val sum = value1 + value2
         val subtract = value1 - value2
         println ("The sum of the two values ​​is: $sum")
         println ("The subtraction of the two values ​​is: $subtract")
     } else {
         val product = value1 * value2
         val division = value1 / value2
         println ("The product of the two values ​​is: $product")
         println ("The division of the two values ​​is: $division")
     }

The same situation is produced by the branch of the false, ie by the else we must lock the keys with the block.

Problems proposed

  • Three notes from a student are entered, if the average is greater than or equal to seven, display a “Promoted” message.
  • Enter an integer from 1 to 99, display a message indicating whether the number has one or two digits.(Note that condition must be met to have two digits, an integer)

Topic: Conditional if Statement in Kotlin Language.

Solution

// Project13

About The Author

Related posts

Leave a Reply