In this video, learn how to create more succinct conditional expressions and handle optional values using ternary conditional and nil coalescing operators.
- [Narrator] The ternary conditional operator…has three parts, a true, false condition,…and if expression to execute if the condition is true,…and an expression to execute if it's false.…These parts are separated by a question mark and a colon.…If name is empty returns true,…hello will be assigned to salutation.…If it's not empty, the interpolated…hi name string will be returned.…Ternary operations can be used for…conditional value assignment as I just did.…Or the true false expressions can call functions…that don't necessarily return a value,…such as the print functions here.…
Ternary operators can also be nested and optionally…wrapped to multiple lines to improve readability.…I'll check if birth year's less than 1945.…If that's true, I'll return greatest generation.…If false, I'll nest another ternary operator…to check if birth year is less than 1965.…If so, I'll return baby boomers and if…not, I'll nest another ternary and so on.…This is pretty verbose though, a switch statement…would be a much better choice here.…
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?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
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.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.