0% found this document useful (0 votes)
3 views

Learn Kotlin_ Conditional Expressions Cheatsheet

This document is a cheatsheet for Kotlin conditional expressions, covering if, else, else-if, comparison operators, logical operators, and nested conditionals. It explains how to use these expressions with examples, including the use of the when expression and range operator. Additionally, it details equality operators for comparing values.

Uploaded by

hasan zahid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Learn Kotlin_ Conditional Expressions Cheatsheet

This document is a cheatsheet for Kotlin conditional expressions, covering if, else, else-if, comparison operators, logical operators, and nested conditionals. It explains how to use these expressions with examples, including the use of the when expression and range operator. Additionally, it details equality operators for comparing values.

Uploaded by

hasan zahid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

12/12/23, 12:36 PM Learn Kotlin: Conditional Expressions Cheatsheet | Codecademy

Cheatsheets / Learn Kotlin

Conditional Expressions

If Expressions

An if expression is a conditional that runs a block of var morning = true


code when its condition has a true value.

if (morning) {
println("Rise and shine!")
}
// Prints: Rise and shine!

Else Expressions

An else expression is a conditional that runs a block of var rained = false


code only when the conditions contained in the previous
expressions have false values.
if (rained) {
println("No need to water the plants
today.")
} else {
println("Plants need to be watered!")
}
// Prints: Plants need to be watered!

https://www.codecademy.com/learn/learn-kotlin/modules/learn-kotlin-conditional-expressions/cheatsheet 1/7
12/12/23, 12:36 PM Learn Kotlin: Conditional Expressions Cheatsheet | Codecademy

Else-If Expressions

An else - if expression allows for more conditions to be var age = 65


evaluated within an if / else expression.
You can use multiple else - if expressions as long as
they appear after the if expression and before the if (age < 18 ) {
else expression. println("You are considered a minor.")
} else if (age < 60) {
println("You are considered an adult.")
} else {
println("You are considered a senior.")
}

// Prints: You are considered a senior.

Comparison Operators

Comparison operators are symbols that are used to var myAge = 19


compare two values in order to return a result of true or
var sisterAge = 11
false . Comparison operators include > , < , >= , <= .
var cousinAge = 11

myAge > sisterAge // true


myAge < cousinAge // false
myAge >= cousinAge // true
myAge <= sisterAge // false

https://www.codecademy.com/learn/learn-kotlin/modules/learn-kotlin-conditional-expressions/cheatsheet 2/7
12/12/23, 12:36 PM Learn Kotlin: Conditional Expressions Cheatsheet | Codecademy

Logical Operators

Logical operators are symbols used to evaluate the var humid = true
relationship between two or more Boolean expressions in
var raining = true
order to return a true or false value.
Logical operators include ! , && , and || . var jacket = false

println(!humid)
// Prints: false

println(jacket && raining)


// Prints: true

println(humid || raining)
// Prints: true

The AND Operator: &&

The logical AND operator ( && ) is used to compare the var humid = true
relationship between two Boolean expressions and will
var raining = true
only return a true value if both expressions are true .
var shorts = false
var sunny = false

// true AND true


println(humid && raining) // true

// true AND false


println(humid && shorts) // false

// false AND true


println(sunny && raining) // false

// false AND false


println(shorts && sunny) // false

https://www.codecademy.com/learn/learn-kotlin/modules/learn-kotlin-conditional-expressions/cheatsheet 3/7
12/12/23, 12:36 PM Learn Kotlin: Conditional Expressions Cheatsheet | Codecademy

The OR Operator : ||

The logical OR operator ( || ) is used to compare the var late = true


relationship between two Boolean expressions and will
var skipBreakfast = true
return true when at least one of the expressions are
true . var underslept = false
var checkEmails = false

// true OR true
println(skipBreakfast || late) // true

// true OR false
println(late || checkEmails) // true

// false OR true
println(underslept || late) // true

// false OR false
println(checkEmails || underslept) //
false

The NOT Operator: !

The logical NOT operator ( ! ) evaluates the value of a var hungry = true
Boolean expression and then returns its negated value.
var full = false

println(!hungry) // false
println(!full) // true

https://www.codecademy.com/learn/learn-kotlin/modules/learn-kotlin-conditional-expressions/cheatsheet 4/7
12/12/23, 12:36 PM Learn Kotlin: Conditional Expressions Cheatsheet | Codecademy

Order of Evaluation

The order of evaluation when using multiple logical !true && (false || true) // false
operators in a single Boolean expression is:
/*
1. Expressions placed in parentheses.
2. NOT(!) operator. (false || true) is evaluated first
3. AND(&&) operator. returning true. Then,
4. OR(||) operator.
!true && true is evaluated returning the
final result, false.
*/

!false && true || false // true


/*
!false is evaluated first returning true.
Then true && true are evaluated, returning
true. Then, true || false is evaluated
which ends up returning true.
*/

https://www.codecademy.com/learn/learn-kotlin/modules/learn-kotlin-conditional-expressions/cheatsheet 5/7
12/12/23, 12:36 PM Learn Kotlin: Conditional Expressions Cheatsheet | Codecademy

Nested Conditionals

A nested conditional is a conditional that exists within var studied = true


another conditional.
var wellRested = true

if (wellRested) {
println("Best of luck today!")
if (studied) {
println("You should be prepared for
your exam!")
} else {
println("Take a few hours to study
before your exam!")
}
}

// Prints: Best of luck today!


// Prints: You should be prepared for your
exam!

When Expressions

A when expression controls the flow of code by var grade = "A"


evaluating the value of a variable in order to determine
what code gets executed.
when(grade) {
"A" -> println("Excellent job!")
"B" -> println("Very well done!")
"C" -> println("You passed!")
else -> println("Close! Make sure to
perpare more next time!")
}
// Prints: Excellent job!

https://www.codecademy.com/learn/learn-kotlin/modules/learn-kotlin-conditional-expressions/cheatsheet 6/7
12/12/23, 12:36 PM Learn Kotlin: Conditional Expressions Cheatsheet | Codecademy

The Range Operator

The range operator ( .. ) is used to create a succession of var height = 46 // inches


number or character values.

if (height in 1..53) {
println("Sorry, you must be at least 54
inches to ride the rollercoaster.")
}
// Prints: Sorry, you must be at least 54
inches to ride the rollercoaster.

Equality Operators

Equality operators are symbols that are used to compare var myAge = 22
the equivalence of two values in order to return true or
var sisterAge = 21
false . Equality operators include == and != .

myAge == sisterAge // false


myAge !== sisterAge // true

Print Share

https://www.codecademy.com/learn/learn-kotlin/modules/learn-kotlin-conditional-expressions/cheatsheet 7/7

You might also like