Programming in Swift: Fundamentals

Oct 19 2021 Swift 5.5, iOS 15, Xcode 13

Part 1: Core Concepts

4. Challenge: Booleans

Episode complete

Play next episode

About this episode
See versions

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 3. Booleans & Comparison Operators Next episode: 5. Logical Operators

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

It’s time for your first challenge! You can find the challenge in the “Challenge - Booleans” page of the playground you’ve been using, or you can download a new one from the resources for this video. If you open it up…

Try solving the challenge questions on your own, then keep watching to compare your work to mine. Good luck!

Create a constant called myAge and set it to your age. Then, create a constant named isVotingAge that uses Boolean logic to determine if the value stored in myAge denotes someone of voting age. In my part of the world, the voting age is 18, so I’ll use that here.

Ok, first I’ll set my age, which is currently 42.

let myAge = 42

And then I need to check if my age greater than or equal to 18, the voting age.

let isVotingAge = myAge >= 18

Challenge 2! Create a constant called student and set it to your name as a string. Create a constant called author and set it to “Matt Galloway”, the original author of these exercises. Create a constant called authorIsStudent that uses string equality to determine if student and author are equal.

OK, first I’ll set up the student and author constants:

let student = "Chris Belanger"
let author = "Matt Galloway"

And then to find of if the author and student have the same name…

let authorIsStudent = 

I’ll use the double equals sign to check for equality.

let authorIsStudent = student == author

And lo, I am not Matt Galloway.

Challenge 3: Create a constant named studentBeforeAuthor which uses string comparison to determine if the string value in the constant student comes, alphabetically speaking, before the string value in the constant author.

This is a short one! To compare the strings, I need the “less than” or “greater than” operator…

let studentBeforeAuthor = student < author

I used “less than” because that will tell us if student comes before. With strings, “less than” really means “comes earlier in the dictionary”.

That’s it! Now, you could also have written this using the greater than operator:

let studentBeforeAuthor = author > student

However, when it comes to less than and greater than signs, it’s best to use the arrangement that makes it most clear what you’re trying to do; in this case, the constant is named “studentBeforeAuthor”, not “authorAfterStudent”, so it makes more sense here to use the previous form which reads like “if student is less than author.” That’s it for this challenge! Next up, some more work with operators and Booleans.