- 4.1 Introduction
- 4.2 Algorithms
- 4.3 Pseudocode
- 4.4 Control Structures
- 4.5 If...Then Selection Statement
- 4.6 If...Then...Else Selection Statement
- 4.7 Nested If...Then...Else Statements
- 4.8 Repetition Statements
- 4.9 Compound Assignment Operators
- 4.10 Formulating Algorithms: Counter-Controlled Repetition
- 4.11 Formulating Algorithms: Nested Control Statements
- 4.12 Using the Debugger: Locating a Logic Error
- 4.13 Wrap-Up
- Summary
- Terminology
- Self-Review Exercises
- Answers to Self-Review Exercises
- Quick Quiz
- Exercises
- Making a Difference Exercises
4.2 Algorithms
Any computing problem can be solved by executing a series of actions in a specific order. A procedure for solving a problem, in terms of
- the actions to be executed and
- the order in which these actions are to be executed
is called an algorithm. The following example demonstrates the importance of getting the order right.
Consider the "rise-and-shine algorithm" followed by one junior executive for getting out of bed and going to work: (1) get out of bed, (2) take off pajamas, (3) take a shower, (4) get dressed, (5) eat breakfast and (6) carpool to work. This routine prepares the executive for a productive day at the office.
However, suppose that the same steps are performed in a slightly different order: (1) get out of bed, (2) take off pajamas, (3) get dressed, (4) take a shower, (5) eat breakfast, (6) carpool to work. In this case, our junior executive shows up for work soaking wet.
Specifying the order in which statements (actions) execute in a program is called program control. This chapter investigates program control using control statements.