• No results found

7-RepetitionStatements.pdf

N/A
N/A
Protected

Academic year: 2020

Share "7-RepetitionStatements.pdf"

Copied!
41
0
0

Loading.... (view fulltext now)

Full text

(1)

STRUCTURED

PROGRAMMING

(2)

Content

2

 Types of repetition statements

 while repetition statement

 Counter-controlled repetition

 Sentinel-controlled repetition

 Nested control statements

 for repetition statement

 do..while repetition statement

 break and continue statements

(3)

Objectives

3

By the end you should:

 Recognize the purpose of the loops

 Identify the problems that require the use of loop structures

 Differentiate between while, for, and do..while statement  Differentiate between Counter and sentinel controlled loops

 Understand and use break and continue statements  Explain what is meant by Nested control statements  Apply C++ syntax rules associated with each repetition

statement

 Create, trace, and debug programs that use while, for,

(4)

Repetition Statements

 Three types

for statement and while statement

 Perform a group of actions zero or more times

do..while statement

 Perform a group of actions at least once

(5)

while

Repetition Statements

 Actions repeated while condition remains true

 Syntax

 One of the actions should causes condition to becomes false

 Example

5

while (condition) {

action1; action2; .

.

actionN; }

int product = 3;

while ( product <= 30 )

product *= 3; int product = 3;

while ( product <= 30 )

(6)

while

Repetition Statements

(cont

.(

 Not providing action that causes condition to becomes false

 Lead to infinite loop

 Logic error

 Example 6

int product = 3;

while ( product <= 30 )

cout << product; int product = 3;

while ( product <= 30 )

(7)

Counter-controlled Repetition

 Uses a variable to control number of loops’ iterations  Control counting loops with integer values

 Also known as definite repetition

 Number of repetitions known beforehand

 Requires

 Name of loop control variable

 Initial value of loop control variable

 Condition to test of reaching final value

 Update loop control variable 7

Initialize control var ;

while (condition) {

action1; action2; .

.

actionN;

(8)

8

1 // Fig. 5.1: fig05_01.cpp 

2 // Counter­controlled repetition. 

3 #include <iostream> 

4 using std::cout; 

5 using std::endl; 

6

7 int main() 

8

9    int counter = 1; // declare and initialize control variable 

10

11    while ( counter <= 10 ) // loop­continuation condition 

12    {     

13       cout << counter << " "; 

14       counter++; // increment control variable by 1 

15    } // end while  

16

17    cout << endl; // output a newline 

18    return 0; // successful termination 

19 } // end main 

1 2 3 4 5 6 7 8 9 10 

Control-variable name is counter

with variable initial value 1

Condition tests for

counter’s final value

(9)

Example

A class of ten students took a quiz. The grades (integers in the

range 0 to 100) are entered by the user. Determine the class

average on the quiz.

(10)

10 1      // Fig. 2.7: fig02_07.cpp

2      // Class average program with counter-controlled repetition.

3      #include <iostream>

4      

5      using std::cout;

6      using std::cin;

7      using std::endl;

8      

9      // function main begins program execution 10    int main()

11    {

12     int total; // sum of grades input by user

13     int gradeCounter; // number of grade to be entered next

14     int grade; // grade value

15     int average; // average of grades

16    

17     // initialization phase

18     total = 0; // initialize total

19     gradeCounter = 1; // initialize loop counter

20    

(11)

21     // processing phase

22     while ( gradeCounter <= 10 ) { // loop 10 times

23     cout << "Enter grade: "; // prompt for input

24     cin >> grade; // read grade from user

25     total = total + grade; // add grade to total

26     gradeCounter = gradeCounter + 1; // increment counter

27     }

28    

29     // termination phase

30     average = total / 10; // integer division

31    

32     // display result

33     cout << "Class average is " << average << endl;

34    

35     return 0; // indicate program ended successfully

36    

37    } // end function main

Enter grade: 98 Enter grade: 76 Enter grade: 71 Enter grade: 87 Enter grade: 83 Enter grade: 90 Enter grade: 57 Enter grade: 79 Enter grade: 82 Enter grade: 94 Class average is 81

The counter gets incremented each time the loop executes.

Eventually, the counter causes the loop to end.

The counter gets incremented each time the loop executes.

(12)

Sentinel-controlled Repetition

 

 When number of students’ grades is unknown (prev. example)

 How will loop know when to end?

 Using a special value called sentinel value

 Indicate “end of data entry”

 A sentinel value cannot also be a valid input value

 -1 in this case

(13)

26    

27     // processing phase

28     // get first grade from user

29     cout << "Enter grade, -1 to end: "; // prompt for input

30     cin >> grade; // read grade from user

31    

32     // loop until sentinel value read from user

33     while ( grade != -1 ) {

34     total = total + grade; // add grade to total

35     gradeCounter = gradeCounter + 1; // increment counter

36    

37     cout << "Enter grade, -1 to end: "; // prompt for input

38     cin >> grade; // read next grade

39    

40     } // end while

To read first entry and check condition in 1st iteration

(14)

Nested Control Statement

 

 Control statements can be one inside the another

 Nested building blocks

 Example # 1

A college has a list of test results (1 = pass, 2 = fail) for 10

students. Write a program that analyzes the results. If more

than 8 students pass, print "Raise Tuition".

(15)

1      // Fig. 2.11: fig02_11.cpp

2      // Analysis of examination results.

3      #include <iostream>

4      

5      using std::cout;

6      using std::cin;

7      using std::endl;

8      

9      // function main begins program execution

10    int main()

11    {

12     // initialize variables in declarations

13     int passes = 0; // number of passes

14     int failures = 0; // number of failures

15     int studentCounter = 1; // student counter

16     int result; // one exam result

17    

18     // process 10 students using counter-controlled loop

19     while ( studentCounter <= 10 ) {

20    

21     // prompt user for input and obtain value from user

22     cout << "Enter result (1 = pass, 2 = fail): ";

23     cin >> result;

(16)

16

25     // if result 1, increment passes; if/else nested in while

26     if ( result == 1 ) // if/else nested in while

27     passes = passes + 1;

28    

29     else // if result not 1, increment failures

30     failures = failures + 1;

31    

32     // increment studentCounter so loop eventually terminates

33     studentCounter = studentCounter + 1;

34    

35     } // end while

36    

37     // termination phase; display number of passes and failures

38     cout << "Passed " << passes << endl;

39     cout << "Failed " << failures << endl;

40    

41     // if more than eight students passed, print "raise tuition"

42     if ( passes > 8 )

43     cout << "Raise tuition " << endl;

44    

45     return 0; // successful termination

46    

(17)

Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 2 Enter result (1 = pass, 2 = fail): 2 Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 2 Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 2 Passed 6

Failed 4

Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 2 Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 1 Passed 9

Failed 1

Raise tuition

(18)

Nested Control Statement

(cont

.(

 

 Example # 2

Ask teacher to enter a set of letter grades, then display a summery

of the number of students who received each grade.

(19)

cin >> grade ; char

2      // Counting letter grades.

3      #include <iostream>

4      

5      using std::cout;

6      using std::cin;

7      using std::endl;

8      

9      // function main begins program execution

10    int main()

11    {

12     int grade; // one grade

13     int aCount = 0; // number of As

14     int bCount = 0; // number of Bs

15     int cCount = 0; // number of Cs

16     int dCount = 0; // number of Ds

17     int fCount = 0; // number of Fs

18    

19     cout << "Enter the letter grades." << endl

20     << "Enter X character to end input." << endl;

(20)

20 22     // loop until user types end-of-file key sequence

23     while ( ( grade = cin.get() ) != EOF ) {

24    

25     // determine which grade was input

26     switch ( grade ) { // switch structure nested in while

27    

28     case 'A': // grade was uppercase A

29     case 'a': // or lowercase a

30     ++aCount; // increment aCount

31     break; // necessary to exit switch

32    

33     case 'B': // grade was uppercase B

34     case 'b': // or lowercase b

35     ++bCount; // increment bCount

36     break; // exit switch

37    

38     case 'C': // grade was uppercase C

39     case 'c': // or lowercase c

40     ++cCount; // increment cCount

41     break; // exit switch

42    

Compares grade (anint) to the numerical representations ofA and a.

(21)

43     case 'D': // grade was uppercase D

44     case 'd': // or lowercase d

45     ++dCount; // increment dCount

46     break; // exit switch

47    

48     case 'F': // grade was uppercase F

49     case 'f': // or lowercase f

50     ++fCount; // increment fCount

51     break; // exit switch

52    

53     case '\n': // ignore newlines,

54     case '\t': // tabs,

55     case ' ': // and spaces in input

56     break; // exit switch

57    

58     default: // catch all other characters

59     cout << "Incorrect letter grade entered."

60     << " Enter a new grade." << endl;

61     break; // optional; will exit switch anyway

62    

63     } // end switch

64    

65     } // end while

66    

43     case 'D': // grade was uppercase D

44     case 'd': // or lowercase d

45     ++dCount; // increment dCount

46     break; // exit switch

47    

48     case 'F': // grade was uppercase F

49     case 'f': // or lowercase f

50     ++fCount; // increment fCount

51     break; // exit switch

52    

53     case '\n': // ignore newlines,

54     case '\t': // tabs,

55     case ' ': // and spaces in input

56     break; // exit switch

57    

58     default: // catch all other characters

59     cout << "Incorrect letter grade entered."

60     << " Enter a new grade." << endl;

61     break; // optional; will exit switch anyway

62    

63     } // end switch

64    

65     } // end while

66    

(22)

22 67     // output summary of results

68     cout << "\n\nTotals for each letter grade are:"

69     << "\nA: " << aCount // display number of A grades

70     << "\nB: " << bCount // display number of B grades

71     << "\nC: " << cCount // display number of C grades

72     << "\nD: " << dCount // display number of D grades

73     << "\nF: " << fCount // display number of F grades

74     << endl;

75    

76     return 0; // indicate successful termination

77    

(23)

x

Enter the letter grades.

Enter X character to end input. a

B c C A d f C E

Incorrect letter grade entered. Enter a new grade. D

A b ^Z

Totals for each letter grade are: A: 3

(24)

for

Repetition Statement

 Provide counter-controlled repetition details in a single statement

 Syntax

 for loop repeats actions until condition becomes false 24

for (initialization; loopContinuationCondition; update) action1;

for (initialization; loopContinuationCondition; update) {

(25)

1 // Fig. 5.2: fig05_02.cpp 

2 // Counter­controlled repetition with the for statement. 

3 #include <iostream> 

4 using std::cout; 

5 using std::endl; 

6

7 int main() 

8

9    // for statement header includes initialization,  

10    // loop­continuation condition and increment.     

11    for ( int counter = 1; counter <= 10; counter++ ) 

12       cout << counter << " ";      

13

14    cout << endl; // output a newline 

15    return 0; // indicate successful termination 

16 } // end main 

1 2 3 4 5 6 7 8 9 10  Control-variable name is counter with initial value 1

(26)

for

Repetition Statement (cont

.(

 When loop counter is declared in initialization expression, it can

ONLY be used inside for statement (local variable)

initialization and update expressions can be comma-separated lists

of expressions

26

for(int i=0, j=0; i<4 && j<8; i++,j++)

cout << “*”;

for(int i=0, j=0; i<4 && j<8; i++,j++)

(27)

for

Repetition Statement (cont

.(

initialization, condition and update expressions are optional, but

separated semicolon are required

 Omit initialization

 Initializes control variable earlier in program

 Omit condition

 Always condition is true, may lead to infinite loop

 Omit update

 Calculated as statement in for body

(28)

Examples Using

for

Statement

 Vary control variable from 100 to 1 in increments by -1 (decrement

by 1)

 Vary control variable from 7 to 77 in steps of 7

 Vary control variable over the sequence: 99, 88, 77, 66, 55, 44, 33,

22, 11, 0

28

for(int i = 100; i >= 1; i-- ) for(int i = 100; i >= 1; i-- )

for(int i = 7; i <= 77; i += 7 ) for(int i = 7; i <= 77; i += 7 )

(29)

Examples Using

for

Statement

(cont

.(

 Using a comma-separated list of expressions

can be written as

29

int total =0;

for ( int number = 2; // initialization

number <= 20; // loop continuation condition

total += number, number += 2 ) // total and increment

; // empty statement int total =0;

for ( int number = 2; // initialization

number <= 20; // loop continuation condition

total += number, number += 2 ) // total and increment

; // empty statement

int total =0;

for ( int number = 2; number <= 20; number += 2 ) total += number;

int total =0;

(30)

Example

A person invests 1000$ in a savings account yielding 5% interest.

Assuming that all interest is left on deposit in the account, calculate

and print the amount of money in the account at the end of each year

for 10 years.

Use the formula:

where

p is the original amount invested r is the annual rate

n is the number of years

a is the amount of deposit at the end of the n year 30

n

r

p

(31)

1 // Fig. 5.6: fig05_06.cpp 

2 // Compound interest calculations with for. 

3 #include <iostream>  4 using std::cout;  5 using std::endl;  6 using std::fixed;  7

8 #include <iomanip> 

9 using std::setw; // enables program to set a field width   10 using std::setprecision; 

11

12 #include <cmath> // standard C++ math library      

13 using std::pow; // enables program to use function pow 

14

15 int main()  16

17    double amount; // amount on deposit at end of each year 

18    double principal = 1000.0; // initial amount before interest 

19    double rate = .05; // interest rate 

20

21    // display headers 

22    cout << "Year" << setw( 21 ) << "Amount on deposit" << endl;  23

24    // set floating­point number format 

(32)

32

27    // calculate amount on deposit for each of ten years      

28    for ( int year = 1; year <= 10; year++ )      

29    {       

30       // calculate new amount for specified year       

31       amount = principal * pow( 1.0 + rate, year );      

32        

33       // display the year and the amount       

34       cout << setw( 4 ) << year << setw( 21 ) << amount << endl;  35    } // end for      

36

37    return 0; // indicate successful termination 

38 } // end main 

(33)

do..while

Repetition

Statement

 Similar to while statement but while statement tests

loop-continuation before performing body of loop

 do..while tests loop-continuation after performing body of loop

 Loop body always executes at least once

 Syntax 33

do {

action1; action2; .

.

actionN;

(34)

34

1 // Fig. 5.7: fig05_07.cpp 

2 // do...while repetition statement. 

3 #include <iostream> 

4 using std::cout; 

5 using std::endl; 

6

7 int main() 

8

9    int counter = 1; // initialize counter 

10

11    do      

12    {       

13       cout << counter << " "; // display counter 

14       counter++; // increment counter      

15    } while ( counter <= 10 ); // end do...while  

16

17    cout << endl; // output a newline 

18    return 0; // indicate successful termination 

19 } // end main 

1 2 3 4 5 6 7 8 9 10 

Declare and initialize control variable counter

do…while loop displays counter’s value before testing for counter’s final value

What is the output produced by this loop ??

int counter=1; do

{

cout << counter << endl; }

while (++counter <= 10); int counter=1;

do {

cout << counter << endl; }

(35)

break

Statement

 Alter flow of control

 Causes immediate exit from control structure

 Used with while, for, do…while or switch statements

 Escape early from a loop (while, for, do…while )

 Skip the remainder of switch

(36)

36

1 // Fig. 5.13: fig05_13.cpp 

2 // break statement exiting a for statement. 

3 #include <iostream> 

4 using std::cout; 

5 using std::endl; 

6

7 int main() 

8

9    int count; // control variable also used after loop terminates 

10

11    for ( count = 1; count <= 10; count++ ) // loop 10 times 

12    { 

13       if ( count == 5 )      

14          break; // break loop only if x is 5 

15

16       cout << count << " "; 

17    } // end for  

18

19    cout << "\nBroke out of loop at count = " << count << endl; 

20    return 0; // indicate successful termination 

21 } // end main 

1 2 3 4 

Broke out of loop at count = 5 

Loop 10times

Exit for statement (with a

(37)

continue

Statement

 Used with while, for or do…while statements

 Alter flow of control

 Skips remainder of loop body of current iteration

 Proceeds with next iteration of loop

 With while and do…while statements

 Loop-continuation test is evaluated immediately after continue statement

 With for statement

(38)

38

1 // Fig. 5.14: fig05_14.cpp 

2 // continue statement terminating an iteration of a for statement. 

3 #include <iostream> 

4 using std::cout; 

5 using std::endl; 

6

7 int main() 

8

9    for ( int count = 1; count <= 10; count++ ) // loop 10 times 

10    {  

11       if ( count == 5 ) // if count is 5, 

12          continue;      // skip remaining code in loop 

13

14       cout << count << " "; 

15    } // end for 

16

17    cout << "\nUsed continue to skip printing 5" << endl; 

18    return 0; // indicate successful termination 

19 } // end main 

1 2 3 4 6 7 8 9 10 

Used continue to skip printing 5 

Loop 10 times

(39)

Common Errors

 Compilation errors

 Using commas instead of the two required semicolons in a for header

 Logic errors

 Not initializing counters and totals

 Choosing a sentinel value that is also a legitimate data value

 Placing semicolon immediately after for header or while condition

(40)

Exercise - 1

40

Write a program that asks for a number and reports the

summation from 1 to the number. Use

while

statement.

(41)

Included Sections

41

References

Related documents

Javascript Syllabus Introduction JavaScript Output JavaScript Statements JavaScript Syntax JavaScript Variables JavaScript Operators Control Statements Conditional Statements Data

Does the proposed owner have full knowledge of the proposed insured’s medical history?  Yes  No If ‘no’, provide name and relationship of person who is

This species is distributed from Eastern Europe through Siberia, in- cluding northern Kazakhstan, Mongolia and China, to the Far East and Sakhalin (Cherepanov 1990c,

In Staphylinidae, Paramichrotus Naomi is resurrected as a valid subgenus of Hesperosoma Scheerpeltz with Hemihesperosoma Hayashi placed as a new synonym of it; Sonoma corticina Casey

• After the initialize statement, the condition part of the loop will be executed if the condition becomes true then body of the loop will be executed otherwise the loop will

Υπολογίζω με δύο τρόπους ( με συμμιγείς και δεκαδικούς) 32κ. Με συμμιγείς Με δεκαδικούς.. Βρίσκω με το νου το αποτέλεσμα κάθε διαίρεσης.

Program ::= Statements print Expression Statement ::= Identifier = Expression ; Statements ::= Statement | Statements Statement ..... Introduction (chapter 1) 7 Example: Syntax

In the process of sending email, you will learn about C goto statement, it does not terminates the loop execution but it skip the current iteration of the loop and passes