Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.

Similar presentations


Presentation on theme: "CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct."— Presentation transcript:

1 CHAPTER 5 CONTROL STRUCTURES II (Repetition)

2 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

3 THE while LOOPING (REPETITION) STRUCTURE The general form of the while statement is while(expression) statement

4 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

5 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; }

6 Demonstrate a counter loop Star t 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

7 // This program displays the numbers 1 through 10 and // their squares. #include using namespace std; void main(void) { int Num = 1; 1 cout << "Number Number Squared\n"; cout << "-------------------------\n"; while (Num <= 10) 2 { cout << Num << "\t\t" << (Num * Num) << endl; 3a Num++; 3b }

8 Program Output Number Number Squared ------------------------- 1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64 9 81 10 100

9 Case 2: Sentinel Controlled while Loop cin>>variable; while(variable != sentinel) {. cin>> variable;. }

10 This program demonstrates a loop using sentinel Star t 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

11 // This program demonstrates a simple while loop and 99 is the // sentinel #include 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;1 while (Number != 99) 2 cin >> Number; 3a & 3b }

12 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;. }

13  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;. }

14 The eof Function Suppose 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.

15 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); }

16 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.

17

18 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; } for(init;test;update) { statement; for (initialization;test;update) { statement; } statement; } The Nested Loop for (init1,init2; test; update1,update2) statement; init; for (; test; update) statement;

19

20 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<<" ";

21 THE do … while LOOPING (REPETITION) STRUCTURE The general form of a do...while statement is: do statement while(expression);

22 Example: Consider the following two loops (a) (b) 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.

23 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.

24  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; }

25 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.

26 sum = 0; cin>>num; while(cin) { if(num < 0) { cout<<"Negative number found in the data"<<endl; continue; } sum = sum + num; cin>>num; }

27 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; }

28 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.

29 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--)


Download ppt "CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct."

Similar presentations


Ads by Google