Docsity

Conditional Statements - Computer Programming and Problem Solving | CS 111, Study notes of Computer Science

Partial preview of the text

Download Conditional Statements - Computer Programming and Problem Solving | CS 111 and more Study notes Computer Science in PDF only on Docsity!

CS111 Computer Programming

Department of Computer Science

Wellesley College

Conditional Statements

Friday, September 28, 2007

Conditionals 8-

Spider sense

Actuators change the world:

forward();

left();

brushDown();

dropBagel();

Sensors detect the world:

isFacingWall();

getColor();

isOverBagel();

Control takes input from

sensors to do something

through actuators

Conditionals 8-

WallHuggerWorld

Straight-line code

forward();

forward();

left();

forward();

Conditionals 8-

Cruelty to Buggles

Straight-line code

forward();

forward();

left();

forward();

Conditionals 8-

Conditional statements*

class WallHugger extends Buggle {

// Move forward one step // unless facing a wall, // in which case turn left. public void followWall() {

if (isFacingWall()) { left(); } else { forward(); } } }

*In general, write: if ( ) { <statements in “then” clause> } else { <statements in “else” clause> }

Conditionals 8-

Repeating ourselves

class WallHugger extends Buggle { public void followWall() { if (isFacingWall()) { left(); } else { forward(); } } }

public class WallHuggerWorld extends BuggleWorld { public void run () { WallHugger peterParker = new WallHugger(); peterParker.setPosition(new Location(5,3)); peterParker.followWall(); peterParker.followWall(); … } }

Conditionals 8-

Repeating ourselves more succinctly

class WallHugger extends Buggle {

public void followWall() {

… // same as before

public void followWall2() {

followWall();

followWall();

public void followWall4() {

followWall2();

followWall2();

public void followWall8() {

followWall4();

followWall4();

public class WallHuggerWorld

extends BuggleWorld {

public void run () {

WallHugger peterParker =

new WallHugger();

peterParker.setPosition(

new Location(5, 3));

peterParker.followWall8();

Conditionals 8-

Capturing the pattern

// Allows instances of subclasses to

// perform some action on every clock

// tick (a predetermined number of times).

public class TickBuggle extends Buggle {

public void tick() {

// Default is to do nothing

public void tick2() {

tick();

tick();

public void tick4() {

tick2();

tick2();

public void tick1024() {

tick512();

tick512();

} // class TickBuggle

Conditionals 8-

WallHuggerWorld

WallHugger peterParker = peterParker.setPosition(new Location(5,3)); peterParker.tick4();

Object Land

Execution Land

.run()

A spider bites Peter

this

WHW

WHW

WHW

color red

brushDown true

WallHugger WH

heading EAST

position (1,1)

WH

peterParker WH

Conditionals 8-

WallHuggerWorld

WallHugger peterParker = .setPosition( ); peterParker.tick4();

Object Land

Execution Land

.run()

Peter jumps

this

WHW

WHW

WHW

color red

brushDown true

WallHugger WH

heading EAST

position

WH

peterParker WH

WH1 (5,3)

Conditionals 8-

WallHuggerWorld

WallHugger peterParker = .setPosition( ); .tick4();

Object Land

Execution Land

.run()

And the clock starts ticking...

this

WHW

WHW

WHW

color red

brushDown true

WallHugger WH

heading EAST

position

WH

peterParker WH

WH

WH

.tick4()

this

WH

WH

tick2(); tick2();

Conditionals 8-

WallHuggerWorld

WallHugger peterParker = .setPosition( ); .tick4();

Object Land

Execution Land

.run()

tick4() invokes tick2()

this

WHW

WHW

WHW

color red

brushDown true

WallHugger WH

heading EAST

position

WH

peterParker WH

WH

WH

.tick4()

this

WH

WH

tick2(); tick2();

tick(); tick();

.tick2()

this

WH

WH

Conditionals 8-

WallHuggerWorld

WallHugger peterParker = .setPosition( ); .tick4();

Object Land

Execution Land

.run()

The boolean expression is evaluated

this

WHW

WHW

WHW

color red

brushDown true

WallHugger WH

heading EAST

position

WH

peterParker WH

WH

WH

.tick4()

this

WH

WH

tick2(); tick2();

tick(); tick();

.tick2()

this

WH

WH

followWall();

.tick()

this WH

WH

.followWall()

this

if (false) { left(); } else { forward(); }

WH

WH

Conditionals 8-

WallHuggerWorld

WallHugger peterParker = .setPosition( ); .tick4();

Object Land

Execution Land

.run()

The “else” clause is executed

this

WHW

WHW

WHW

color red

brushDown true

WallHugger WH

heading EAST

position

WH

peterParker WH

WH

WH

.tick4()

this

WH

WH

tick2(); tick2();

tick(); tick();

.tick2()

this

WH

WH

followWall();

.tick()

this WH

WH

.followWall()

this

if (false) { left(); } else { forward(); }

WH

WH

Conditionals 8-

WallHuggerWorld

WallHugger peterParker = .setPosition( ); .tick4();

Object Land

Execution Land

.run()

And spidey moves forward

this

WHW

WHW

WHW

color red

brushDown true

WallHugger WH

heading EAST

position

WH

peterParker WH

WH

WH

.tick4()

this

WH

WH

tick2(); tick2();

tick(); tick();

.tick2()

this

WH

WH

followWall();

.tick()

this WH

WH

.followWall()

this

if (false) { left(); } else { forward(); }

WH

WH

Conditionals 8-

WallHuggerWorld

WallHugger peterParker = .setPosition( ); .tick4();

Object Land

Execution Land

.run()

followWall() goes away

this

WHW

WHW

WHW

color red

brushDown true

WallHugger WH

heading EAST

position

WH

peterParker WH

WH

WH

.tick4()

this

WH

WH

tick2(); tick2();

tick(); tick();

.tick2()

this

WH

WH

followWall();

.tick()

this WH

WH

Conditionals 8-

WallHuggerWorld

WallHugger peterParker = .setPosition( ); .tick4();

Object Land

Execution Land

.run()

And so does another followWall()

this

WHW

WHW

WHW

color red

brushDown true

WallHugger WH

heading EAST

position

WH

peterParker WH

WH

WH

.tick4()

this

WH

WH

tick2(); tick2();

tick(); tick();

.tick2()

this

WH

WH

followWall();

.tick()

this WH

WH

.followWall()

this

if (isFacingWall()) { left(); } else { forward(); }

WH

WH

Conditionals 8-

WallHuggerWorld

WallHugger peterParker = .setPosition( ); .tick4();

Object Land

Execution Land

.run()

The “if” clause is executed

this

WHW

WHW

WHW

color red

brushDown true

WallHugger WH

heading NORTH

position

WH

peterParker WH

WH

WH

.tick4()

this

WH

WH

tick2(); tick2();

tick(); tick();

.tick2()

this

WH

WH

followWall();

.tick()

this WH

WH

.followWall()

this

if (true) { left(); } else { forward(); }

WH

WH

Conditionals 8-

WallHuggerWorld

WallHugger peterParker = .setPosition( ); .tick4();

Object Land

Execution Land

.run()

Until eventually, tick4() finishes execution

this

WHW

WHW

WHW

color red

brushDown true

WallHugger WH

heading NORTH

position

WH

peterParker WH

WH

WH

Conditionals 8-

Bagels, bagels everywhere

BuggleWorld

RandomBagel World

WallHugger World

public class HungryWallHuggerWorld

extends RandomBagelWorld {

public void run () { HungryWallHugger peterParker = new HungryWallHugger(); peterParker.setPosition(new Location(5,3)); peterParker.tick128(); }

}

Conditionals 8-

Put another way*

class HungryWallHugger extends TickBuggle { // If over a bagel, eat it. // Otherwise, turn left if // facing a wall, or move // forward if not facing a wall. public void followWall() {

if (isOverBagel()) { pickupBagel(); } else if (isFacingWall()) { left(); } else { forward(); } } }

*Multi-way conditionals.

Conditionals 8-

Multi-way conditional idiom

true false

test1()

true false

test2()

test3()

false

. . .

. . . . . .

true

. . .

if (test1)) {

} else if (test2) {

} else if (test3) {

} else {

Conditionals 8-

Eat and run

left();

isFacingWall()

forward();

true false

isOverBagel()

true false

pickUpBagel();

// If over a bagel, eat it.

// Also, turn left if facing

// a wall, or move forward

// if not facing a wall.

public void eatAndRun() {

if (isOverbagel()) {

pickupBagel();

} else {

} // do nothing

if (isFacingWall()) {

left();

} else {

forward();

Conditionals 8-

It’s okay to leave off the “else”

left();

isFacingWall()

forward();

true false

isOverBagel()

true false

pickUpBagel();

// If over a bagel, eat it.

// Also, turn left if facing

// a wall, or move forward

// if not facing a wall.

public void eatAndRun() {

if (isOverbagel()) {

pickupBagel();

if (isFacingWall()) {

left();

} else {

forward();