Asking computer to
select different operations under different conditions
How to do selections?
True or False?
The only test in computer Boolean Test
COMP 1005 Final Exam
The if statement
Study at home
, then
study at home
The if statement
condition can only be
evaluated as either TRUE
or FALSE
if (condition)
{
}
10
11
Meaning
>
greater than
<
less than
>=
<=
!=
not equals
==
equals
12
The if statement
if (condition)
{
}
13
14
15
16
17
18
19
20
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
How to do selections?
23
24
, then
Otherwise, then
study at home
}else
{
go out for fun
}
25
if (condition)
{
26
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
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
2. Get a random
number inside setup()
& store it in the variable
29
if (condition)
{
30
31
Case 1:
the random number
coin is smaller than 1
e.g. 0.788
32
Case 2:
the random number
coin is greater than 1
e.g. 1.544
33
How to do selections?
34
35
More Actions
Lets throw a dice at the start
of the program to decide the
robots direction to move
36
if (condition 1)
{
}else if (condition 2)
{
}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
{
}
38
Case 1:
the random number
coin is smaller than 1
e.g. 0.788
39
Case 2:
the random number
coin is between 1 and 2
e.g. 1.544
40
Case 3:
the random number
coin is between 2 and 3
e.g. 2.333
41
Case 4:
the random number
coin is greater than 3
e.g. 3.567
42
43
44
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
Information provided
key
keyCode
47
Supplementary:
Getting a random number
Instruction
Result
Example
random(low, high);
random(0, 2);
gives you a
random float
number between 0
and 2, but not
including 2
int( random(0, 2) );
integer
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)
{
}
if (condition 1)
{
}else if (condition 2)
{
}else if (condition 3)
{
operations to do if condition 3 is TRUE
if (condition)
{
}else if (condition n)
{
operations to do if condition n is TRUE
}else
{
}
51
Summary
Writing conditions in computer language
Relational / Equality
Operators
Meaning
>
greater than
<
less than
>=
<=
!=
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/