190 likes | 226 Views
Learn about pretest loops, counters, accumulators, and for statements in C++ programming, along with flowcharts and pseudocode examples. Understand loop conditions and bodies for efficient programming practices.
E N D
Chapter 7:The Repetition Structure Introduction to Programming with C++ Fourth Edition
Objectives • Include a repetition structure in pseudocode and in a flowchart • Code a pretest loop using the C++ while statement • Initialize and update counters and accumulators • Code a pretest loop using the C++ for statement Introduction to Programming with C++, Fourth Edition
Using the Repetition Structure • Repetition structure or loop - allows repeated processing of one or more program instructions until some condition is met • Repetition structure can be either a pretest loop or a posttest loop • Pretest loop: evaluation occurs before loop instructions are processed • Posttest loop: evaluation occurs after loop instructions are processed Introduction to Programming with C++, Fourth Edition
Pretest Loops Introduction to Programming with C++, Fourth Edition
Pretest Loops (continued) Introduction to Programming with C++, Fourth Edition
Pretest Loops (continued) • Every loop has: • a loop condition • a loop body • Loop body: • instructions within a loop • Loop condition: • appears at the beginning of a pretest loop • determines the number of times instructions within the loop are processed Introduction to Programming with C++, Fourth Edition
Pretest Loops (continued) • Sentinel value – special value to end the loop • Priming read: • Appears above the loop • Used to prepare or set up the loop Introduction to Programming with C++, Fourth Edition
Components of a Loop Introduction to Programming with C++, Fourth Edition
O’Donnell Incorporated Algorithm Shown in Flowchart Form Introduction to Programming with C++, Fourth Edition
Using the while Statement to Code the Pretest Loop • While statement: programmer must supply the loop condition to be evaluated • Loop condition: • must be a Boolean expression (true or false) • can contain variables, constants, functions, methods, arithmetic operators, comparison operators, and logical operators • Endless or infinite loop – one that processes its instructions indefinitely Introduction to Programming with C++, Fourth Edition
Syntax of the C++ while Statement Introduction to Programming with C++, Fourth Edition
Using Counters and Accumulators • Counter - a numeric variable used for counting • Accumulator - a numeric variable used for accumulating a value • Initializing - assigning a beginning value to the counter or accumulator • Updating or incrementing - adding a number to the value stored in the counter or the accumulator Introduction to Programming with C++, Fourth Edition
Counter-Controlled Pretest Loops Introduction to Programming with C++, Fourth Edition
Using the for Statement to Code a Pretest Loop • Most common use for the for statement: • Code pretest loops whose processing is controlled by a counter • For statement: • Begins with the for clause • Followed by the body of the loop • If the loop body contains more than one statement: • Statements must be entered as a statement block i.e., enclosed in braces ({}) Introduction to Programming with C++, Fourth Edition
Syntax of the C++ for statement Introduction to Programming with C++, Fourth Edition
forStatement Displays the Numbers 1 Through 3 Introduction to Programming with C++, Fourth Edition
for Statement Calculates and Displays a Commission Introduction to Programming with C++, Fourth Edition
for Statement Calculates and Displays a Bonus Introduction to Programming with C++, Fourth Edition
Summary • Repetition structure or loop - allows repeated processing of one or more program instructions until some condition is met • Repetition structures contain a condition and a loop body Introduction to Programming with C++, Fourth Edition