teaching machines

CS1: Lecture 19 – If Diversions

October 16, 2019 by . Filed under cs1, fall 2019, lectures.

Dear students,

We’ve seen the computer as a calculator, crunching numbers. We’ve seen the computer as a chef, calling upon recipes of code. We’ve seen the computer as a philosopher, considering truths about our data. Now it’s time to let those truths drive our computer’s actions. We will see the computer as a pilot, navigating wild routes through our code based on conditions.

Diversions

Normally, the flow of control in a program goes from the top of a method to its bottom, one statement after another. The ball gets rolling in main, and it may temporarily pause its execution to let another method do some work. But in general, it’s one statement after another. Sometimes, however, we want to do some extra work under certain conditions. I call this pattern a diversion, and we structure it like so:

// pre work
if (booleanCondition) {
  // diversion work
}
// post work

The key observation here is that the diversion doesn’t always get executed. Only when booleanCondition is true. Let’s see this pattern in some real contexts:

Bifurcations

In other situations, we actually want to choose between two alternative bits of code. We want to take some action when a condition is true, but some other action when the condition is false, but not both actions. They are mutually exclusive. I call this pattern a bifurcation, and it has this structure:

// pre work
if (booleanCondition) {
  // then work
} else {
  // else work
}
// post work

Only one of the two blocks will get executed. Let’s also see this pattern in some real contexts:

TODO

Here’s your TODO list to complete before we meet again:

See you next class!

Sincerely,

P.S. It’s time for a haiku!

Do good? Or evil?
Harvey Dent sees heads or tails
When “faced” with a choice

P.P.S. Here’s the code we wrote together in class…

Ifs.java

package lecture1016.cs145;

public class Ifs {
  public static void main(String[] args) {
    int nMessages = 3;

    System.out.printf("You've got %d %s!%n", nMessages, pluralize("message", nMessages));
    System.out.println(getDamageHP(9001, 2));

  }

  public static String pluralize(String singular, int quantity) {
    String form = singular;
    if (1 != quantity) {
      form += "s";
    }
    return form;
  }

  public static int getDamageHP(int attack, int defense) {
    int hp = Math.max(attack - defense, 0);
    if (Math.random() <= 0.05) {
      hp += 100;
    }
    return hp;
  }
}

Ifs.java

package lecture1016.cs148;

public class Ifs {
  public static void main(String[] args) {
    System.out.println(getDamage(1000, 235));
    System.out.println(sanitizeUrl("coolmathgames.com"));
  }

  public static int getDamage(int attack, int defense) {
    int damage = Math.max(0, attack - defense);
    if (Math.random() >= 0.9) {
      damage *= 2;
    }
    return damage;
  }

  public static String sanitizeUrl(String url) {
    String sanitizedUrl = url;
    if (!sanitizedUrl.startsWith("https://")) {
      sanitizedUrl = "https://" + sanitizedUrl;
    }
    return sanitizedUrl;
  }

  
}