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.
Solution: Select an item - C Tutorial
From the course: C Essential Training
Solution: Select an item
(upbeat music) - [Instructor] Here is my solution where the tall if-else, if-else construction has been replaced by a switch case structure. The switch is variable a. Each of the case statements references values 1, 2, and 3 with the appropriate puts statements outputting the strings. The default condition replaces the old final else statement in the original construction. I believe this approach is far more efficient than performing multiple if and if-else tests in the original challenge code, especially when the comparison is simple, such as these values 1, 2, and 3. It's also more readable I think. Always be on the lookout for similar improvements in your code. I hope your solution generated positive results.
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
-
-
-