Programming in Swift: Fundamentals

Oct 19 2021 · Swift 5.5, iOS 15, Xcode 13

Part 1: Core Concepts

05. Logical Operators

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: 04. Challenge: Booleans Next episode: 06. Challenge: Logical Operators

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.

Notes: 05. Logical Operators

Update Notes: The student materials have been reviewed and are updated as of October 2021.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re 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

In the previous set of exercises, you saw how you can work with Boolean values, and how you could use comparison operators such as less than, greater than, equal to and not equal to to compare values to each other.

let passingGrade = 50
let studentGrade = 50
let chrisGrade = 49
let samGrade = 99
let studentPassed = studentGrade >= passingGrade
let chrisPassed = chrisGrade >= passingGrade
let samPassed = samGrade >= passingGrade
!samPassed
!chrisPassed
chrisPassed == false
let catName = "Ozma"
!catName
//!catName
// AND Operator
// &&
let bothPassed = chrisPassed && samPassed
// OR Operator
// ||
let eitherPassed = chrisPassed || samPassed
let anyonePassed = chrisPassed || samPassed || studentPassed
let everyonePassed = chrisPassed && samPassed && studentPassed
let meritAwardGrade = 90
let samHasPerfectAttendance = true
let samIsMeritStudent = samHasPerfectAttendance && samGrade > meritAwardGrade
let chrisHasPerfectAttendance = true
let chrisIsMeritStudent = chrisHasPerfectAttendance && chrisGrade > meritAwardGrade
if chrisIsMeritStudent {
    print("Congratulations!")
}
else {
    print("Keep studying.")
}
var betterStudent: String
if samGrade > chrisGrade {
    betterStudent = "Sam"
} else {
    betterStudent = "Chris"
}
// Ternary conditional operator
betterStudent = samGrade > chrisGrade ? "Sam" : "Chris"