Previous Page Next Page

6.5. What's Next?

In the next chapter, we discuss the Perl control structures, how to test whether a condition is true or false with if and unless constructs, how to block statements, how to use loops to repeat a statement(s), and how to break out of loops, use labels, and nest loops.

Exercise 6: Operator, Operator

1.Print the average of three floating point numbers with a precision of two decimal places.
2.What are two other ways you could write

$x = $x + 1;


3.Write the following expression using a shortcut:

$y = $y + 5;


4.Calculate the volume of a room that is 12.5 ft. long, 9.8 ft. wide and 10.5 ft. high.
5.Square the number 15 and print the result.
6.What would the following program print?

$a = 15;
$b = 4;
$c = 25.0;
$d = 3.0;
printf ("4 + c / 4 * d = %f\n", 4 + $c / 4 * $d);
printf ("a / d * a + c = %.2f\n", $a / $d * $a + $c);
printf ("%d\n", $result = $c / 5 - 2);
printf ("%d = %d + %f\n", $result = $b + $c, $b, $c);
printf ("%d\n", $result == $d);


7.Given the values of $a=10, $b=3, $c=7, and $d=20, print the value of $result:
  1. $result = ( $a >= $b ) && ( $c < $d );

    print "$result\n";

  2. $result = ( $a >= $b ) and ( $c < $d );

    print "$result\n";

  3. $result = ( $a < $b) || ( $c <= $d );

    print "$result\n";

  4. $result=( $a < $b) or ( $c <= $d );

    print "$result\n";

  5. $result = $a % $b;

7.Write a program called convert that converts a Fahrenheit temperature to Celsius using the following formula.

C = ( F - 32 ) / 1.8


8.Create an array of five sayings:
"An apple a day keeps the doctor away"
"Procrastination is the thief of time"
"The early bird catches the worm"
"Handsome is as handsome does"
"Too many cooks spoil the broth"

Each time you run your script, a random saying will be printed. Hint: the index of the array will hold a random number.

9.The following formula is used to calculate the fixed monthly payment required to fully amoritize a loan over a term of months at a monthly interest rate. Write a Perl expression to represent the following formula where: P = principal amount borrowed, r = periodic interest rate (annual interest rate divided by 12), n = total number of payments (for a 30-year loan with monthly payments, n = 30 years x 12 months = 360), and A = periodic payment.


Previous Page Next Page