Conditions - Watchung Hills Regional High School / Overview

Download Report

Transcript Conditions - Watchung Hills Regional High School / Overview

1
We’ve learned that our programs are
read by the compiler in order, from top
to bottom, just as they are written
The order of statement execution is
called the flow of control
Today we will learn how to change this
so our programs can branch in different
directions
2
A
conditional statement lets us choose
which statement will be executed next
(branching)
 Conditional
statements give us the
power to make basic decisions
 We
will use ActionScript’s conditional
statements if, if else, else
3
The if statement has the following syntax:
if is an
ActionScript
reserved word
The condition must be a Boolean
expression.
It must evaluate to either true or false.
if ( condition ) {
statement;
}
If the condition is true, the statement is
executed.
If it is false, the statement is skipped.
4
if (score > 10) {
trace(“winner");
}
score > 10
false
true
trace(“winner”)
5
 Our
conditions will
be written using
Boolean expressions
such as (score > 10)
 Do
you know your
relational and
logical operators?
What do these
mean?
<
• Less than
>
• Greater than
<=
• Less than or
equal to
>=
• Greater than
or equal to
==
• Equal to
!=
• Not equal to
6
if (age >= 18) {
trace(“adult");
}
else {
trace(“kid");
}
false
age >=18
true
trace(“adult”)
Note: One or the other will be executed, but not both.
trace(“kid”)
7
 You
can test more than one condition at a
time by using an if and then else if
This
condition is
only tested
if x>20 is
false
This only runs
if the earlier
tests both
were false
if (x > 20) {
trace("x is > 20");
}
else if (x < 0) {
trace("x is negative");
}
else {
trace(“x<=20 and x>=0”);
}
8