From the course: C Essential Training
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Using the ternary operator - C Tutorial
From the course: C Essential Training
Using the ternary operator
- [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.…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
Making a decision3m 37s
-
(Locked)
Exploring the possibilities2m 34s
-
(Locked)
Using the ternary operator3m 1s
-
Working with the switch-case structure4m 9s
-
(Locked)
Challenge: Select an item29s
-
(Locked)
Solution: Select an item57s
-
(Locked)
Creating a for loop3m 58s
-
(Locked)
Setting up a while loop3m 6s
-
(Locked)
Challenge: Repeat some text48s
-
(Locked)
Solution: Repeat some text1m 40s
-
(Locked)
Nesting loops1m 55s
-
(Locked)
Breaking out of a loop3m 28s
-
(Locked)
Avoiding the goto keyword1m 56s
-
(Locked)
Challenge: Work the grid2m 21s
-
(Locked)
Solution: Work the grid2m 8s
-
-
-