CS 199 Willamette University Spring, 2019
 
Programming in PHP, Databases with MySQL,
and Web Applications
 

Laboratory Exercise on For and While Loops, Part 2

We will be using with Chars(characters) and nested loops.

For Loops

  1. Write a for loop that prints out numbers from -7 to +7 inclusive.

    1. Write the numbers in ascending order: -7, -6, ..., 7
    2. Write the numbers in descending order: 7, 6, 5, ..., -7
  2. Consider the following code segment:

     
       for ($row = 1; $row < 4; $row++) {
           for ($column = 1; $column <= $row; $column++) {
               echo " * ";
           }
           echo "<br />";
        }
        

    1. What are the bounds of the first for loop?
    2. What are the bounds on the second for loop?
    3. What is executed inside the nested for loop?
    4. What is executed inside the outer for loop?
    5. What does this for loop print out?
    6. What would happen if you put the new line echo statement inside the nested for loop as opposed to in the outer for loop?
  3. Create a nested for loop that prints out row numbers 1 through 5 rather than the asterisks in the previous problem. The output should have the form:

      1
      22
      333
      4444
    
  4. Create a nested for loop that prints out a checkered board.

    Hints:

While Loops

  1. Consider this code segment:

     
        $row = 1;
        $col = 1;
        while( $row < 3 )
        {
            while( $col < 3 )
            {
                echo ":)  ";
                $col++;
             }
            echo "<br />";
            $row++;
        }
        
    1. What are the bounds of the first while loop?
    2. What are the bounds on the second while loop?
    3. What is executed inside the nested while loop?
    4. What is executed inside the outer while loop?
    5. What does this while loop print out?
  2. Printing box shapes

    1. Create a nested for loop that prints out 1 through 4 in a box shape. So 1's would take up the first row, 2's would take up the second row, etc.
    2. Create the same box shape, but with 1's in the first column, 2's in the second column and so on.
    3. Create another box shape, but add the row number to the column number and print out its sum.

Final Problem

From the previous lab, you created a table that produced a table of monthly balances

  1. Using the same equation for monthly rate, interest and new balance, create similar tables, but continue the printout until the bank balance doubles.

    1. In this program, one table should show monthly balances, using an annual rate of 8%, starting with $100—the same numbers used in the previous lab.
    2. Expand the program to include a second table, using an annual rate of 11%., still with a $100 starting balance.
  2. Revise the previous problem, so that it prints just the interest rate and the number of months to double, for interest rates of 5%, 6%, through 12%.

    Hint: Go through the work of what you just did, but do not print every month, but instead just print the total number of months after it is doubled.

    As an example, the output might begin as follows:

      Interest     Months
        Rate     to Double
        0.05        167
        0.06        139
        0.07        120
         ...        ...
          


created 5 February 2019 by Saniya Lakka
revised 5-10 February 2019 by Saniya Lakka, with minor reformatting and editing by Henry M. Walker
Valid HTML 4.01! Valid CSS!