teaching machines

CS 145 Lecture 20 – For

October 26, 2016 by . Filed under cs145, fall 2016, lectures.

Dear students,

Hey. Check out this while loop:

int i = 0;
while (i < 5) {
  System.out.println(i);
  ++i;
}

Let’s abstract this out a bit into its four parts and pieces:

init
while (condition) {
  body;
  update;
}

Loops are powerful, but any time you put a motor in something, it gets away from you pretty quickly. In the case of while, it’s really easy to forget the update step—and you end up with an infinite loop. Also, the initialization step may introduce variables into the surrounding scope that are really only relevant to the loop. Enter for, a loop that is not really any different from while, but it can fix both problems. It has this syntax:

for (init; condition; update) {
  body;
}

The for loop forces you to think about all four parts of a loop, making it hard to forget one. Any variables declared in the initialization step are visible only within the loop. Otherwise, the behavior and power of the loops is identical. Don’t let anyone tell you otherwise. Every for loop can be rewritten as a while loop and vice versa.

So, when do you use for and when do you use while? Here’s my criteria:

Use a for loop when the continuation logic is simple and the update involves a steady march through your data. When the continuation logic gets involved or the update step involves bouncing around, use while. The ultimate goal is to write code that you and others can read weeks and months later.

Let’s see some for loops in action for some of the following problems:

See you next class!

Sincerely,

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

FlashMod.java

package lecture1026;

import java.util.Random;
import java.util.Scanner;

public class FlashMod {
  public static void main(String[] args) {
    Random generator = new Random();
    Scanner in = new Scanner(System.in);
    int nRight = 0;

    for (int i = 0; i < 10; ++i) {
      int n = generator.nextInt(40 + 5);
      int d = generator.nextInt(20) + 1;
      System.out.printf("%d %% %d = ", n, d);

      // keep getting input until we have an int!
      while (!in.hasNextInt()) {
        in.nextLine();
        System.out.printf("%d %% %d = ", n, d);
      }
      
      int answer = in.nextInt();

      if (n % d == answer) {
        System.out.println("CORR-ect!");
        ++nRight;
      } else {
        System.out.println("IN-correct!");
      }
    }

    System.out.println("You got " + nRight + "/10!");
  }
}

Unitable.java

package lecture1026;

public class Unitable {
  public static void main(String[] args) {
    for (char c = ' '; c < 65535; ++c) {
      System.out.printf("%c  ", c);
      if (c % 20 == 0) {
        System.out.println();
      }
    }
  }
}

MouseTakeover.java

package lecture1026;

import java.awt.AWTException;
import java.awt.Robot;

public class MouseTakeover {
  public static void main(String[] args) throws AWTException {
    int n = 360;
    double angle = 0;
    double jump = 360.0 / n;
    
    Robot robb = new Robot();
    
    for (int i = 0; i < 5 * n; ++i) {
      double x = 200 * Math.cos(Math.toRadians(angle)) + 600;
      double y = 200 * Math.sin(Math.toRadians(angle)) + 400;
      
      robb.mouseMove((int) x, (int) y);
      robb.delay(1);
      
//      angle = angle + jump;
      angle += jump;
    }
  }
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *