CppHTP5e_05.pdf
Document Details
Uploaded by Deleted User
Tags
Full Transcript
1 5 Control Statements: Part 2 2006 Pearson Education, Inc. All rights reserved. 2 OBJECTIVES In this chapter you will learn:...
1 5 Control Statements: Part 2 2006 Pearson Education, Inc. All rights reserved. 2 OBJECTIVES In this chapter you will learn: The essentials of counter-controlled repetition. To use the for and do…while repetition statements to execute statements in a program repeatedly. To understand multiple selection using the switch selection statement. To use the break and continue program control statements to alter the flow of control. To use the logical operators to form complex conditional expressions in control statements. To avoid the consequences of confusing the equality and assignment operators. 2006 Pearson Education, Inc. All rights reserved. 3 5.1 Introduction 5.2 Essentials of Counter-Controlled Repetition 5.3 for Repetition Statement 5.4 Examples Using the for Statement 5.5 do…while Repetition Statement 5.6 switch Multiple-Selection Statement 5.7 break and continue Statements 5.8 Logical Operators 5.9 Confusing Equality (==) and Assignment (=) Operators 5.10 Structured Programming Summary 5.11 Problem Solving 5.12 Wrap-Up 2006 Pearson Education, Inc. All rights reserved. 4 5.1 Introduction Continue structured programming discussion – Introduce C++’s remaining control structures for, do…while, switch 2006 Pearson Education, Inc. All rights reserved. 5 5.2 Essentials of Counter-Controlled Repetition Counter-controlled repetition requires: – Name of a control variable (loop counter) – Initial value of the control variable – Loop-continuation condition that tests for the final value of the control variable – Increment/decrement of control variable at each iteration 2006 Pearson Education, Inc. All rights reserved. 1 // Fig. 5.1: fig05_01.cpp 6 2 // Counter-controlled repetition. Outline 3 #include 4 using std::cout; 5 using std::endl; Control-variable name is counter 6 fig05_01.cpp with variable initial value 1 7 int main() 8 { (1 of 1) 9 int counter = 1; // declare and initialize control variable 10 Condition tests for 11 while ( counter