HTML and CSS Reference
In-Depth Information
In this section, you've learned
how to do calculations and com-
parisons and store the results in
variables. That is fundamental to
writing programs but not very
useful by itself. You need to be able to take different actions depending
on the results, or perform actions repeatedly to make it worth your
while to write a program in the first place.
In programming, we call
choosing between
different actions
branching and repeating
actions looping . You'll
learn about them in the
next section.
Branching and looping
If your program did some calculations and always produced the same
output, there wouldn't be a point to it. A program can't do much unless
it can make decisions based on the variables being passed into it. When
a program executes one block of code rather than another based on the
value of a variable, that's what we call branching . Looping is a related
concept: executing a block of code multiple times. In this section, you'll
step back from the console for a few
pages and learn about the various
branching and looping concepts in
JavaScript so that in the following
sections you can see how they're used.
The term branching , unsurprisingly, comes from an analogy to a tree
branch. Imagine you're walking along the branch of a tree: eventually
you come to a point where it divides into two. You can choose to go up
one branch or the other one. That's all branching is in programming—
choosing to go one way or another. The simplest branching construct is
the if statement.
A block of code
is one or more
within braces
like these: { } .
A condition that
evaluates to a
Do this if the
condition is true.
if (x % 2 == 0) {
} else {
Do this if the
condition is false.
The else is optional.
remove everything after the bracket
if no action is required.
Search WWH ::

Custom Search