Join Harrison Ferrone for an in-depth discussion in this video The while loop, part of Learning Swift 3 for Developers.
- [Harrison] Let's take a look…at what a while loop looks like in Swift.…I'm going to make some new room…underneath our for loop section,…add in a quick comment, "The While Loop."…Let's have two variables called alive,…which is going to be a boolean and equal true,…and playerLives, which is going to be a integer…and equal three.…We want our while loop to keep executing…as long as alive equals true…so let's do that.…
Use the while keyword and add the condition.…While alive equals true,…we're going to take the playerLives,…I misspelled this, playerLives,…and we're going to use the minus equals operator,…one so we're going to decrement it by one each turn,…and here we're going to add a conditional…to check if the player is still alive…so if playerLives is greater than or equal to zero,…alive will equal false,…and we're going to add a print statement…just to keep track of how many times this is executing.…
If you look in your debug log,…we have, "It's alive," printed three times.…With the while loop,…the order of execution is really important.…
- Starting a new playground
- Printing to the console
- Declaring variables
- Working with numbers
- Using strings
- Breaking down optionals
- Understanding closures
- Classes vs. structs
- Extensions and protocols
Skill Level Beginner
1. Hello Swift
2. Back to Basics: Variables and Operators
3. Collections, Tuples, and Enumerations
4. Working with Logic Flows
5. Only in Swift: Optionals
6. Functions, Closures, and Associated Types
7. Classes vs. Structs
8. Advanced Topics
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.