Chapter 3. Programming in the Small

Control

Table of Contents

1. Blocks, Loops, and Branches
2. Algorithm Development
3. The while statement
4. The if statement
5. Writing Code that Works

THE BASIC BUILDING BLOCKS of programs -- variables, expressions, assignment statements, and method call statements -- were covered in the previous chapter. Starting with this chapter, we look at how these building blocks can be put together to build complex programs with more interesting behavior.

Two types of control structures, loop and branches, can be used to repeat a sequence of statements over and over or to choose among two or more possible courses of action. C# includes several control structures of each type, and we will look at each of them in some detail.

This chapter will also begin the study of program design. Given a problem, how can you come up with a program to solve that problem? We'll look at a partial answer to this question in Section 2. In the following sections, we'll apply the techniques from Section 2 to a variety of examples.