An if
expression is a conditional that runs a block of code when its condition has a true
value.
var morning = trueif (morning) {println("Rise and shine!")}// Prints: Rise and shine!
An else
expression is a conditional that runs a block of code only when the conditions contained in the previous expressions have false
values.
var rained = falseif (rained) {println("No need to water the plants today.")} else {println("Plants need to be watered!")}// Prints: Plants need to be watered!
An else
-if
expression allows for more conditions to be 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 else
expression.
var age = 65if (age < 18 ) {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 are symbols that are used to compare two values in order to return a result of true
or false
. Comparison operators include >
, <
, >=
, <=
.
var myAge = 19var sisterAge = 11var cousinAge = 11myAge > sisterAge // truemyAge < cousinAge // falsemyAge >= cousinAge // truemyAge <= sisterAge // false
Logical operators are symbols used to evaluate the relationship between two or more Boolean expressions in order to return a true
or false
value.
Logical operators include !
, &&
, and ||
.
var humid = truevar raining = truevar jacket = falseprintln(!humid)// Prints: falseprintln(jacket && raining)// Prints: trueprintln(humid || raining)// Prints: true
The logical AND operator (&&
) is used to compare the relationship between two Boolean expressions and will only return a true
value if both expressions are true
.
var humid = truevar raining = truevar shorts = falsevar sunny = false// true AND trueprintln(humid && raining) // true// true AND falseprintln(humid && shorts) // false// false AND trueprintln(sunny && raining) // false// false AND falseprintln(shorts && sunny) // false
The logical OR operator (||
) is used to compare the relationship between two Boolean expressions and will return true
when at least one of the expressions are true
.
var late = truevar skipBreakfast = truevar underslept = falsevar checkEmails = false// true OR trueprintln(skipBreakfast || late) // true// true OR falseprintln(late || checkEmails) // true// false OR trueprintln(underslept || late) // true// false OR falseprintln(checkEmails || underslept) // false
The logical NOT operator (!
) evaluates the value of a Boolean expression and then returns its negated value.
var hungry = truevar full = falseprintln(!hungry) // falseprintln(!full) // true
The order of evaluation when using multiple logical operators in a single Boolean expression is:
!
) operator.&&
) operator.||
) operator.!true && (false || true) // false/*(false || true) is evaluated first returning true. Then,!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.*/
A nested conditional is a conditional that exists within another conditional.
var studied = truevar wellRested = trueif (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!
A when
expression controls the flow of code by evaluating the value of a variable in order to determine what code gets executed.
var grade = "A"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 (..
) is used to create a succession of number or character values.
var height = 46 // inchesif (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 are symbols that are used to compare the equivalence of two values in order to return true
or false
. Equality operators include ==
and !=
.
var myAge = 22var sisterAge = 21myAge == sisterAge // falsemyAge !== sisterAge // true