Programming in Kotlin: Fundamentals

Aug 9 2022 · Kotlin 1.6, Android 12, IntelliJ IDEA CE 2022.1.3

Part 1: Use Data Types & Operations

07. Branch with If Expressions & Scopes

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 06. Combine Logical Operators Next episode: 08. Challenge: Practice If Expressions & Boolean Logic

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You鈥檙e accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You鈥檙e accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

At this point, you should feel pretty good about working with logical operators. You鈥檝e seen how combining them can produce a true or false value. But what if you wanted to set a value based on the result of our logical operators? Often times, you鈥檒l want to set a variable based on a condition.

/*
// Starter code
val chrisGrade = 49
val meritAwardGrade = 90
*/
var message
var message: String
val chrisHasPerfectAttendance = true
val chrisIsMeritStudent = chrisHasPerfectAttendance && chrisGrade > meritAwardGrade
if (chrisIsMeritStudent) {
    message = "Congratulations"
}
println(message)
else {
    message = "Keep studying"
}
val samGrade = 99
val betterStudent 
val betterStudent = if (samGrade > chrisGrade) "Sam" else "Chris"
println(betterStudent)
val betterStudent = if (samGrade > chrisGrade) {
    "Sam"
} else if (samGrade < chrisGrade) {
    "Chris"
} else {
    "They have equal grades!!!"
}