Making Decisions with Statements


If-statements

Sometimes a program needs to decide either to perform one sequence of statements or another. You can handle that using an If-statement. The form is

  If expression then
    statements to do if the expression has value true
  else
    statements to do if the expression has value false
  %If

Let's reexamine the quadratic equation solver. It should work after modification to handle any values of the coefficients a, b and c. For simplicity, let's assume that a is not 0, since otherwise the equation ax2 + bx + c = 0 is not really quadratic, it is linear. Even so, the equation might not have any solutions at all. An If-statement can decide what to do.

  Let a: Real = 20.0.
  Let b: Real = -50.0.
  Let c: Real = 1.0.
  Let d: Real = b*b - 4*a*c.
  If d >= 0 then
    Let e = sqrt(d).
    Let twoA = 2*a.
    Let x1 = (-b + e)/twoA.
    Let x2 = (-b - e)/twoA.
    Displayln "The first solution is " ++ $(x1).
    Displayln "The second solution is " ++ $(x2).
  else
    Displayln "There are no solutions".
  %If


Indentation

When you write a program, you show structure (which parts are actually part of something else) by indenting. The example above demonstrates indentation. Typically, you indent two spaces.

An If-statement can contain if-statements inside it. When you do that, you indent further to show what belongs to what. To illustrate, let's modify the quadratic equation solver again, this time to handle the special case where there is only one solution.

  Let a: Real = 20.0.
  Let b: Real = -50.0.
  Let c: Real = 1.0.
  Let d: Real = b*b - 4*a*c.
  Let twoA = 2*a.
  If d > 0 then
    Let e = sqrt(d).
    Let x1 = (-b + e)/twoA.
    Let x2 = (-b - e)/twoA.
    Displayln "The first solution is " ++ $(x1).
    Displayln "The second solution is " ++ $(x2).
  else
    If d == 0 then
      Let x = -b/twoA.
      Displayln "The only solution is " ++ $(x).
    else
      Display "There are no solutions\n".
    %If
  %If


Context

Any variables that you create (using Let) inside an If-statement can only be used in that same If-statement. They are destroyed as soon as the program gets out of the If-statement. So

  Let a = 1.
  If a == 0 then
    Let x = 1.
  else
    Let x = 2.
  %If
  Displayln x.
is not allowed since the definition of x is only available inside the If-statement. You are allowed to request an open context, where definitions made inside the If-statement are available after the If-statement, provided things are defined in both parts (before and after else). Just write open in front of If. So
  Let a = 1.
  open If a == 0 then
    Let x = 1.
  else
    Let x = 2.
  %If
  Displayln x.
is allowed.


Leaving off the else part

Sometimes you either want to do something or to do nothing. In that case, you normally just omit the word else, and only write the part to be done when the expression is true. For example.

  If w < 0 then
    Relet w = -w.
  %If
performs the Relet-statement if w is negative, but does nothing if w is not negative.


Problems

  1. [solve] Make the second quadratic equation solver above into a complete program by putting it inside Execute ... %Execute. Modify it to use values of a = 1.0, b = -10.0 and c = 25.0. Test it.

  2. [solve] Write a program that first defines name to be "Sam", then writes "You are wise" if the name begins with W, writes "You are intelligent" if name begins with R, and writes "You are generous" otherwise.

    Of course, since Sam begins with S, the program will write "You are generous". But write the program so that it will do the right thing even after you change the line that defines name to be a different name. For example, if you change the name to "William", it should write "You are wise", without any other changes to the program. Be sure to include Execute ... %Execute.


Summary

An If-statement allows you to make a decision either to do one sequence of statements or another.


Review

A common way to solve problems is to break them down into cases, where you have a different way to solve each case. One way to do that is to decide among equations to use using when phrases.