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

Laboratory Exercise on For and While Loops, Part 1

Be sure to read pages 33 - 44 in your textbook.

For Loops

  1. Consider the loop:

    for($i = 6; $i <= 12; $i++){
    	echo $i
         }
       
    1. Where does this for loop begin?
    2. When does this for loop end?
    3. What does this for loop print out?
  2. Next, consdier this loop

     for($i = 20; $i <= 10; $i--){
    	echo $i;
    	}
       
    1. Where does this for loop begin?
    2. When does this for loop end?
    3. What does this for loop print out?
  3. What does this for loop print out?

    	for($i = 1; $i <= 50; $i ++){
    		$i = $i*2;
    		echo $i;
    	}
       
  4. Using a for loop, print out numbers 1 through 10:

    1. Format the numbers so they appear on a single line with a space between each number.
    2. Format the numbers, so they appear on separate lines.
  5. a) Initialize a constant, doubleIt, outside of the for loop and set it equal to 1. Within the for loop multiply doubleIt by 2, and print out each multiple 10 times.
    b) Now print each item in a new line.
  6. A Table of Fahrenheit and Celsius Temperatures

    Given a temperature in Fahrenheit, the equivalent temperature in Celsius is given by the formula:

    Celsius = (Fahrenheit - 32) * 5.0 / 9.0
      

    Write a program that shows the Celsius temperatures corresponding to Fahrenheit temperatures 50, 51, 52, ..., 70. For example, possible output might have the form:

    Fahrenheit   Celsius
        50        10.0
        51        10.6
        52        11.1
        53        11.7
        54        12.2
        55        12.8
        56        13.3
        57        13.9
        ...       ...
      
  7. Interest in a Savings Account

    A simple savings account earns compound interest over time, based upon an annual interest rate (e.g., 1% or 9%) per year. The basic computations proceed as follows:

While Loops

  1. What is printed by the following loop?

     
    	$x = 4
    	while($x <= 10){
    		echo $x;
    		$x ++;
    	}
      
  2. What does this while loop print out?

     
    	$x = 5;
    	while(x != 0){
    		echo $x;
    		$x--;
    	}
       
  3. Write a while loop that prints out numbers from 13 to 26.

  4. Write code utilizing the following outline:

    Explain in words what this program does.

  5. Write code with the following steps that uses while loop to count down from 10 to 0, printing each item on a new line. After the count down is complete, the code should print "done".

Determining Whether to Use a For loop or a While loop

For the following questions, state whether you should use a for loop or a while loop. Then explain why you chose to use that loop.

  1. Task: Loop through all odd numbers from 1 to 100.

  2. Terminology: An integer $i is said to be divisible by the integer $d, if division of $i by $d yields no remainder. (In terms of PHP, $i % $d = 0 (recall % is PHP's symbol for finding a remainer.)

    Prime numbers: A number $i is said to be prime, if $i is divisible only by 1 and itself. (That is, $i is not divisible by any integers 2, ..., $i-1.)

    Task: Given an integer $i, determine whether or not it is prime.




created 24 January 2019 by Saniya Lakka
revised 26-31 January 2019
Valid HTML 4.01! Valid CSS!