chapter 5 control structures ii (repetition)

29
CHAPTER 5 CONTROL STRUCTURES II (Repetition)

Upload: laith-rhodes

Post on 30-Dec-2015

70 views

Category:

Documents


6 download

DESCRIPTION

CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct and use count-controlled, sentinel-controlled, flag-controlled, and EOF-controlled repetition structures - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

CHAPTER 5

CONTROL STRUCTURES II

(Repetition)

Page 2: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

In this chapter, you will: Learn about repetition (looping) control

structures Explore how to construct and use count-

controlled, sentinel-controlled, flag-controlled, and EOF-controlled repetition structures

Examine break and continue statements Discover how to form and use nested control

structures

Page 3: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

THE while LOOPING (REPETITION) STRUCTURE

The general form of the while statement is

while(expression)

statement

Page 4: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

Introduction to Loops • Execute a block of statement(s) repetitiously

as long as the condition is true (non-zero)

a) counting loop

b) conditional loop

- sentinel loop, flag-controlled loop

• It requires 3 steps to design a loop:

1)Initial the condition before the loop

2)Design the condition (Boolean expression) for the loop

3)Design the body of the loop.

3a)The action you want to repeat

3b)Update the condition

Page 5: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

The while Loop• A loop is part of a program that repeats.

• A while loop is a “pre test” loop - the expression is tested before the loop is executed

while (expression)

statement;

while (expression)

{

statement;

statement;

statement;

}

Page 6: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

Demonstrate a counter loop

Start

initialize the Num to 1

Num <= 10

Output Num and Num*Num

Stop

Increment Num by 1

F

T

1)initial Num to 1

2)Repeat while Num is less or equal to 10

2a)output Num and Num*Num

2b)increment Num by 1

End while

Page 7: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

// This program displays the numbers 1 through 10 and // their squares. #include <iostream>using namespace std;void main(void){

int Num = 1; 1cout << "Number Number Squared\n";cout << "-------------------------\n";while (Num <= 10) 2{

cout << Num << "\t\t" << (Num * Num) << endl; 3aNum++; 3b

}}

Page 8: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

Program Output

Number Number Squared

-------------------------1 12 43 94 165 256 367 498 649 8110 100

Page 9: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

Case 2: Sentinel Controlled while Loop

cin>>variable;

while(variable != sentinel)

{

.

.

.

cin>> variable;

.

.

.

}

Page 10: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

This program demonstrates a loop using sentinel

Start

Number !=99

Prompt user to enter a number

Stop

Enter number

Initialize Number to 0

1)Initialize Number to 0

2)Prompt user to enter an number

3)Repeat while Number is not 99

enter an number

End while

T

F

Page 11: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

// This program demonstrates a simple while loop and 99 is the // sentinel#include <iostream> using namespace std;void main(void){

int Number = 0;  cout << "This program will let you enter number after\n";

cout << "number. Enter 99 when you want to quit the ";cout << "program.\n";cin >> Number; 1while (Number != 99) 2

cin >> Number; 3a & 3b}

Page 12: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

Case 3: Flag-Controlled while Loops• A flag controlled while loop uses a Boolean variable to control the

loop. Suppose found is a Boolean variable. The flag controlled while loop takes the form:

found = false;while(!found){

. .

. if(expression) found = true; .

. .

}

Page 13: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

The value returned by cin can be used to determine if the program has reached the end of input data or read invalid data.

Since cin returns a logical value true or false, in a while loop it can be considered a logical expression. An example of an EOF controlled while loop is:

cin>>variable;while(cin){

.

.

.cin>>variable;...

}

Page 14: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

The eof FunctionSuppose we have the following declaration:

ifstream infile;

Consider the expression:

infile.eof()

• This is a logical (Boolean) expression.

• The value of this expression is true if the program has read past the end of the input file, infile, otherwise the value of this expression is false.

Page 15: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

Suppose we have the declaration:

ifstream infile;

char ch;

infile.open("inputDat.dat");

• The following while loop continues to execute as long as the program has not reached the end of file.

infile.get(ch);

while(!infile.eof())

{

cout<<ch;

infile.get(ch);

}

Page 16: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

THE for LOOPING (REPETITION) STRUCTURE

The general form of the for statement is

for(initial statement; loop condition; update statement)

statement

• The initial statement, loop condition and update statement (called for loop control statements) that are enclosed with in the parentheses controls the body (the statement) of the for statement.

Page 17: CHAPTER 5 CONTROL STRUCTURES II (Repetition)
Page 18: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

The for Loop• Ideal for situations that require a

counter because it has built-in expressions that initialize and update variables.

1 2 3b

for (initialization; test; update)

statement; 3a

enter --> 1 2 3a 3b 2 3a 3b

2 ……. ....3a 3b 2 --> exit

for(init;test;update) {

statement;

statement;

statement;

}

for(init;test;update) { statement; for (initialization;test;update) {

statement;statement;

} statement;}

The Nested Loop

for (init1,init2; test; update1,update2) statement;

init;for (; test; update) statement;

Page 19: CHAPTER 5 CONTROL STRUCTURES II (Repetition)
Page 20: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

Example 5-6

The following for loop prints the first 10 positive integers:

for(i = 1; i <= 10; i++)

cout<<i<<" ";

. The following is a legal for loop:

for(;;)

cout<<"Hello"<<endl;

for(i = 10; i <= 10; i++)

cout<<i<<" ";

for(i = 1; i <= 10; i++);

cout<<i<<" ";

for(i = 1; ; i++)

cout<<i<<" ";

Page 21: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

THE do…while LOOPING (REPETITION) STRUCTURE

The general form of a do...while statement is:

do

statement

while(expression);

Page 22: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

Example: Consider the following two loops

(a) (b)

i = 11; i = 11;

while(i <= 10) do

{ {

cout<<i<<" "; cout<<i<<" ";;

i = i + 5; i = i + 5;

} }

while(i <= 10);

In (a), the while loop, produces nothing.

In (b) the do...while loop, outputs the number 11.

Page 23: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

BREAK AND CONTINUE STATEMENTS A break and continue statement alters the flow of control.

The break statement, when executed in a switch structure, provides an immediate exit from the switch structure.

You can use the break statement in while, for, and do...while loops.

When the break statement executes in a repetition structure, it immediately exits from these structures.

The break statement is typically used for two purposes:

1. To exit early from a loop

2. To skip the remainder of the switch structure

After the break statement executes, the program continues to execute with the first statement after the structure.

Page 24: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

The following while loop is written without using the variable isNegative:

sum = 0;cin>>num;

while(cin){ if(num < 0) //if number is negative, terminate the loop { cout<<"Negative number found in the data"<<endl; break;

}

sum = sum + num; cin>>num;}

Page 25: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

• The continue statement is used in while, for, and do-while structures.

• When the continue statement is executed in a loop, it skips the remaining statements in the loop and proceeds with the next iteration of the loop.

• In a while and do-while structure, the expression (that is, the loop-continue test) is evaluated immediately after the continue statement.

• In a for structure, the update statement is executed after the continue statement, and then the loop condition (that is, the loop-continue test) executes.

Page 26: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

sum = 0;

cin>>num;

while(cin)

{

if(num < 0)

{

cout<<"Negative number found in the data"<<endl; continue;

}

sum = sum + num;

cin>>num;

}

Page 27: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

• The use of a break statement in a loop can eliminate the use of certain (flag) variables.

sum = 0;cin>>num;isNegative = false;

while(cin && !isNegative){ if(num < 0) //if number is negative, terminate the loop { cout<<"Negative number found in the data"<<endl; isNegative = true;

} else { sum = sum + num; cin>>num;

}}

Page 28: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

• Suppose we want to create the following pattern.

***************

• In the first line we want to print one star, in the second line two stars and so on.

• Since five lines are to be printed, we start with the following for statement.

for(i = 1; i <= 5 ; i++)

• The value of i in the first iteration is 1, in the second iteration it is 2, and so on.

• We can use the value of i as the limiting condition in another for loop nested within this loop to control the number of starts in a line.

Page 29: CHAPTER 5 CONTROL STRUCTURES II (Repetition)

for(i = 1; i <= 5 ; i++)

{

for(j = 1; j <= i; j++)

cout<<"*";

cout<<endl;

}

• What pattern does the code produce if we replace the first for statement with the following?

for(i = 5; i >= 1; i--)