1 / 29

CHAPTER 5 CONTROL STRUCTURES II (Repetition)

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

Download Presentation

CHAPTER 5 CONTROL STRUCTURES II (Repetition)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 5CONTROL 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 whileLOOPING (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; statement; statement; }

  6. Demonstrate a counter loop 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 Start initialize the Num to 1 F Num <= 10 T Output Num and Num*Num Increment Num by 1 Stop

  7. // This program displays the numbers 1 through 10 and // their squares. #include <iostream> 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 12 43 94 165 256 367 498 649 8110 100

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

  10. This program demonstrates a loop using sentinel 1)Initialize Number to 0 2)Prompt user to enter an number 3)Repeat while Number is not 99 enter an number End while Start Initialize Number to 0 Prompt user to enter a number T Number !=99 Enter number F Stop

  11. // 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; 1 while (Number != 99) 2 cin >> Number; 3a & 3b }

  12. Case 3: Flag-Controlled whileLoops • 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 forLOOPING (REPETITION) STRUCTURE The general form of the for statement is for(initial statement; loop condition; update statement) statement • The initialstatement, loopcondition and updatestatement (called for loop control statements) that are enclosed with in the parentheses controls the body (the statement) of the for statement.

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

  18. 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<<" ";

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

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

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

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

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

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

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

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

  27. 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--)

More Related