Making Decisions
Branch with if, the ternary operator and switch.
4.1Nested if in C, Explained with Real-Life ExamplesWhat a nested if statement is in C, when the inner check runs, and two worked examples (a store discount and an ATM withdrawal) with flowcharts, code and common pitfalls.4.2Shortcut Ways to Write if in C: the Ternary Operator and MoreShorten an if to one line by dropping the braces, write an if-else with the ternary operator ?:, and use other shortcuts: testing a value directly and using a comparison as a number. With short examples and their real output.4.3The switch Statement in C: Syntax, break, Fall-Through and ExamplesHow C's switch statement works: matching cases, break, default, deliberate fall-through, grouped cases, chars and enums, and when to choose switch over if / else if.
Back to top