next up previous
Next: Shortcut evaluation of a Up: Unit 05 Previous: Ambiguity of the else

Example: type of a triangle

Given three values representing the lengths of the three sides of a triangle, determine whether the triangle is regular (all three sides are equal), symmetric (two sides are equal), or irregular (no two sides are equal).

A possible algorithm is the following: compare the sides two by two, until we have gathered sufficient information to decide the type of the triangle.

The algorithm can be implemented as follows:

double first, second, third;
...
if (first == second) {
  if (second == third)
    System.out.println("regular");
  else
    System.out.println("symmetric");
} else {
  if (second == third)
    System.out.println("symmetric");
  else if (first == third)
    System.out.println("symmetric");
  else
    System.out.println("irregular");
}


next up previous
Next: Shortcut evaluation of a Up: Unit 05 Previous: Ambiguity of the else