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

21
DAY 3 COMPUTER SCIENCE 12 Dependent and Independent If-Else Structures & Modular Programming through Procedures

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

Page 1: DAY 3 COMPUTER SCIENCE 12 Dependent and Independent If-Else Structures & Modular Programming through Procedures.

DAY 3COMPUTER SCIENCE 12

Dependent and Independent If-Else Structures&

Modular Programming through Procedures

Page 2: DAY 3 COMPUTER SCIENCE 12 Dependent and Independent If-Else Structures & Modular Programming through Procedures.

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

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

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}

Page 4: DAY 3 COMPUTER SCIENCE 12 Dependent and Independent If-Else Structures & Modular Programming through Procedures.

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.

Page 5: DAY 3 COMPUTER SCIENCE 12 Dependent and Independent If-Else Structures & Modular Programming through Procedures.

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

Page 6: DAY 3 COMPUTER SCIENCE 12 Dependent and Independent If-Else Structures & Modular Programming through Procedures.

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)}

Page 7: DAY 3 COMPUTER SCIENCE 12 Dependent and Independent If-Else Structures & Modular Programming through Procedures.

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.

Page 8: DAY 3 COMPUTER SCIENCE 12 Dependent and Independent If-Else Structures & Modular Programming through Procedures.

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.

Page 9: DAY 3 COMPUTER SCIENCE 12 Dependent and Independent If-Else Structures & Modular Programming through Procedures.

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

Page 10: DAY 3 COMPUTER SCIENCE 12 Dependent and Independent If-Else Structures & Modular Programming through Procedures.

• 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()}

Page 11: DAY 3 COMPUTER SCIENCE 12 Dependent and Independent If-Else Structures & Modular Programming through Procedures.

• 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()}

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

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.

Page 13: DAY 3 COMPUTER SCIENCE 12 Dependent and Independent If-Else Structures & Modular Programming through Procedures.

Practicing with Procedures -

Page 14: DAY 3 COMPUTER SCIENCE 12 Dependent and Independent If-Else Structures & Modular Programming through 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:

Page 15: DAY 3 COMPUTER SCIENCE 12 Dependent and Independent If-Else Structures & Modular Programming through 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:

spin(20)

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

Page 16: DAY 3 COMPUTER SCIENCE 12 Dependent and Independent If-Else Structures & Modular Programming through 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:

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

Page 17: DAY 3 COMPUTER SCIENCE 12 Dependent and Independent If-Else Structures & Modular Programming through 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:

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

Page 18: DAY 3 COMPUTER SCIENCE 12 Dependent and Independent If-Else Structures & Modular Programming through Procedures.

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() }}

Page 19: DAY 3 COMPUTER SCIENCE 12 Dependent and Independent If-Else Structures & Modular Programming through Procedures.

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

and use white paint

Page 20: DAY 3 COMPUTER SCIENCE 12 Dependent and Independent If-Else Structures & Modular Programming through Procedures.

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 }

Page 21: DAY 3 COMPUTER SCIENCE 12 Dependent and Independent If-Else Structures & Modular Programming through Procedures.

Complete last question on Wiki with procedures• Exercise 5