Kotlin. The conditional branch operator if-else

The conditional branch operator if-else. Types of conditional branch operators


Contents


Search other resources:

1. Operator of conditional branch if-else. Types of conditional branch operators. The purpose

In the Kotlin programming language, there are tools that change the computational process depending on the fulfillment of a certain condition.

Such means include the if-else operator.

The conditional branch operator has 3 forms:

  • abbreviated form if;
  • full form if-else;
  • ladder form if-else-if.

 

2. Abbreviated form of the conditional branch operator: if

The general form of the abbreviated form of the conditional jump operator is as follows

if (condition)
  statement;

here

  • condition – a conditional expression that returns a Boolean result. If the value of the conditional expression is true, then the statement is executed. If condition is false, then nothing is executed (the statement following the if statement is executed);
  • statement – some operator.

If you need to execute several statements in the abbreviated if statement, then according to the Kotlin syntax, these statements are enclosed in curly braces { }. In this case, the general form of the if statement is:

if (condition) {
  // statements
  // ...
}

Figure 1 shows a schematic representation of the abbreviated form of an if statement.

Kotlin. The abbreviated form of the if statement

Figure 1. The abbreviated form of the if statement

 

3. Full form: if-else

The full form of the conditional branch operator is

if (condition) {
  // statements if condition=true
  // ...
}
else {
  // statements if condition=false
  // ...
}

here

  • condition – a conditional expression, the value of which determines which group of statements will be executed. If condition = true, then statements after the if keyword are executed. Otherwise, the statements after the else keyword are executed.

If only one statement needs to be executed after the if or else keyword, then the curly braces {} can be omitted.

Figure 2 shows a graphical representation of the if-else operator.

Kotlin. Statement if-else. Graphic designation

Figure 2. Statement if-else. Graphic designation

 

4. Ladder (cascade) form: if-else-if

The general syntax of the ladder form of the if-else-if conditional statement is as follows

if (condition1) {
  // statements1
  // ...
}
else
if (condition2) {
  // statements2
  // ...
}
...
else
if (conditionN) {
  // statementsN
  // ...
}
else {
  // statementsN+1
  // ...
}

here

  • condition1, condition2, conditionN – conditional expressions. If the value of one of the conditional expressions is true, then the corresponding block of statements is executed. All other blocks (branches) of operators are skipped. For example, if condition2 = true, then only one block of statement2 is executed;
  • statement1, statement2, statementN – one or more statements that are executed if the corresponding conditional expression is true. If after some if or else block you need to execute only one statement, then the curly braces { } are optional.

 

5. Examples of using the if statement (short form)
5.1. The absolute value of a number

Task. Given a real number a, which is entered from the keyboard. Print its absolute value. Do not use additional functions in the program.

Solution.

fun main(args:Array<String>) {
  // Statement if(...) { ... }

  // 1. Variable declaration a
  val a : Double

  // 2. Input value of a
  print("a = ")
  a = readLine().toString().toDouble()

  // 3. Outputting the absolute value using 
  //   the shorthand form of the if statement
  if (a<0)
    println("abs(a) = " + (-a))
  if (a>=0)
    println("abs(a) = " + a)
}

The result of the program

a = -8
abs(a) = 8

 

5.2. Calculating the sum of numbers

Task. Four real numbers are given. Find the sum of those numbers that are greater than 10. Do not use the loop operator.

Solution.

fun main(args:Array<String>) {
  // Statement if(...) { ... }

  // 1. Declaring variables
  val a : Double
  val b : Double
  val c : Double
  val d : Double
  var summ : Double = 0.0   // The required summ
  val Value : Double = 10.0 // number to compare

  // 2. Input numbers a, b, c, d
  print("a = "); a = readLine().toString().toDouble()
  print("b = "); b = readLine().toString().toDouble()
  print("c = "); c = readLine().toString().toDouble()
  print("d = "); d = readLine().toString().toDouble()

  // 3. Calculation using the if statement
  if (a>Value)
    summ += a
  if (b>Value)
    summ += b
  if (c>Value)
    summ += c
  if (d>Value)
    summ += d

  // 4. Print the result
  println("summ = " + summ)
}

Program execution result

a = 15
b = 5
c = 6
d = 11
summ = 26.0

 

6. Examples of using the if-else operator (full form)
6.1. Determining the sum of the digits of a number

A two-digit number is given. Determine if the sum of the digits of a number is a two-digit number. For example, for number 78, the sum of the digits is 15, which means the answer is “Yes”.

Solution.

fun main(args:Array<String>) {
  // Statement if-else

  // 1. Declaring variables
  val num : Int   // Specified number

  // 2. Input number num
  print("num = ")
  num = readLine().toString().toInt()

  // 3. Calculation
  // 3.1. Checking if a number is two-digit
  if ((num<10) || (num>99))
  {
    println("Incorrect input.")
    return
  }

  // 3.2. Calculate the sum of the digits of a number
  val sum = (num % 10 + num / 10)

  // 3.3. Print the result
  if (sum > 9)
    println("Yes")
  else
    println("No")
}

Program execution result

num = 57
Yes

 

6.2. Checking the fulfillment of inequality

Task. Three numbers a, b, c are given. Check if the inequality a<b<c holds.

Solution.

fun main(args:Array<String>) {
  // Statement if-else

  // 1. Declaring variables
  val a : Int
  val b : Int
  val c : Int

  // 2. Print the numbers a, b, c
  print("a = ")
  a = readLine().toString().toInt()

  print("b = ")
  b = readLine().toString().toInt()

  print("c = ")
  c = readLine().toString().toInt()

  // 3. Calculation and output of the result
  if ((a<b) && (a<c))
    println("a<b<c => true")
  else
    println("a<b<c => false")
}

Program execution result

a = 5
b = 9
c = 12
a<b<c =>   true

 

7. Examples of using the operator if-else-if (ladder form)
7.1. Determining a shorter distance

Task. Two distances are known, one in kilometers and the other in feet (1 foot = 0.45 m). Which of the distances is shorter and which is longer? Work through all possible options.

Solution.

fun main(args:Array<String>) {
  // Statement if-else-if

  // 1. Declaring Variables
  val lenKm : Double // distance in kilometers
  val lenFt : Double // distance in feet (1 ft = 0.45 m)

  // 2. Input distance
  print("Distance in kilometers: lenKm = "); 
  lenKm = readLine().toString().toDouble()
  print("Distance in feet: lenFt = "); 
  lenFt = readLine().toString().toDouble()

  // 3. Calculation. The if-else-if statement is used
  if (lenKm*1000 > lenFt*0.45) // Bring all distances to meters
    println("lenKm > lenFt")
  else
  if (lenKm*1000 < lenFt*0.45)
    println("lenKm < lenFt")
  else
    println("lenKm == lenFt")
}

Program execution result

Distance in kilometers: lenKm = 0.9
Distance in feet: lenFt = 2000
lenKm == lenFt

 

7.2. Determining the quarter number on the coordinate plane

Task. Display the number of the quarter of the coordinate plane to which the point with coordinates (x, y) belongs, provided that x≠0 and y≠0.

fun main(args:Array<String>) {
  // Statement if-else-if

  // 1. Declaring variables
  val x : Double
  val y : Double

  // 2. Input values x, y
  print("x = ")
  x = readLine().toString().toDouble()

  print("y = ")
  y = readLine().toString().toDouble()

  // 3. Calculation and output the result
  if ((x==0.0) || (y==0.0)) {
    println("Incorrect input")
    return
  }

  if ((x>0) && (y>0))
    println("1")
  else
  if ((x>0) && (y<0))
    println("4")
  else
  if ((x<0) && (y>0))
    println("2")
  else
    println("3")
}

Program execution result

x = 5
y = -2
4

 


Related topics