Introduction
Selection is the process of choosing between multiple pathways in a program. In this section, we have 3 examples of applying selection in different circumstances instead of the usual single example. In C there are 3 syntactic constructs that you can use to perform selection. The most important of which involves the if/else statement. We will only focus on this in this chapter. In a later chapter we will introduce the other constructs. It is not necessary to use them... they provide convenience under some circumstances. It is more important to know and understand the if/else construct (and their variations) instead and connect them to the problems we may wish to solve.
Learning Outcomes
After completing this chapter, students will be able to:
- Understand selection and control flow and explain how if/else statements allow programs to execute different code paths based on conditions
- Construct and evaluate boolean expressions using comparison operators
(==, !=, <, >, <=, >=)and logical operators(&&, ||, !) - Implement if/else and if/else if/else statements to handle mutually exclusive conditions and make decisions in programs
- Distinguish between mutually exclusive and independent conditions and select appropriate control structures for each scenario
- Apply De Morgan's Law to simplify or rewrite boolean expressions in equivalent forms
- Understand truthy and falsey values in C and predict how non-zero and zero values are evaluated in conditional statements
- Design programs with selection that solve real-world problems involving conditional logic, such as calculating taxes, determining fares, or computing prices based on multiple criteria