Press enter to see results or esc to cancel.

# Lesson 6: Kotlin if else Statement

##### Share this
()

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: Kotlin if else Statement.

### 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:

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: Kotlin if else Statement.

### 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.

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: Kotlin if else Statement.

### 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:

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: Kotlin if else Statement.

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: Kotlin if else Statement

### 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: Kotlin if else Statement.

### Solution

`// Project13`
``````fun main (parameter: Array <String>) {
//coding180.com
print ("Enter first note:")
val nota1 = readLine () !!. toInt ()
print ("Enter second note:")
val nota2 = readLine () !!. toInt ()
print ("Enter third note:")
val nota3 = readLine () !!. toInt ()
var average = (nota1 + nota2 + nota3) / 3
if (average >= 7)
println ("Promoted")
}``````
` //project14`
``````fun main (parameter: Array <String>) {
//coding180.com
print ("Enter a value between 1 and 99:")
val num = readLine () !!. toInt ()
if (num <10)
println ("Has a digit")
else
println ("It has two digits")
}

``````

How useful was this Lesson?

Click on a star to rate it!

Average rating / 5. Vote count:

Be the first to rate this post.!

As you found this post useful...

Follow us on social media!

We are sorry that this lesson was not useful for you!

Let us improve this lesson!

Tell us how we can improve this lesson?