Discover how to write conditional expressions that verify expectations are met up front or else exit the enclosing scope. Conditionally unwrap optional values for use in the enclosing scope.
- [Instructor] The guard statement is similar to…an if statement except the guard statement…only executes the body of code in its else clause…if the conditional expression it checks resolves to false.…For example, I have a riders value…that is an array of tuples containing two labeled elements,…name of type string and height inches of type int…and I have populated riders with a few values.…I will iterate through this array…and guard that each rider is at least 44 inches tall,…which is the minimum height…for a typical roller coaster ride.…Unlike an if statement, a guard statement always…includes a else clause that is executed…if the if condition resolves to false.…
The continue control transfer statement…is also required in a guard statement,…right before the closing curly brace.…If the guard condition fails,…it will transfer control out of the enclosing scope.…In this example, continue will cause the program flow…to skip to the beginning of the next iteration…or the for end loop.…If the guard statement is successful,…
Learn how to write code, understand Swift's key concepts and best practices, and strengthen your programming problem-solving skills. Instructor Scott Gardner teaches the fundamentals, so you'll be prepared to develop applications for iOS, macOS, and other platforms. Completing this course will enable you to not only write first-class code, but to think like a Swift developer.
- Creating playgrounds
- Defining variables and constants
- Working with characters and strings
- Working with collections and groups
- Using operators and defining custom operators
- Controlling program flow
- Defining functions and closures
- Working with classes, structures, and enumerations
- Adopting protocols
Skill Level Beginner
1. Get Started
2. Variables and Constants
3. Characters and Strings
4. Collections and Tuples
6. Control Flow
- 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.