CPSC 210

B5: Control Flow

Learning Goals

  • Determine the value of a variable
  • Read and trace control flow (method calls, loops, conditionals)
    • by hand and by using the debugger
  • Represent control flow for a method with flowcharts
  • Use conditional statements for conditional control flow
  • Use a loop for repeated execution flow

Conditionals

public boolean isGreaterThan50(int number) {
  if (number > 50) {
    return true;
  } else {
    return false;
  }
}

isGreaterThan50(20); // false
isGreaterThan50(60); // true
public int compare(int a, int b) {
  if (a == b) {
    return 0;
  } else if (a < b) {
    return -1;
  } else {
    return 1;
  }
}

int z = compare(1, 1); // 0
int y = compare(1, 2); // -1
int x = compare(2, 1); // 1

Loops

void printXTimes(String somethingToPrint, int x) {
  for (int i = 0; i < x; i++) {
    System.out.println(somethingToPrint);
  }
}

printXTimes("Heellooo", 3);

// Heellooo
// Heellooo
// Heellooo
ArrayList<Dog> dogs = new ArrayList();
Dog snoopy = new Dog("Snoopy");
Dog oscar = new Dog("Oscar");
Dog willow = new Dog("Willow");
dogs.add(snoopy);
dogs.add(oscar);
dogs.add(willow);

for (Dog dog : dogs) {
  System.out.println(dog.getName());
}

// Snoopy
// Oscar
// Willow
int index = 5;

while (index > 2) {
  System.out.println(index);
  index--;
}

// Now at: 5
// Now at: 4
// Now at: 3

Flow Charts

public void spin() {
    System.out.println("Spinning!");
    System.out.println("Dizzy...");
}

print "Spinning!"

print "Dizzy..."

End

Start

Flow Charts (2)

public void dance(String songName) {
    System.out.println("Dancing to 
    	" + songName + "!");
    jump();
    spin();
    System.out.println("One more time!");
    jump();
}

public void jump() {
    System.out.println("Jumping!");
}
public void spin() {
    System.out.println("Spinning!");
    System.out.println("Dizzy...");
}

Start

print "Dancing..."

jump()

spin()

print "One more time!

jump()

End

Flow Charts (3)

public void printNum(int i) {
    if (i == 3) {
        print("three");
    } else {
        print("not 3!");
        print(i);
    }
    print("done");
}

if i == 3

print "not 3"

F

T

print "three"

print i

print "done"

End

Start

Flow Charts (4)

public void printList(List<String> theList) {
    for (String s : theList) {
        print(s);
        print("done item "+s);
    }
    print("done "+theList);
}

Start

for: s in theList

T

F

print "done..."

print "done item"

End

print s

Lecture Ticket Review

public class Person {
    private int age;
    private String status;

    public Person() {
        this.status = "young";
        this.age = 0;
    }

    public void getOlderByYears(int years) {
        this.age = this.age + years;
        if (age > 1) {
            status = "old";
        }
    }
}
public class Main {
    public static void main(String[] args) {
        Person bubbles = new Person();
        Person buttercup = new Person();
        Person blossom = new Person();

        ArrayList<Person> people = new ArrayList<>();
        people.add(bubbles);
        people.add(buttercup);
        people.add(blossom);

        int ageAmount = 1;
        for (Person p : people) {
            p.getOlderByYears(ageAmount);
            ageAmount = ageAmount + 1;
            //POINT A
        }
        //POINT B
    }
}

Lecture Lab

We need flow charts for

Tank.handleBoundary
SIGame.moveMissiles
SIGame.checkGameOver

B5: Control Flow

The End - Thank You!

Made with Slides.com