The ternary operator involves a comparison and two values or expressions, the first returned when the comparison is true and the second otherwise. Using the ternary operator instead of an if statement allows you to concoct more compact decision-making expressions, though the ternary operator returns single values.
- [Instructor] In this exercise file, … you see a standard if-else construction. … If a is greater than b, assign its value to variable c, … otherwise, assign the value of variable b to c, … output the results, build and run. … And there you see the output. … Here is the same code, … but with the if-else construction replaced … by a ternary operator statement at line nine. … Like the if keyword, … the ternary operator involves a comparison. … It comes first. … Is a greater than b? … This expression, the comparison, … need not be enclosed in parentheses. … I did it here to make this new format look … a little bit more readable. … The expression is followed by a question mark, … which is the first piece of the ternary operator. … If this comparison is true, … then the result is the value a, … whatever comes after the question mark. … This value is assigned to variable c. … If this expression is false, … then what follows the colon is assigned to a variable c. … In this case the value of variable b. …
- Writing your first C program
- Working the C development cycle
- Adding comments to code for clarity
- C language data types
- Declaring variables
- Making new data types
- Typecasting variables
- Using math, assignment, and logical operators
- Creating for and while loops
- Nesting loops
- Creating functions
- Returning values from functions
- Passing arguments to functions
- Creating recursive functions
Skill Level Intermediate
C: Data Structures, Pointers, and File Systemswith Dan Gookin2h 57m Intermediate
1. The Fundamentals of C
2. Variables and Values
3. Operators and Math
4. Decisions and Loops
Next steps1m 38s
- 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.