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

Lab Conditional Statements Part 2

Conditional Practice, Continued

Consider this example from your textbook page 30:

   $roll = rand(1, 6);
   
   echo "You rolled a $roll";
   if ($roll == 6) {
      echo "You win this round!";
   }
   else {
      echo "Sorry, you didn't win this round, better luck next time!";
   }
   
   echo "Thanks for playing";
  1. Let's change the game in the following way:

Nested Conditional Statements

Below we have an example of nested conditional statements:

   $grades = rand(40,100);

   if($grades >= 60){
      if($grades >= 90){
         echo "Congratulations! You Aced it!";
      }
      else{
         echo "You passed";
      }
   }
   else {
      echo "See me after class";
   }
  1. Describe how the program will run when $grades has the value of 87.

  2. Expand upon this snippet of code to have it output different messages for letter grades A-D using conditional statements with the cutoffs: 60, 70, 80, 90.

  3. Based on your past experience with forms,

    1. Write your own form page that reports three names ($nameOne, $nameTwo, $nameThree).
    2. Now write a conditional statement that takes the data from this form and prints the names in alphabetical order.
    3. Review your work, in the case that two (or all three) of the names are the same.

Triangles

Refresher from geometry: In order for three numbers to identify the length of sides in a triangle, the sum of any two must be greater than the third:

   a + b > c
   a + c > c
   b + c > a

Below is a refresher of this concept.

EXAMPLES:

Example A: Consider

For these values, each of the triangle equations are satisfied, so this is a triangle.

Example B: Consider

Here, it is not true that $a + $b > $c, so this is not a triangle.

  1. With this background, write a program to determine whether or not a triangle can be formed , given three sides.

Logical Operators

We have some other operators that help us use more complex conditions.

Logical Operators Function Description
a && b and Both condition a and condition b must be true
a || b or Either condition a, condition b, or both conditions and b are true
a != b not equal Value a is not equal to condition b

Examples

Condition
in English
Equivalent Condition
in PHP
variable $a is between 3 and 10, inclusive ((3 <= $a) && ($a <= 10))
variable $a is NOT between 3 and 10, inclusive ( ! ((3 <= $a) && ($a <= 10)))
variable $a is less than 3 or greater than 10
(equivalent to test $a is not in the interval)
(($a < 3) || ($a > 10))

Some Illustrious Applications

Driver's insurance can be pricey, especially for young people. Fortunately for college-age students, each year it gets cheaper until your mid 30's, and insurers also offer 'good grades' discounts.

The table below lists some fictional insurance rates based on age:

Age Group Average Rate
16-19 $2,999
20-24 $2,040
25-29 $1,707
30-34 $1,591

  1. Assume that the average discount for good grades is 18%, so students who qualify pay 82% of the price listed for their age range,

    1. Write a program that contains the following:
      • Variable $insuranceAge that stipulates the age displayed in the chart above
      • Variable $goodGrades that is either true or false based on whether the student qualifies for the discount
      • The logical comparator variable &&
      • Echo the person's premium
    2. Replace the && with ||. What does this do to the program? Why?
  2. Checking Prerequisites

    1. In order for students to take the Computer Science thesis class, they must have taken CS-141, CS-241, and be declared as a CS-major. Write a program that prints whether or not a student may register for the thesis, based on these prerequisites:
      • $major is Computer Science
      • $CS141 is true
      • $CS241 is true

      Output might be

          Success! You can register for CS 495W
          

      or

          Sorry, you have not fulfilled the prerequisites for this course
          
    2. ENVS-120, Social Systems and the Environment, is only offered to first-year and second-year students who are environmental science or archaeology majors. Edit your code from part a. in order to fit the new parameters:

      • $major is Environmental Science or Archaeology
      • $class is firstYear or secondYear

RGB Values

In digital photography and on Web pages, pictures are divided into pixels, as discussed in the first reading and lab on cascading style sheets. In this setting, colors are seperated into red, green, and blue values, each in the range from 0 to 255.

(0,0,0) is black, white is (255,255,255), (255,0,0) is pure red etc. Here is a table to help you get an understanding of the kinds of colors you can make:

Color Category Swatch RGB Value
Full Red (255, 0, 0)
Orange (234, 137, 0)
Yellow (234, 234, 0)
Full Green (0, 255, 0)
Full Blue (0, 0, 255)
Indigo (33, 0, 149)
Violet (117, 0, 153)
Dark red (106, 0, 0)
White (255, 255, 255)
Black (0, 0, 0)

As you can see, you can customize your color very specifically through using RGB. For more information this website can be helpful:

RGB Chart

With this concept of RGB in mind, we can make each of these three values a variable:

	$red = rand(0,255);
	$green = rand(0,255);
	$blue = rand(0,255);

If we take an example RGB combo (230, 15, 20), its distance from red, rgb(255,0,0), may be found considering the distance of the red, green, and blue values:

	255-100 = 155 (red distance)
	0-15 = -15 (green distance)
	0-20 = -20 (blue distance)

Then these results are squared results and added to get a distance (actually a distance squared): (155)2 + (-15)2 + (-20)2 = 24650

Comparing these computations against red (rgb(255,0,0)), green (rgb(0,255,0)), and blue (rgb(0,0,255)) will indicate what primary color is closest to the given color.

  1. Comparing colors:

    1. Write a program that determine which of the following colors is closest to blue.
      1. (10,40,200)
      2. (127, 127, 127)
      3. 255, 10, 200)
    2. Follow a similar approach to determine which of the following is closest to green.
      1. (0, 164, 117)
      2. (89, 190, 82)
      3. (15, 15, 200)

Reflection

  1. Both the previous lab and this lab have provided experience with various forms of if statements and conditions.

    Concepts:

    Time for reflection:

    1. To what extent are you comfortable with each of these ideas?
    2. Do all of the ideas seem clear?
    3. Are some confusing? (Please talk to the instructor about any questions!)
created 4 February 2019 by Zhen Zhen McMahon
modest editing 9 February 2019 by Zhen Zhen McMahon and Henry M. Walker
Valid HTML 4.01! Valid CSS!