In programming, conditional statements are constructs that make it possible to control a program flow based on conditions. The condition can, for example, refer to a specific variable or the state of a program. If the condition is met, the code within the conditional statement is executed. Otherwise the code is skipped or an alternative code block is executed.
There are two types of conditional statements in Kotlin: if statements and when expressions.
The if statement allows an action to be performed if a condition is true. The syntax of the if statement is similar to other programming languages, but there are some differences.
The general syntax of if statement in Kotlin is as follows:
Here is a simple example:
This code defines a variable called number with the value 10. Then a conditional statement is introduced with the if statement. The condition is number > 5, which means that the code block inside the curly braces will only be executed if the variable number greater than 5 is. In this case that is the case, there number the value 10 has that is greater than 5 is. Therefore, the code inside the curly braces is executed and the output “The number is greater than 5” is printed on the console.
Basically, if the code block consists of only one expression, the curly braces can be omitted:
The if-else statement allows one of two blocks of code to be executed depending on whether a condition is true or false. The syntax of if-else statement in Kotlin is as follows:
This code defines a variable called age with the value 18. Then a conditional statement with the if-Instruction initiated. The condition is age > = 18, which means that the block of code inside the curly braces will only be executed if the person's age is 18 or older. In this case that is the case because age is the value 18 has, which means that the person is of legal age. Therefore, the code inside the first curly braces is executed and the output “You are of legal age” is printed on the console. If the condition is false (i.e. the person's age is under 18), the code within the elsestatement executed and the output “You are a minor” is printed on the console.
The if statement can also return a value. For example, we can find the maximum of two numbers:
If additional actions need to be performed to determine the returned value, these actions can be placed in code blocks:
At the end of each block the returned value is specified.
Kotlin also has that if-else-if statement, which allows checking multiple conditions and executing different blocks of code depending on the conditions. The syntax for the if-else-if statement is as follows:
Here is an example of using the if-else-if statement to determine the grading system based on the score:
When is a conditional statement in Kotlin that allows checking a variety of conditions and executing the corresponding block of code associated with the appropriate condition.
Here is an example of using when to determine the day of the week based on the day of the week:
Comparison with group of values
A comparison can be made with a group of values. It is possible to define the same actions for several of these values at the same time. In this case the values are listed separated by commas,
You can also compare a value to a whole range of values using the in operator .
If the in operator makes it possible to find out whether a value is within a certain range, the combination of operators allows !in checking whether a value is not contained in a specific sequence.
when expression and dynamically calculated values
The when expression can also be compared with dynamically calculated values.
When can also accept a dynamically calculated object.
It is even possible to define variables that will be available within the when expression.
The construction When can also be used as an alternative to if..else be used. The value of an object does not necessarily have to be compared. Instead, a series of conditions are checked. Once one of the conditions returns true, the associated set of statements is executed.
Similar to the if construct, the when expression return a value. The result returned is through the arrow operator -> Are defined.