Anda di halaman 1dari 53

Lecture 4 Selections

Asking computer to
select different operations under different conditions

What youll learn today?


What are conditions?

How to do selections?

Try asking computer to select different operations


under different conditions

How would you like the final exam to be?

True or False?
The only test in computer Boolean Test
COMP 1005 Final Exam

Do something under a certain condition


Study at home

The if statement
Study at home

, then

if (I have EXAM tomorrow)


{
}

study at home

The if statement
condition can only be
evaluated as either TRUE
or FALSE

if (condition)
{
}

the operations that you want


to do if condition is TRUE

When the condition is


TRUE , the instruction(s)
inside the block will
execute
When the condition is
FALSE, the instruction(s)
inside the block will
NOT execute

TELL THE COMPUTER TO


DO SOME OPERATIONS
UNDER A CERTAIN CONDITION
The if statement

Recap: robot moves upward

When to move the robot upward?

10

How to write conditions in computer language?


Which variable in the program
remembers the robots vertical position?
What does below the upper edge
mean?

11

How to write conditions in computer language?

Relational / Equality Operators


Relational / Equality
Operators

Meaning

>

greater than

<

less than

>=

greater than or equal to

<=

less than or equal to

!=

not equals

==

equals

12

The if statement

if (condition)
{
}

the operations that you want


to do if condition is TRUE

13

Lets try using the if statement


Modify the source program
robotMove.pde so that the
robot move upward only if it is
below the upper edge
Which line moves the robot?
Where to add the if statement?
if (condition)

the operations that


you want to do if
condition is TRUE

14

How does the if statement work?

15

How does the if statement work?

16

How does the if statement work?

17

How does the if statement work?

18

How does the if statement work?

19

How does the if statement work?

20

How does the if statement work?

21

Class Exercise
How to make the robot move in reverse direction
when it reach the top of the window?
Hint: need 1 more variable to remember the direction
Remember the 4 steps to use a variable?

22

What youll learn today?


What are conditions?

How to do selections?

Try asking computer to select different operations


under different conditions

23

Selecting between two actions


Study at home

Go out for fun

24

The if-else statement


Study at home

, then
Otherwise, then

Go out for fun

if (I have EXAM tomorrow)


{

study at home
}else
{
go out for fun
}
25

The if-else statement


condition can only be
evaluated as either TRUE
or FALSE

if (condition)
{

the operations that you want


to do if condition is TRUE
}else
{
the operations that you want
to do if condition is FALSE
}

When the condition is


TRUE , the instruction(s)
inside the if-block will
execute
When the condition is
FALSE, the instruction(s)
inside the else-block will
execute

26

TELL THE COMPUTER TO


SELECT BETWEEN TWO ACTIONS
The if-else statement

27

Two Actions
Lets put the robot in the middle of the window initially and
flip a coin at the start of the program to decide the robots
direction to move

28

How to flip a coin


We can get a random number by

random(low, high);
which will give you a random float
number between low and high,
including low but NOT including high

1. Add a variable
of type float

for remembering the


random number

2. Get a random
number inside setup()
& store it in the variable

29

Selecting between two actions


The if-else statement

if (condition)
{

the operations that you want


to do if condition is TRUE
}else
{
the operations that you want
to do if condition is FALSE
}

30

Lets try using the if-else statement

31

How does the if-else statement work?

Case 1:
the random number
coin is smaller than 1
e.g. 0.788

32

How does the if-else statement work?

Case 2:
the random number
coin is greater than 1
e.g. 1.544

33

What youll learn today?


What are conditions?

How to do selections?

Try asking computer to select different operations


under different conditions

34

TELL THE COMPUTER TO


SELECT BETWEEN MORE ACTIONS
The if-else-if statement

35

More Actions
Lets throw a dice at the start
of the program to decide the
robots direction to move

36

Selecting between more actions


The if-else-if statement
Get a random number
between 0 and 4

if (condition 1)
{

operations to do if condition 1 is TRUE

}else if (condition 2)
{

operations to do if condition 2 is TRUE

}else if (condition 3)
{
operations to do if condition 3 is TRUE

}else if (condition n)
{
operations to do if condition n is TRUE

}else
{
}

operations to do if ALL the above


conditions are FALSE
37

Lets try using the if-else-if statement

38

How does the if-else-if statement work?

Case 1:
the random number
coin is smaller than 1
e.g. 0.788

39

How does the if-else-if statement work?

Case 2:
the random number
coin is between 1 and 2
e.g. 1.544

40

How does the if-else-if statement work?

Case 3:
the random number
coin is between 2 and 3
e.g. 2.333

41

How does the if-else-if statement work?

Case 4:
the random number
coin is greater than 3
e.g. 3.567

42

What is the output of the following program?


Suppose the random number
coin is 1.35, in which direction
will the robot move?

43

What is the output of the following program?


Suppose the random number
coin is 1.35, in which direction
will the robot move?

44

Compare the two programs


(Suppose the random number coin is 1.35)

45

Class Exercise
Move the robot along the edge of the window
Hints:
Need to remember the direction
Move the robot according to the direction
Change the direction according to the position

2
3
46

Challenge & Bonus


1. Change direction of the robot when user presses
the UP / DOWN / LEFT / RIGHT arrow keys
(robot moves automatically in this case)
2. Move robot when user presses the arrow keys
(robot will not move if user does not press any key)
Hint: check the keyCode system variable
System Variable

Information provided

key

The most recent key pressed on the keyboard

keyCode

The most recent special key pressed,

e.g. ENTER, ESC, DELETE, BACKSPACE, UP, DOWN, LEFT, RIGHT,

47

Supplementary:
Getting a random number
Instruction

Result

Example

random(low, high);

Gives you a random


float number
between low and high,
including low but NOT
including high

random(0, 2);
gives you a
random float
number between 0
and 2, but not
including 2

int( random(low, high) );

Gives you a random

int( random(0, 2) );

integer

between low and high,


including low but NOT
including high

gives you two


possible integers
0 or 1

48

Supplementary:
Using a random integer

49

Be careful of = and ==
= is used for assigning value to a variable
e.g. x = 5 is to use variable x to remember the value 5
== is used for equality testing
e.g. x == 5 is to test whether the value of variable x
is equal to 5 or not

50

Summary
To do selections
if (condition)
{
}

the operations that you want


to do if condition is TRUE

if (condition 1)
{

operations to do if condition 1 is TRUE

}else if (condition 2)
{

operations to do if condition 2 is TRUE

}else if (condition 3)
{
operations to do if condition 3 is TRUE

if (condition)
{

the operations that you want


to do if condition is TRUE
}else
{
the operations that you want
to do if condition is FALSE
}

}else if (condition n)
{
operations to do if condition n is TRUE

}else
{
}

operations to do if ALL the above


conditions are FALSE

51

Summary
Writing conditions in computer language
Relational / Equality
Operators

Meaning

>

greater than

<

less than

>=

greater than or equal to

<=

less than or equal to

!=

not equals

==

equals

52

References
Learning Processing: A Beginner's Guide to Programming
Images, Animation, and Interaction
Daniel Shiffman.
Published August 2008, Morgan Kaufmann.
Getting Started with Processing
Casey Reas and Ben Fry.
Published June 2010, O'Reilly Media.
Web: http://processing.org/

Anda mungkin juga menyukai