72
1 Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming Chapter 5: Control Structures II (Repetition)

Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

  • Upload
    others

  • View
    17

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

1

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Chapter 5:

Control Structures II (Repetition)

Page 2: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

2

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Objectives

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

Learn how to avoid bugs by avoiding

patches

Learn how to debug loops

Page 3: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

3

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Why Is Repetition Needed?

• Repetition allows you to efficiently use variables

• Can input, add, and average multiple numbers

using a limited number of variables

• For example, to add five numbers:

– Declare a variable for each number, input the

numbers and add the variables together

– Create a loop that reads a number into a

variable and adds it to a variable that contains

the sum of the numbers

Page 4: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

4

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Read and Add Numbers

� 2 Numbers

int Sum;

int N1;

cin >> N1;

int N2;

cin >> N2;

Sum = N1 + N2;

cout << Sum;

INPUT N1

INPUT N2

COMPUTE

Sum=N1+N2;

OUPUT Sum

START

STOP

Page 5: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

5

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Read and Add Numbers

�3 Numbersint Sum;

int N1;

cin >> N1;

int N2;

cin >> N2;

int N3;

cin >> N3;

Sum = N1 + N2 + N3;

cout << Sum;

INPUT N1

INPUT N2

COMPUTE

Sum=N1+N2+N3;

OUPUT Sum

START

STOP

INPUT N3

Page 6: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

6

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

N2

Read and Add Numbers

� 3 Numbers

int Sum;

int N1;

cin >> N1;

int N2;

cin >> N2;

int N3;

cin >> N3;

Sum = N1 + N2 + N3;

cout << Sum;

N1

N3

� 3 Numbers Sum so far

int Sum=0;

int N1;

cin >> N1;

Sum = Sum + N1;

int N2;

cin >> N2;

Sum = Sum + N2;

int N3;

cin >> N3;

Sum = Sum + N3;

cout << Sum;

Page 7: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

7

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

REPEAT

3

TIMES

N2

Read and Add Numbers

N1

N3

int Sum=0;

int N1;

cin >> N1;

Sum = Sum + N1;

int N2;

cin >> N2;

Sum = Sum + N2;

int N3;

cin >> N3;

Sum = Sum + N3;

cout << Sum;

int Sum=0;

int N;

cin >> N;

Sum = Sum + N;

cout << Sum;

Page 8: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

8

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

REPEAT

1000

TIMES

Read and Add Numbers

int Sum=0;

int N;

cin >> N;

Sum = Sum + N;

cout << Sum;

REPEAT

3

TIMES

int Sum=0;

int N;

cin >> N;

Sum = Sum + N;

cout << Sum;

Page 9: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

9

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

while Looping (Repetition) Structure

• The general form of the while statement is:

while is a reserved word

• Statement can be simple or compound

• Expression acts as a decision maker and is usually

a logical expression

• Statement is called the body of the loop

• The parentheses are part of the syntax

while ( Expression )

Statement;

Page 10: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

10

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

while Looping (Repetition) Structure

• Infinite loop: continues to execute endlessly

– Avoided by including statements in loop

body that assure exit condition is eventually false

false trueExpression

Statement

while ( Expression )

Statement;

Page 11: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

11

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

while Looping (Repetition) Structure

i=0;

while (i<=20)

{

cout << i << “ ”;

i=i+5;

}

cout << “\n”;

Sample run:

0 5 10 15 20

i=25;

while (i<=20)

{

cout << i << “ ”;

i=i+5;

}

cout << “\n”;

Sample run:

5 times 0 times

Page 12: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

12

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

while Looping (Repetition) Structure

i=0;

while (i<=20)

{

cout << i << “ ”;

i=i+5;

}

cout << “\n”;

Sample run:

0 5 10 15 20

i=0;

while (i>=-20)

{

cout << i << “ ”;

i=i+5;

}

cout << “\n”;

Sample run:

_

5 times INFINITE

Page 13: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

13

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

while Looping (Repetition) Structure

i=0;

while (i<=20)

{

cout << i << “ ”;

i=i+5;

}

cout << “\n”;

Sample run:

0 5 10 15 20

i=0;

while (i<=20);

{

cout << i << “ ”;

i=i+5;

}

cout << “\n”;

Sample run:

_

5 times

INFINITE

times

Page 14: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

14

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

while Looping (Repetition) Structure

i=0;

while (i<=20)

{

cout << i << “ ”;

i=i+5;

}

cout << “\n”;

Sample run:

0 5 10 15 20

i=0;

while (i<20)

{

cout << i << “ ”;

i=i+5;

}

cout << “\n”;

Sample run:

5 timesRandom number

or INFINITE

Page 15: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

15

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

while Looping (Repetition) Structure

i=0;

while (i<=20)

{

cout << i << “ ”;

i=i+5;

}

cout << “\n”;

Sample run:

0 5 10 15 20

i=0;

while (i<20)

{

cout << i << “ ”;

i=i+5;

}

cout << “\n”;

Sample run:

0 0 0 0 0 0 0 0 0 ….

5 times INFINITE

Page 16: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

16

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Case 1: Counter-Controlled while Loops

• If you know exactly how many pieces of data need

to be read

//initialize the counter

counter = 0;

//test the counter

while (counter < N)

{

//loop statements

Statements;

//update the counter

counter++;

}

Page 17: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

17

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Counter-Controlled while Loops

counter = 0;

while ( counter < N)

{

Statement;

counter++;

}

false truecounter < N

Statement

counter = 0

counter=counter +1

Page 18: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

18

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

REPEAT

5

TIMES

Read and Add Numbersint Sum=0;

int counter=0;

while (counter<5)

{

int N;

cin >> N;

Sum = Sum + N;

counter++;

}

cout << Sum;

REPEAT

5

TIMES

int Sum=0;

int N;

cin >> N;

Sum = Sum + N;

cout << Sum;

Page 19: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

19

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Example: Sum of N Numbers

Page 20: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

20

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Case 2: Sentinel-Controlled while Loops

• Sentinel variable is tested in the condition

• Loop ends when sentinel is encountered

//initialize the loop control variable

cin >> variable;

//test the loop control variable

while (variable != SENTINEL)

{

//loop statements

Statements;

//update the loop control variable

cin >> variable;

}

Page 21: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

21

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Sentinel-Controlled while Loops

false truevariable!=

SENTINEL

variable!=

SENTINEL

Statement

cin >> variable;

while ( variable!=

SENTINEL)

{

Statement;

cin >> variable;

}

INPUT variable

INPUT variable

Page 22: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

22

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Example: Sentinel-Controlled Sum

Page 23: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

23

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Example: Letter to Telephone Digits

Page 24: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

24

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Case 3: Flag-Controlled while Loops

� A flag-controlled while loop uses a bool

variable to control the loop

//initialize the loop control variable

found = false;

//test the loop control variable

while (!found)

{

//loop statements

Statements;

//update the loop control variable

if (expression)

found = true;

}

Page 25: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

25

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Flag-Controlled while Loops

found=false;

while (!found)

{

Statement;

if (expression)

found=true;

}true

false true!found

Statement

found=false

found=true

expressionfalse

Page 26: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

26

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

rand Function

� Generate a random int value between 0 and

32767

� In header file cstdlib

� To convert it to an integer greater than or equal to

0 and less than 100:

rand() % 100

Page 27: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

27

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Example: Number Guessing Game

Page 28: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

28

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Case 4: EOF-Controlled while Loops� Use an EOF (End Of File)-controlled while loop

to read from an input stream (file) as long as

there is something in that stream

//initialize the loop control variable

InputStream >> variable;

//test the loop control variable

while (InputStream)

{

//loop statements

Statements;

//update the loop control variable

InputStream >> variable;

}

Page 29: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

29

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

EOF(End of File)-Controlled while Loops

cin >> variable;

while (InputStream)

{

Statement;

cin >> variable;

}

false trueInputStream

Statement

INPUT variable

INPUT variable

Page 30: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

30

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

eof Function

� The function eof can determine the end of file

status

� Is true when you read beyond the end of the file

� eof is a member of data type istream

� The syntax for the function eof is:

where istreamVar is an input stream variable,

such as cin or a input file variable

istreamVar.eof()

Page 31: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

31

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Example: Grades

First Name Last Name TestScore Letter Grade

��������

��������

�������

�������

��������

�������

AVERAGE

LetterGrade=

A if 90<TestScore

B if 80<TestScore<90

C if 70<TestScore<80

D if 60<TestScore<70

F if TestScore<60

ClassAverage=������������������⋯�� �����������������

=∑ ����������������"�"#�

For each

student

For the

class

Page 32: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

32

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Example:

Grades

� Flowchart

START

INPUT FirstName, LastName, AverageTestScore

YESNOInputFile

STOP

NumberStudents=0

INPUT FirstName, LastName, AverageTestScore

Calculate LetterGrade

Sum = Sum + TestScore

NumberStudents = NumberStudents+1

ClassAverage = Sum/NumberStudents

OUTPUT Name + LetterGrade

Sum=0

OUTPUT ClassAverage

Page 33: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

33

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Example: Grades

Page 34: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

34

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

More on Expressions in while Statements

� The expression in a while statement can be

complexnoOfGuesses=0;

while ((noOfGuesses < 5) && (!isGuessed))

{

cout << "Enter an integer greater than or equal

to 0 and less than 100: ";

cin >> guess;

cout << endl;

noOfGuesses++;

}

Page 35: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

35

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Programming Example: Fibonacci Number

� Consider the following sequence of numbers:

� 1, 1, 2, 3, 5, 8, 13, 21, 34, ....

� Given the first two numbers of the sequence

(say, a1 and a2)

� nth number an, n >= 3, of this sequence is

given by: an = an-1 + an-2

Page 36: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

36

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Programming Example: Fibonacci Number

� Fibonacci sequence

� nth Fibonacci number

� a2 = 1

� a1 = 1

� Determine the nth number, an, n >= 3

a1 a2 a3 a4 a5

+

Page 37: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

37

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Programming Example: Fibonacci Number

� Input:

� first two Fibonacci numbers

� the desired Fibonacci number n

� Output:

� nth Fibonacci number

Page 38: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

38

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Programming Example: Fibonacci Number

� Problem Analysis and Algorithm Design:

1. Get the first two Fibonacci numbers

2. Get the desired Fibonacci number

� Get the position, n, of the Fibonacci number in the sequence

3. Calculate the next Fibonacci number

� By adding the previous two elements of the Fibonacci

sequence

4. Repeat Step 3 until the nth Fibonacci number is

found

5. Output the nth Fibonacci number

Page 39: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

39

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

� Variables

Programming Example: Fibonacci Number

Page 40: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

40

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Programming Example: Fibonacci Number

� Main Algorithm:

1. Prompt the user for the first two numbers—that is,

previous1 and previous2

2. Read (input) the first two numbers into previous1

and previous2

3. Output the first two Fibonacci numbers

4. Prompt the user for the position of the desired

Fibonacci number

5. Read the position of the desired Fibonacci number

into nthFibonacci

Page 41: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

41

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Programming Example: Fibonacci Number

a. if (nthFibonacci == 1)

The desired Fibonacci number is the first Fibonacci number.

Copy the value of previous1 into current

b. else if (nthFibonacci == 2)

The desired Fibonacci number is the second Fibonacci number.

Copy the value of previous2 into current.

c. else calculate the desired Fibonacci number as

follows:

• Initialize counter to 3 to keep track of the calculated

Fibonacci numbers.

6.

Page 42: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

42

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Programming Example: Fibonacci Number � Calculate the next Fibonacci number, as follows:

current = previous2 + previous1;

� Assign the value of previous2 to previous1

� Assign the value of current to previous2

� Increment counter

� Repeat until Fibonacci number is calculated

7. Output the nth Fibonacci number, which is current

previous2 previous1 current

+

Page 43: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

43

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Programming Example: Fibonacci Number

Page 44: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

44

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Programming Example: Fibonacci Number

Page 45: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

45

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

for Looping (Repetition) Structure• The general form of the for statement is:

• The InitialStatement, LoopCondition, and

UpdateStatement are called for loop control

statements

– InitialStatement usually initializes a variable

(called the for loop control, or for indexed,

variable)

• In C++, for is a reserved word

for( InitialStatement; LoopCondition; UpdateStatement )

Statement;

Page 46: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

46

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

for Looping (Repetition) Structure

false true

LoopCondition

Statement

InitialStatement

UpdateStatement

for( InitialStatement; LoopCondition; UpdateStatement )

Statement;

Page 47: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

47

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

for Looping (Repetition) Structure

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

cout << i << “ ”;

cout << endl;

Sample output:

0 1 2 3 4 5 6 7 8 9

Page 48: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

48

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

for Looping (Repetition) Structure

for loop while loop

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

cout << i << “ ”;

cout << endl;

i=0;

while (i<10)

{

cout << i << “ ”;

i++;

}

cout << endl;

Page 49: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

49

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

for Looping (Repetition) Structure

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

cout << “Hello”;

cout << “*”;

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

{

cout << “Hello”;

cout << “*”;

}

simple

statement

compound

statement

Page 50: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

50

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

for Looping (Repetition) Structure

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

{

cout << “Hello”;

cout << “ * ”;

}

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

{

cout << “Hello”;

cout << “ * ”;

}

10 times 1 time

Page 51: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

51

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

for Looping (Repetition) Structure

•The following is a legal for loop:

•The following is a legal while loop:

for (;;)

cout << "Hello\n”;

while (1)

cout << "Hello\n”;

•Both loops are legal, but not semantically correct.

•They are infinite loops (never end).

Page 52: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

52

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

for Looping (Repetition) Structure • C++ allows you to use fractional values for loop control

variables of the double type

– Results may differ. Avoid using them.

• An semicolon before the body of the loop will terminate

it. for (i=0;i<5;i++);

cout << “*" << endl;

• If the initial statement, loop condition, or

update statement are omitted (empty) they are

assumed to be true and the loop is an infinite loop.for (;;)

cout << “*" << endl;

Page 53: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

53

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

for Looping (Repetition) Structure

� Not necessarily in increasing order – you can

decrement the loop control variable

for (i=10; i>0; i--)

cout << i << “ ”;

cout << endl;

Sample output:

10 9 8 7 6 5 4 3 2 1

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

cout << i << “ ”;

cout << endl;

Page 54: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

54

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

for Looping (Repetition) Structure � Not necessarily consecutive values for the loop control

variable

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

cout << i << “ ”;

cout << endl;

Sample output:

1 3 5 7 9

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

cout << i << “ ”;

cout << endl;

Sample output:

2 4 6 8 10

Page 55: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

55

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Example: Fibonacci Number

Page 56: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

56

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

do…while Looping (Repetition) Structure

• General form of a do...while:

• The statement executes first, and then the

expression is evaluated

• To avoid an infinite loop, body must contain a

statement that makes the expression false

• The statement can be simple or compound

• Loop always iterates at least once

do

Statement;

while ( Expression );

Page 57: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

57

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

do…while Looping (Repetition) Structure

false true

Expression

Statement do

Statement;

while ( Expression );

Page 58: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

58

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

while Looping (Repetition) Structure i=0;

do

{

cout << i << “ ”;

i=i+5;

}

while (i<=20);

cout << “\n”;

Sample run:

0 5 10 15 20

i=25;

do

{

cout << i << “ ”;

i=i+5;

}

while (i<=20);

cout << “\n”;

Sample run:

0

5 times 1 times

Page 59: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

59

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

while Looping (Repetition) Structure i=25;

do

{

cout << i << “ ”;

i=i+5;

}

while (i<=20);

cout << “\n”;

Sample run:

0

i=25;

while (i<=20)

{

cout << i << “ ”;

i=i+5;

}

cout << “\n”;

Sample run:

1 times 0 times

Page 60: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

60

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Choosing the Right Looping Structure

� All three loops have their place in C++

– If you know or can determine in advance the number of repetitions needed, the for loop is the correct choice

– If you do not know and cannot determine in advance the number of repetitions needed, and it could be zero, use a while loop

– If you do not know and cannot determine in advance the number of repetitions needed, and it is at least one, use a do...while loop

Page 61: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

61

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

break and continue Statements

• break and continue alter the flow of control

• break statement is used for two purposes:

– To exit early from a loop

• Can eliminate the use of certain (flag) variables

– To skip the remainder of the switch structure

• After the break statement executes, the

program continues with the first statement after

the structure

Page 62: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

62

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

break and continue Statements

� continue is used in while, for, and

do…while structures

� When executed in a loop

� It skips remaining statements and proceeds

with the next iteration of the loop

Page 63: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

63

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

break and continue Statements

break continue

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

{

if (i==5)

break;

cout << “ “ << i;

}

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

{

if (i==5)

continue;

cout << “ “ << i;

}

0 1 2 3 4 0 1 2 3 4 6 7 8 9

Page 64: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

64

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Nested Control Structures

� Print N stars for (j = 1; j <= N; j++)

cout << "*";

� Print the numbers from 1 to 5, one per linefor (i = 1; i <= 5 ; i++)

{

cout << i;

cout << endl;

}

Page 65: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

65

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Nested Control Structures

� Create and print the following pattern: *

**

***

****

*****

� Code:for (i = 1; i <= 5 ; i++)

{

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

cout << "*";

cout << endl;

}

On line i – print i stars

Page 66: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

66

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Nested Control Structures

� Create and print the following pattern: *****

****

***

**

*

� Code:for (i = 5; i >= 1; i--)

{

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

cout << "*";

cout << endl;

}

On line 5-i-1 – print i stars / decreasing order

Page 67: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

67

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Nested Control Structures

� Create and print the following pattern: *

**

***

****

*****

� Code:for (i = 1; i <= 5 ; i++)

{

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

cout << “ ";

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

cout << "*";

cout << endl;

}

On line i – print 5-i spaces and i stars

Page 68: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

68

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Nested Control Structures

� Pattern: *

**

***

****

*****

****

***

**

*

� Code:

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

{

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

cout << "*";

cout << endl;

}

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

{

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

cout << "*";

cout << endl;

}

Page 69: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

69

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Avoiding Bugs by Avoiding Patches

� Software patch

� Piece of code written on top of an existing

piece of code

� Intended to fix a bug in the original code

� Some programmers address the symptom of

the problem by adding a software patch

� Should instead resolve underlying issue

Page 70: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

70

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Debugging Loops

� Loops are harder to debug than sequence and

selection structures

� Use loop invariant

� Set of statements that remains true each

time the loop body is executed

� Most common error associated with loops is

off-by-one

Page 71: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

71

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Summary

C++ has three looping (repetition) structures:

• while, for, and do…while

while and for loops are called pretest loops

do...while loop is called a posttest loop

while and for may not execute at all, but do...while always

executes at least once

Page 72: Chapter 5: Control Structures II (Repetition) · C++ Programming Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

72

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming

Programming Fundamentals I

C++ Programming

Summary

while: expression is the decision maker, and the statement is the body of the

loop

A while loop can be:

•Counter-controlled

•Sentinel-controlled

•Flag-controlled

•EOF-controlled

for loop: simplifies the writing of a

counter-controlled while loop

Executing a breakstatement in the body of a loop immediately

terminates the loop

Executing a continue statement

in the body of a loop skips to the

next iteration