Docsity

Count-Controlled Loops and While Statements in C++, Slides of Advanced Computer Programming

Partial preview of the text

Download Count-Controlled Loops and While Statements in C++ and more Slides Advanced Computer Programming in PDF only on Docsity!

Chapter 6

Looping

Chapter 6 Topics

• While Statement Syntax

• Count-Controlled Loops

• Event-Controlled Loops

• Using the End-of-File Condition to Control

Input Data

• Using a While Statement for Summing and

Counting

• Nested While Loops

• Loop Testing and Debugging

Two Types of Loops

Count controlled loops

Repeat a statement or block a specified number

of times

Event-controlled loops

Repeat a statement or block until a condition

within the loop body changes that causes the

repetition to stop

While Statement

SYNTAX

while ( Expression)

. // loop body

Loop body can be a single statement, a null

statement, or a block

Count-controlled loops contain

– An initialization of the loop control variable

– An expression to test if the proper number of

repetitions has been completed

– An update of the loop control variable to be

executed with each iteration of the body

Count-Controlled Loops

Count-Controlled Loop Example int count; // Loop-control variable count = 4; // Initialize loop variable while(count > 0) // Test expression { cout << count << endl; // Repeated action count --; // Update loop variable } cout << “Done” << endl;

Count-controlled Loop

int count; count = 4; while(count > 0) { cout << count << endl; count --; } cout << “Done” << endl;

OUTPUT

count

Count-controlled Loop

int count; count = 4; while(count > 0) TRUE { cout << count << endl; count --; } cout << “Done” << endl;

OUTPUT

count

Count-controlled Loop

int count; count = 4; while(count > 0) { cout << count << endl; count --; } cout << “Done” << endl;

OUTPUT

count

Count-controlled Loop

int count; count = 4; while(count > 0) TRUE { cout << count << endl; count --; } cout << “Done” << endl;

OUTPUT

count

Count-controlled Loop

int count; count = 4; while(count > 0) { cout << count << endl; count --; } cout << “Done” << endl;

OUTPUT

count

Count-controlled Loop

int count; count = 4; while(count > 0) TRUE { cout << count << endl; count --; } cout << “Done” << endl;

OUTPUT

count

Count-controlled Loop

int count; count = 4; while(count > 0) { cout << count << endl; count --; } cout << “Done” << endl;

OUTPUT

count

Count-controlled Loop

int count; count = 4; while(count > 0) TRUE { cout << count << endl; count --; } cout << “Done” << endl;

OUTPUT

count