Difference between revisions of "KOTLIN: If else expression"

From OnnoWiki
Jump to navigation Jump to search
(Created page with "Sumber: https://www.tutorialspoint.com/kotlin/kotlin_if_else_expression.htm Kotlin if...else expressions works like an if...else expression in any other Modern Computer Progr...")
 
 
(4 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
Kotlin if...else expressions works like an if...else expression in any other Modern Computer Programming. So let's start with our traditional if...else statement available in Kotlin.
 
Kotlin if...else expressions works like an if...else expression in any other Modern Computer Programming. So let's start with our traditional if...else statement available in Kotlin.
  
Syntax
+
Ekspresi Kotlin if...else bekerja seperti ekspresi if...else dalam Pemrograman Komputer Modern lainnya. Jadi mari kita mulai dengan pernyataan if...else tradisional kita yang tersedia di Kotlin.
 +
 
 +
 
 +
==Syntax==
 +
 
 
The syntax of a traditional if...else expression is as follows:
 
The syntax of a traditional if...else expression is as follows:
  
if (condition) {
+
Sintaks ekspresi if...else tradisional adalah sebagai berikut:
  // code block A to be executed if condition is true
+
 
} else {
+
if (condition) {
  // code block B to be executed if condition is false
+
    // code block A to be executed if condition is true
}
+
} else {
 +
  // code block B to be executed if condition is false
 +
}
 +
 
 
Here if statement is executed and the given condition is checked. If this condition is evaluated to true then code block A is executed, otherwise program goes into else part and code block B is executed.
 
Here if statement is executed and the given condition is checked. If this condition is evaluated to true then code block A is executed, otherwise program goes into else part and code block B is executed.
  
Example
+
Di sini jika pernyataan dijalankan dan kondisi yang diberikan diperiksa. Jika kondisi ini bernilai true maka blok kode A dijalankan, jika tidak program masuk ke bagian lain dan blok kode B dijalankan.
 +
 
 +
 
 +
==Example==
 +
 
 
You can try the following example:
 
You can try the following example:
  
fun main(args: Array<String>) {
+
Anda dapat mencoba contoh berikut:
    val age:Int = 10
+
 
 +
 
 +
fun main(args: Array<String>) {
 +
    val age:Int = 10
 +
 +
    if (age > 18) {
 +
        print("Adult")
 +
    } else {
 +
        print("Minor")
 +
    }
 +
}
  
    if (age > 18) {
 
        print("Adult")
 
    } else {
 
        print("Minor")
 
    }
 
}
 
 
When you run the above Kotlin program, it will generate the following output:
 
When you run the above Kotlin program, it will generate the following output:
  
Minor
+
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
Kotlin if...else Expression
+
 
 +
 
 +
Minor
 +
 
 +
==Kotlin if...else Expression==
 +
 
 
Kotlin if...else can also be used as an expression which returns a value and this value can be assigned to a variable. Below is a simple syntax of Kotlin if...else expression:
 
Kotlin if...else can also be used as an expression which returns a value and this value can be assigned to a variable. Below is a simple syntax of Kotlin if...else expression:
  
Syntax
+
Kotlin if...else juga dapat digunakan sebagai ekspresi yang mengembalikan nilai dan nilai ini dapat ditetapkan ke variabel. Di bawah ini adalah sintaks sederhana ekspresi Kotlin if...else:
val result = if (condition) {
+
 
  // code block A to be executed if condition is true
+
===Syntax===
} else {
+
 
  // code block B to be executed if condition is false
+
val result = if (condition) {
}
+
    // code block A to be executed if condition is true
 +
} else {
 +
  // code block B to be executed if condition is false
 +
}
 +
 
 
If you're using if as an expression, for example, for returning its value or assigning it to a variable, the else branch is mandatory.
 
If you're using if as an expression, for example, for returning its value or assigning it to a variable, the else branch is mandatory.
  
Examples
+
Jika Anda menggunakan if sebagai ekspresi, misalnya, untuk mengembalikan nilainya atau menetapkannya ke variabel, cabang else adalah wajib.
fun main(args: Array<String>) {
+
 
    val age:Int = 10
+
===Examples===
 +
 
 +
fun main(args: Array<String>) {
 +
    val age:Int = 10  
 +
 +
    val result = if (age > 18) {
 +
        "Adult"
 +
    } else {
 +
        "Minor"
 +
    }
 +
    println(result)
 +
}
  
    val result = if (age > 18) {
 
        "Adult"
 
    } else {
 
        "Minor"
 
    }
 
    println(result)
 
}
 
 
When you run the above Kotlin program, it will generate the following output:
 
When you run the above Kotlin program, it will generate the following output:
  
Minor
+
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
 +
 
 +
Minor
 +
 
 
You can ommit the curly braces {} when if has only one statement:
 
You can ommit the curly braces {} when if has only one statement:
  
fun main(args: Array<String>) {
+
Anda bisa menghilangkan kurung kurawal {} bila if hanya memiliki satu pernyataan:
    val age:Int = 10
+
 
 +
 
 +
fun main(args: Array<String>) {
 +
    val age:Int = 10
 +
 +
    val result = if (age > 18) "Adult" else  "Minor"
 +
    println(result)
 +
}
  
    val result = if (age > 18) "Adult" else  "Minor"
 
    println(result)
 
}
 
 
When you run the above Kotlin program, it will generate the following output:
 
When you run the above Kotlin program, it will generate the following output:
  
Minor
+
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
 +
 
 +
Minor
 +
 
 
You can include multiple statements in if...else block, in this case the last expression is returned as the value of the block. Try the following example:
 
You can include multiple statements in if...else block, in this case the last expression is returned as the value of the block. Try the following example:
  
fun main(args: Array<String>) {
+
Anda dapat memasukkan beberapa pernyataan di blok if...else, dalam hal ini ekspresi terakhir dikembalikan sebagai nilai blok. Coba contoh berikut:
    val age:Int = 10
+
 
 +
fun main(args: Array<String>) {
 +
    val age:Int = 10
 +
 +
    val result = if (age > 18) {
 +
        println("Given condition is true")
 +
        "Adult"
 +
    } else {
 +
        println("Given condition is false")
 +
        "Minor"
 +
    }
 +
    print("The value of result : ")
 +
    println(result)
 +
}
  
    val result = if (age > 18) {
 
        println("Given condition is true")
 
        "Adult"
 
    } else {
 
        println("Given condition is false")
 
        "Minor"
 
    }
 
    print("The value of result : ")
 
    println(result)
 
}
 
 
When you run the above Kotlin program, it will generate the following output:
 
When you run the above Kotlin program, it will generate the following output:
  
Given condition is false
+
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
The value of result : Minor
+
 
Kotlin if...else...if Ladder
+
 
 +
Given condition is false
 +
The value of result : Minor
 +
 
 +
==Kotlin if...else...if Ladder==
 +
 
 
You can use else if condition to specify a new condition if the first condition is false.
 
You can use else if condition to specify a new condition if the first condition is false.
  
Syntax
+
Anda dapat menggunakan else if condition untuk menentukan kondisi baru jika kondisi pertama salah.
if (condition1) {
+
 
  // code block A to be executed if condition1 is true
+
Syntax
} else if (condition2) {
+
if (condition1) {
  // code block B to be executed if condition2 is true
+
  // code block A to be executed if condition1 is true
} else {
+
} else if (condition2) {
  // code block C to be executed if condition1 and condition2 are false
+
  // code block B to be executed if condition2 is true
}
+
} else {
Example
+
  // code block C to be executed if condition1 and condition2 are false
fun main(args: Array<String>) {
+
}
    val age:Int = 13
+
 
 +
===Example===
 +
 
 +
fun main(args: Array<String>) {
 +
    val age:Int = 13
 +
 +
    val result = if (age > 19) {
 +
        "Adult"
 +
    } else if ( age > 12 && age  < 20 ){
 +
        "Teen"
 +
    } else {
 +
        "Minor"
 +
    }
 +
    print("The value of result : ")
 +
    println(result)
 +
}
  
    val result = if (age > 19) {
 
        "Adult"
 
    } else if ( age > 12 && age  < 20 ){
 
        "Teen"
 
    } else {
 
        "Minor"
 
    }
 
    print("The value of result : ")
 
    println(result)
 
}
 
 
When you run the above Kotlin program, it will generate the following output:
 
When you run the above Kotlin program, it will generate the following output:
  
The value of result : Teen
+
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
Kotlin Nested if Expression
+
 
 +
The value of result : Teen
 +
 
 +
==Kotlin Nested if Expression==
 +
 
 
Kotlin allows to put an if expression inside another if expression. This is called nested if expression
 
Kotlin allows to put an if expression inside another if expression. This is called nested if expression
  
Syntax
+
Kotlin memungkinkan untuk menempatkan ekspresi if di dalam ekspresi if yang lain. Ini disebut ekspresi if bersarang
if(condition1) {
+
 
  // code block A to be executed if condition1 is true
+
 
  if( (condition2) {
+
===Syntax===
      // code block B to be executed if condition2 is true
 
  }else{
 
      // code block C to be executed if condition2 is fals
 
  }
 
} else {
 
  // code block D to be executed if condition1 is false
 
}
 
Example
 
fun main(args: Array<String>) {
 
    val age:Int = 20
 
  
    val result = if (age > 12) {
+
if(condition1) {
      if ( age > 12 && age  < 20 ){
+
    // code block A to be executed if condition1 is true
          "Teen"
+
    if( (condition2) {
      }else{
+
       // code block B to be executed if condition2 is true
          "Adult"
+
     }else{
       }
+
      // code block C to be executed if condition2 is fals
     } else {
 
        "Minor"
 
 
     }
 
     }
    print("The value of result : ")
+
} else {
    println(result)
+
  // code block D to be executed if condition1 is false
}
+
}
 +
 
 +
===Example===
 +
 
 +
fun main(args: Array<String>) {
 +
    val age:Int = 20 
 +
 +
    val result = if (age > 12) {
 +
        if ( age > 12 && age  < 20 ){
 +
            "Teen"
 +
        }else{
 +
            "Adult"
 +
        }
 +
    } else {
 +
        "Minor"
 +
    }
 +
    print("The value of result : ")
 +
    println(result)
 +
}
 +
 
 
When you run the above Kotlin program, it will generate the following output:
 
When you run the above Kotlin program, it will generate the following output:
  
The value of result : Adult
+
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
Quiz Time (Interview & Exams Preparation)
+
 
 +
The value of result : Adult
 +
 
 +
==Quiz Time (Interview & Exams Preparation)==
 +
 
 
Q 1 - Which of the following is true about Kotlin if expression?
 
Q 1 - Which of the following is true about Kotlin if expression?
  

Latest revision as of 10:01, 24 July 2022

Sumber: https://www.tutorialspoint.com/kotlin/kotlin_if_else_expression.htm

Kotlin if...else expressions works like an if...else expression in any other Modern Computer Programming. So let's start with our traditional if...else statement available in Kotlin.

Ekspresi Kotlin if...else bekerja seperti ekspresi if...else dalam Pemrograman Komputer Modern lainnya. Jadi mari kita mulai dengan pernyataan if...else tradisional kita yang tersedia di Kotlin.


Syntax

The syntax of a traditional if...else expression is as follows:

Sintaks ekspresi if...else tradisional adalah sebagai berikut:

if (condition) {
   // code block A to be executed if condition is true
} else {
  // code block B to be executed if condition is false
}

Here if statement is executed and the given condition is checked. If this condition is evaluated to true then code block A is executed, otherwise program goes into else part and code block B is executed.

Di sini jika pernyataan dijalankan dan kondisi yang diberikan diperiksa. Jika kondisi ini bernilai true maka blok kode A dijalankan, jika tidak program masuk ke bagian lain dan blok kode B dijalankan.


Example

You can try the following example:

Anda dapat mencoba contoh berikut:


fun main(args: Array<String>) {
    val age:Int = 10  

    if (age > 18) {
        print("Adult")
    } else {
        print("Minor")
    }
}

When you run the above Kotlin program, it will generate the following output:

Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:


Minor

Kotlin if...else Expression

Kotlin if...else can also be used as an expression which returns a value and this value can be assigned to a variable. Below is a simple syntax of Kotlin if...else expression:

Kotlin if...else juga dapat digunakan sebagai ekspresi yang mengembalikan nilai dan nilai ini dapat ditetapkan ke variabel. Di bawah ini adalah sintaks sederhana ekspresi Kotlin if...else:

Syntax

val result = if (condition) {
   // code block A to be executed if condition is true
} else {
  // code block B to be executed if condition is false
}

If you're using if as an expression, for example, for returning its value or assigning it to a variable, the else branch is mandatory.

Jika Anda menggunakan if sebagai ekspresi, misalnya, untuk mengembalikan nilainya atau menetapkannya ke variabel, cabang else adalah wajib.

Examples

fun main(args: Array<String>) {
    val age:Int = 10   

    val result = if (age > 18) {
        "Adult"
    } else {
        "Minor"
    }
    println(result)
}

When you run the above Kotlin program, it will generate the following output:

Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:

Minor

You can ommit the curly braces {} when if has only one statement:

Anda bisa menghilangkan kurung kurawal {} bila if hanya memiliki satu pernyataan:


fun main(args: Array<String>) {
    val age:Int = 10

    val result = if (age > 18) "Adult" else  "Minor"
    println(result)
}

When you run the above Kotlin program, it will generate the following output:

Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:

Minor

You can include multiple statements in if...else block, in this case the last expression is returned as the value of the block. Try the following example:

Anda dapat memasukkan beberapa pernyataan di blok if...else, dalam hal ini ekspresi terakhir dikembalikan sebagai nilai blok. Coba contoh berikut:

fun main(args: Array<String>) {
    val age:Int = 10  

    val result = if (age > 18) {
        println("Given condition is true")
        "Adult"
    } else {
        println("Given condition is false")
        "Minor"
    }
    print("The value of result : ")
    println(result)
}

When you run the above Kotlin program, it will generate the following output:

Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:


Given condition is false
The value of result : Minor

Kotlin if...else...if Ladder

You can use else if condition to specify a new condition if the first condition is false.

Anda dapat menggunakan else if condition untuk menentukan kondisi baru jika kondisi pertama salah.

Syntax
if (condition1) {
  // code block A to be executed if condition1 is true
} else if (condition2) {
  // code block B to be executed if condition2 is true
} else {
  // code block C to be executed if condition1 and condition2 are false
}

Example

fun main(args: Array<String>) {
    val age:Int = 13

    val result = if (age > 19) {
        "Adult"
    } else if ( age > 12 && age  < 20 ){
        "Teen"
    } else {
        "Minor"
    }
    print("The value of result : ")
    println(result)
}

When you run the above Kotlin program, it will generate the following output:

Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:

The value of result : Teen

Kotlin Nested if Expression

Kotlin allows to put an if expression inside another if expression. This is called nested if expression

Kotlin memungkinkan untuk menempatkan ekspresi if di dalam ekspresi if yang lain. Ini disebut ekspresi if bersarang


Syntax

if(condition1) {
   // code block A to be executed if condition1 is true
   if( (condition2) {
      // code block B to be executed if condition2 is true
   }else{
      // code block C to be executed if condition2 is fals
   }
} else {
  // code block D to be executed if condition1 is false
}

Example

fun main(args: Array<String>) {
    val age:Int = 20   

    val result = if (age > 12) {
       if ( age > 12 && age  < 20 ){
           "Teen"
       }else{
           "Adult"
       }
    } else {
        "Minor"
    }
    print("The value of result : ")
    println(result)
}

When you run the above Kotlin program, it will generate the following output:

Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:

The value of result : Adult

Quiz Time (Interview & Exams Preparation)

Q 1 - Which of the following is true about Kotlin if expression?

A - Kotlin support traditional if...else expression.

B - Kotlin if...else expression can be nested.

C - Kotlin if...else expression returns a value which can be assigned to a variable.

D - All of the above

Q 2 - Which of the following is not supported by Kotlin?

A - if...else if...else

B - if...then...else

C - if...else...

D - None of the above

Q 3 - What will be the output of the following code?

fun main(args: Array<String>) {

   var x = 20
   var y = 15
   var z = "Mango"
   val result = if (x > y ) {
      z = "Orange"
   } else {
      z = "Apple"
   }
   println("Value of result = $z")

} A - Mango

B - Orange

C - Apple

D - None of the above


Referensi