Programming in Kotlin: Fundamentals

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

Part 1: Use Data Types & Operations

7. Branch with If Expressions & Scopes

Episode complete

Play next episode

Next
About this episode
See versions
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 6. Combine Logical Operators Next episode: 8. 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 Pro subscription. 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.

This video Branch with If Expressions & Scopes was last updated on Aug 9 2022

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

At this point, you should feel pretty good about working with logical operators. You’ve 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’ll 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!!!"
}