DAY 3 COMPUTER SCIENCE 12 Dependent and Independent If-Else Structures & Modular Programming...

Preview:

Citation preview

DAY 3COMPUTER SCIENCE 12

Dependent and Independent If-Else Structures&

Modular Programming through Procedures

Exercise 1) What will thy robot do based on the code below?

Exercise 1) What will thy robot do based on the code below?

if (leftIsObstacle and frontIsObstacle){ right() forward(1)}else if (frontIsBeacon){ pickUp

}else if (rightIsObstacle and frontIsClear){ forward(1)}else{ left left}

Exercise 1) What will thy robot do based on the code below?

if (leftIsObstacle and frontIsObstacle){ right() forward(1)}else if (frontIsBeacon){ pickUp

}else if (rightIsObstacle and frontIsClear){ forward(1)}else{ left left}

Note:

As soon as you see conditions that follow the structure of

(if, else if) or (if, else if, else) in order, it is a dependent chain of options offered

starting at the top. As the computer reads from top to bottom, as soon a condition is

satisfied, the condition’s statement is executed and the computer leaves the

conditional structure.

Exercise 2) What will thy robot do based on the code below?

Exercise 2) What will thy robot do based on the code below?

if (frontIsWhite){ forward(1)}else { right right}if (frontIsWhite){ forward(1) right}if (frontIsBlack){ forward(1)}

Exercise 2) What will thy robot do based on the code below?

if (frontIsWhite){ forward(1)}else if (not frontIsWhite){ right right}if (frontIsWhite){ forward(1) right}if (frontIsBlack){ forward(1)}

Note:

When you see just if by itself used again, that means it’s a separate or

independent conditional structure and will be examined by the program. All

independent if structures are subject to execution if their conditions are met.

Exercise 1

• Make your robot arrive in any corner.• Write a code involving

an (if, else if, else) structure in which the third of five options is satisfied and executed.

Exercise 2

• Start from scratch (erase all code)• Make your robot

arrive in front of a beacon• Write a code involving

independent conditionals such that two of three them are executed. Hint on next slide.

• Hint: To make two of three conditional execute, remove the else and just have if and else if. For example:

• Hint: To make two of three conditional execute, remove the else and just have if and else if. For example:

if (frontIsBeacon){ pickup()}

if (frontIsObstacle){ left()}else if (frontIsWhite){ putDown()}

if (leftIsClear){ putDown()}

• Hint: To make two of three conditional execute, remove the else and just have if and else if. For example:

if (frontIsBeacon){ pickup()}

if (frontIsObstacle){ left()}else if (frontIsWhite){ putDown()}

if (leftIsClear){ putDown()}

Modular Programming – Using Procedures

• Some chaps use the word “modules”, other chaps use the word “procedures”, others call them “methods”

• These words mean the same thing - that programs are written in small chunks to avoid getting too long and complicated.

Practicing with Procedures -

Practicing with Procedures - • Erase your code and start from scratch• To write procedures, you give the procedure a name (i.e., spin) and

you define what it does, and then you call upon it anywhere in the code. • Write the following code and let’s see what happens:

Practicing with Procedures - • Erase your code and start from scratch• To write procedures, you give the procedure a name (i.e., spin) and

you define what it does, and then you call upon it anywhere in the code. • Write the following code and let’s see what happens:

spin(20)

procedure spin(n){ repeat(n) { left() }}

Practicing with Procedures - • Erase your code and start from scratch• To write procedures, you give the procedure a name (i.e., spin) and

you define what it does, and then you call upon it anywhere in the code. • Write the following code and let’s see what happens:

spin(20)

procedure spin(n){ repeat(n) { left() }}

It can take many arguments like (n1, n2, n3), but here I’ve only given it one: n

Practicing with Procedures - • Erase your code and start from scratch• To write procedures, you give the procedure a name (i.e., spin) and

you define what it does, and then you call upon it anywhere in the code. • Write the following code and let’s see what happens:

spin(20)

procedure spin(n){ repeat(n) { left() }}

It can take many arguments like (n1, n2, n3), but here I’ve only given it one: n

It’s no different than functions

f (x) = 2x+1f(3) = 7

Practicing with Procedures

• Let’s revise the code we just wrote.

• You can call upon procedures inside procedures. • Write the following code and

let’s see what happens.

spin(5, 3)

procedure spin(clockwise, counterClockwise){ repeat(clockwise) { right() } grab(2) repeat(counterClockwise) { left() } grab(3)}

procedure grab(n){ repeat(n) { pickUp() }}

Exercise 2 with Procedures• # define how to draw a rectangle with two arguments: width, height

and use white paint

Exercise 2 with Procedures• # define how to draw a rectangle with two arguments: width, height

procedure rectangle(width, height) { paintWhite repeat(2) { forward(height) right() forward(width) right } stopPainting }

Complete last question on Wiki with procedures• Exercise 5

Recommended