In this chapter we introduce all but one of C#’s other control statements—the foreach statement is introduced in Chapter 8. We demonstrate C#’s for, do…while and switch statements. Through examples using while and for, we enumerate the essentials of counter-controlled iteration. You’ll use type decimal to represent monetary amounts precisely, rather than incuring the representational errors that can occur with types float or double.
We use a switch multiple-selection statement to count the number of A, B, C, D and F grade equivalents in a set of numeric grades entered by the user. We introduce the break and continue program-control statements. We discuss C#’s logical operators, which enable you to combine simple conditions in control statements.
Essentials of Counter-Controlled Iteration
This section uses the while iteration statement introduced in Chapter 5 to formalize the elements of counter-controlled iteration:
1. a control variable (or loop counter),
2. the control variable’s initial value,
3. the control variable’s increment that’s applied during each iteration of the loop,
4. the loop-continuation condition that determines if looping should continue.