Overview
Thus far, your students have only written programs that execute statements in sequential
order, from beginning to end. Beginning in this chapter, students will learn how to write
code that executes based on certain conditions. Two major categories of control
structures will be discussed in this text: selection structures are introduced in this
chapter, and repetition structures in the next. In the process of learning about control
structures, students will also become familiar with relational operators and Boolean
expressions. Finally, they will learn how to handle input errors with the assert
function.
Objectives
In this chapter, the student will:
• Learn about control structures
• Examine relational operators
• Discover how to use the selection control structures if, if…else
• Examine int and bool data types and logical (Boolean) expressions
• Examine logical operators
• Explore how to form and evaluate logical (Boolean) expressions
• Learn how relational operators work with the string type
• Become aware of short-circuit evaluation
• Learn how the conditional operator, ?:, works
• Learn how to use pseudocode to develop, test, and debug a program
• Discover how to use a switch statement in a program
• Learn how to avoid bugs by avoiding partially understood concepts
• Learn how to use the assert function to terminate a program
Teaching Tips
Control Structures
Selection: if and if…else
1. Introduce the concept of logical expression.
2. Review the relational operators using Table 4-1.