Learn Kotlin - Conditional Expressions Cheatsheet - Codecademy
Learn Kotlin - Conditional Expressions Cheatsheet - Codecademy
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 code only when the conditions contained in var rained = false
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!
Else-If Expressions
An else - if expression allows for more
var age = 65
conditions to be evaluated within an if / else
expression.
if (age < 18 ) {
You can use multiple else - if expressions as long
println("You are considered
as they appear after the if expression and before
a minor.")
the else expression.
} else if (age < 60) {
println("You are considered an
adult.")
} else {
println("You are considered
a senior.")
}
/
Comparison Operators
Comparison operators are symbols that are used to
compare two values in order to return a result of var myAge = 19
true or false . Comparison operators include var sisterAge = 11
> , < , >= , <= . var cousinAge = 11
Logical Operators
Logical operators are symbols used to evaluate the
relationship between two or more Boolean expressions var humid = true
in order to return a true or false value. var raining = true
Logical operators include ! , && , and || . var jacket = false
println(!humid)
// Prints: false
println(humid || raining)
// Prints: true
/
The OR Operator : ||
The logical OR operator ( || ) is used to compare the
relationship between two Boolean expressions and will var late = true
return true when at least one of the expressions var skipBreakfast = true
var underslept = false
are true .
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
println(!hungry) // false
println(!full) // true
/
Order of Evaluation
The order of evaluation when using multiple logical
operators in a single Boolean expression is: !true && (false || true) // false
/*
1. Expressions placed in parentheses.
(false || true) is evaluated first
2. NOT( ! ) operator. returning true. Then,
!true && true is evaluated returning
3. AND( && ) operator.
the final result, false.
4. OR( || ) operator. */
Nested Conditionals
A nested conditional is a conditional that exists within
another conditional. var studied = true
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!")
}
}
/
When Expressions
A when expression controls the ow of code by
evaluating the value of a variable in order to determine var grade = "A"
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!
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 the equivalence of two values in order to var myAge = 22
return true or false . Equality operators var sisterAge = 21
include == and != .
myAge == sisterAge // false
myAge !== sisterAge // true