320 likes | 541 Views
CSC530 Data Structures - LOOP. Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA 19383 zjiang@wcupa.edu. Loop. Price is right. Sample execution (click on this link to try) Each button in the above sample REPEAT …?. While loop Format & Logic.
E N D
CSC530 Data Structures - LOOP Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA 19383 zjiang@wcupa.edu
Loop • Price is right. • Sample execution (click on this link to try) • Each button in the above sample REPEAT …?
While loop • Format & Logic
<initialization>; while (<test>) { <body>; }
Do-while loop • Format • Logic
How does this differ from the while loop? • The controlled <statement(s)> will always execute the first time, regardless of whether the <test> is true or false.
For loop • Format • Logic
for (<init>; <test>; <update>) { <body>; }
Summary Body first, and then event change/update
Controlling Number of Loop Iterations If the number of iterations is known before the loop starts, the loop is called a count-controlled loop. Counter =0, counter++, counter <number Counter = 1, counter++, counter <=number Use for loop for an easy development.
Code: for (int i = 1; i <= 4; i++) { cout << i << " squared is " << (i * i) << endl; } Output: 1 squared is 1 2 squared is 4 3 squared is 9 4 squared is 16 Counter-controlled loop? How many iterations? What are they? 1st? 2nd? 3rd?
Code: cin >> n; // try 6! for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cout << “*”; } cout <<endl; } Output: ****** ****** ****** ****** ****** ****** Counter-controlled loop? How many iterations? What is the body? 1st iteration? 2nd Iteration? 3rd iteration?
Code: cin >> n; // try 5! for (int i = 1; i <= n; i++) { for (int j = 1; j <= 10; j++) { cout <<(i * j) << " "; } cout << endl; } Output: 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 Counter-controlled loop? How many iterations? What is the body? 1st iteration, each +1? 2nd Iteration, each +2? 3rd iteration, each +3? i, i+I, i+i+I, …?
Code: cin >> n; // try 6! for (i = 1; i<=n; i++) cout << “*”; cout <<endl; for (i = 1; i <= n-2; i++) { cout << “*”; for (int j = 1; j <= n-2; j++) cout <<“ ”; cout <<“*” <<endl; } for (i = 1; i<=n; i++) cout <<“*”; cout << endl; Output: ****** * * * * * * * * ****** What is the body? 1st iteration? 2nd Iteration? 3rd iteration? Counter controlled loop
Code: cin >> n; // try 6! for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { cout << "*"; } cout <<endl; } Output: * ** *** **** ***** ****** i * each line!
Code: cin >> n; // try 6! for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { cout << i; } cout << endl; } Output: 1 22 333 4444 55555 666666 How many i each line?
Code: cin >> n; // try 5! for (int i = 1; i <= n; i++) { for (int j = 1; j <= (n - i); j++) { cout << " "; } for (int k = 1; k <= i; k++) { cout << i; } cout << endl; } Output: 1 22 333 4444 55555 Space and numbers?
Otherwise (unknown or unclear), the loop is called a event-controlled loop. Use a while loop or a do-while loop for an easy checkpoint development. Asking the user before each iteration if it is time to end the loop is called the ask-before-iterating technique. Appropriate status update (or event initializing) for a sequence of iterations Controlling Event of Loop Iterations
Finds and prints a number's first factor other than 1: cin >> n; // try 91 int f = 2; while (n % f != 0) { f++; } cout << "First factor:“ << f <<endl; Sample run: First factor:7 Body: exploring the factor. When/what to stop the loop? n% f = = 0 divisible! The range of f? Initialization of f? The change of f?
Write a program that will repeatedly prompt the user to type a number until the user types a non-negative number, then square it. Example log: Type a non-negative integer: -5 Invalid number, try again: -1 Invalid number, try again: -235 Invalid number, try again: -87 Invalid number, try again: 11 11 squared is 121
cout << "Type a non-negative integer: "; cin >> n; while (n < 0) { cout << "Invalid number, try again: "; cin >> n; } int square = n * n; cout << n << " squared is " << square<<endl; Notice that the number variable had to be declared outside the while loop in order to remain in scope. Body: trying different value of n. When/what to stop the loop? n>= 0 non-negative! The range of n? -> any (since n<0) Initialization of n? The change of n?
Write a class named DigitSum that reads an integer from the user and prints the sum of the digits of that number. You may assume that the number is non-negative. Example: Enter a nonnegative number: 29107 prints out 19 (i.e.,2+9+1+0+7 ) Hint: Use the % operator to extract the last digit of a number. If we do this repeatedly, when should we stop?
cin >> n; int sum = 0; while (n > 0) { sum += n % 10; // add last digit to sum n = n / 10; // remove last digit } cout << “sum = “ << sum << endl; Body: adding the last digit and extracting that from the original number (for next round) When/what to stop? All digits are counted! n <= 0 no more need to count! The change of n? -> make the extraction valid Initialization?
Write a program named CountFactors that reads in an integer and displays its number of factors. For example, if the user enters 60, CountFactors displays 12 because 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, and 60 are all factors of 60. Body: try all possible numbers k, count k (sum++) only when n is divisible by k. When/what to stop? K>n (trial from 1 is over) The range of k? -> 1 to n Change? k++ Initialization? cin >> n; int sum = 0, k = ?; while ( ) { } cout << “sum = “ << sum <<endl;
Exercise population TV purchase 1+2+4+8+... 1+2+3+4+...+99