Control Structure:
the most important part of the course

Loops: while, for, do-while

  1. nottoprogram.C: how not to program.
    Conventional to count from 0 to 9 inclusive, not from 1 to 10 inclusive, because “array subscripts” will start at 0.
  2. while.C: while loop has same (parentheses) and {curly braces} braces as the if statement. Prefix increment operator ++.
    1. 0 to 9 (inclusive) by 1’s: the < operator
    2. 10 to 20 (inclusive) by 1’s
    3. 10 to 20 (inclusive) by 2’s: the three vital statistics
    4. 1 to 1,024 = 210 (inclusive), doubling each time.
      Humans like to see a column of strings left justified, a column of numbers right justified.
         1     John
         2     Joe
         4     Joey
         8     Jacob
        16     Jake
        32     Johann
        64     Jimmy
       128     James
       256     Judd
       512     Jedediah
      1024     Johnny
      
      #include <iomanip>    //for setw (means "set width")
      
      		cout << setw(4) << "\n";   //right justify
      
    5. count down from 9 to 0 (inclusive): the prefix decrement operator -- (two minus signs, no space between them)
    6. How long does it take to loop a billion times (e.g., from 0 to 999,999,999 inclusive)? Don’t output the numbers.
      jsmith@storm:~$ time ./a.out
      
    7. iterate zero times
  3. for loops:
    1. for1.C. Put all three vital statistics on the same line: start, end, stride.
    2. for2.C: make the induction variable i local to the for loop.
    3. Exercise. Write a C++ program named hide.C whose output is this. Use the assigment operator += which we saw in assignment.C to add 5 to the induction variable.
    4. beer.C: A Hundred Bottles of Beer on the Wall.
      Interrupt the C++ program with the keystroke control-c.
    5. stylesheet1.C and stylesheet1.html. Direct the output into an output file named stylesheet1.html
      jsmith@storm:~$ cd
      jsmith@storm:~$ pwd
      
      jsmith@storm:~$ wget https://markmeretzky.com/fordham/1600/src/loop/stylesheet1.C
      jsmith@storm:~$ ls -l        (There should be a new file named stylesheet1.C)
      jsmith@storm:~$ c++ stylesheet1.C
      
      jsmith@storm:~$ ./a.out > stylesheet1.html
      jsmith@storm:~$ ls -l        (There should be a new file named stylesheet1.html)
      jsmith@storm:~$ cp stylesheet1.html public_html
      
      Then point your web browser at
      https://storm.cis.fordham.edu/~jsmith/stylesheet1.html
      where jsmith is your Fordham login name.
    6. chinese.C and chinese.html: loop through a range of 16 consecutive Chinese characters, using Unicode character code numbers.
    7. infinite.C: an infinite loop.
      Interrupt the C++ program with the keystroke control-c.
    8. pierogies.C: needs only one variable, h.
      Use setw(2) to make the columns line up neatly. You will have to include the header file <iomanip> in order to use setw.
    9. thruway.C: needs only one variable, a. Typical sign.
    10. Add up all the integers from 1 to 100 inclusive. (The sum should be 5,050.)
      1. sum1.C: do it all with one big expression.
      2. sum2.C: do it with a series of small expressions
      3. sum3.C: write the small expression only once, inside a for loop.
      Now modify sum3.C to add up all the integers from 1 to 1,000,000 inclusive. The sum should be 500,000,500,000—about five hundred billion—well beyond the maximum capacity of a variable of data type int. You will therefore have to change the data type of the variable sum from plain old int to the long int that we saw here. (For the expression (n + 1) * n / 2 you will also have to change the data type of the variable n from int to long int.) What will happen if you don’t? (Note: do not change int main to long int main.)
    11. Make an investment grow at 6% per year annually for 10 years.
      1. product1.C: do it all with one big expression. pow
      2. product2.C: do it with a series of small expressions.
      3. product3.C: write the small expression only once, inside a for loop.
      Exercise. Have product3.C ask the user three questions. In response to these questions, let him or her input
      1. the principal into a double variable named principal
      2. the number of years into an int variable named nyears
      3. the annual rate of interest (as a percentage) into a double variable named rate, as shown below
      	double rate {0.0};
      	cout << "What is the annual interest rate in percent (e.g., 6)? ";
      	cin >> rate;
      
      	if (!cin) {
      		//Ouput an error message to cerr and return EXIT_FAILURE
      	}
      	//Also make sure the rate is a reasonable (e.g., positive) number.
      
      	//Each year, multiply the principal by the following factor.
      	//For example, if rate is 6, then factor will be 1.06
      	double factor {1.00 + .01 * rate};
      
    12. organ.C: Make a one-octave set of organ pipes.
    13. converge.C: converge on the value of
      π = 4/1 − 4/3 + 4/5 − 4/7 + 4/9 − 4/11 + 4/13 …
  4. dowhile.C: do-while loop has test at the bottom. Always iterates at least once.