Learn Kotlin_ Conditional Expressions Cheatsheet
Learn Kotlin_ Conditional Expressions Cheatsheet
Conditional Expressions
If Expressions
if (morning) {
println("Rise and shine!")
}
// Prints: Rise and shine!
Else Expressions
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
Comparison Operators
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(humid || raining)
// Prints: true
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
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 : ||
// 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 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.
*/
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
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!")
}
}
When Expressions
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
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 != .
Print Share
https://www.codecademy.com/learn/learn-kotlin/modules/learn-kotlin-conditional-expressions/cheatsheet 7/7