0% found this document useful (0 votes)
29 views5 pages

Learn Kotlin - Conditional Expressions Cheatsheet - Codecademy

Uploaded by

Diprot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views5 pages

Learn Kotlin - Conditional Expressions Cheatsheet - Codecademy

Uploaded by

Diprot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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 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.")
}

// Prints: 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

myAge > sisterAge  // true


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

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(jacket && raining)


// Prints: true

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

The AND Operator: &&


The logical AND operator ( && ) is used to compare the
relationship between two Boolean expressions and will var humid = true
only return a true value if both expressions are
var raining = true
var shorts = false
true .
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

/
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

The NOT Operator: !


The logical NOT operator ( ! ) evaluates the value of a
Boolean expression and then returns its negated value. var hungry = true
var full = 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. */

!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.
*/

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!")
  }
}

// Prints: Best of luck today!


// Prints: You should be prepared for
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!

The Range Operator


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

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

You might also like