You will, however, see many programm ers (in cluding me) consistently write curly
brackets a round statements subordinate to contro l statements like if. It makes your
code more transparent and less prone to obscure errors, especially when you nest one
control statement into the other.
The above form of the if statement does nothing if expression evaluates to fa lse.
If this is not what you want, you can use the augmented form of the statement with
the else part:
if (expression ) statement_true
else statement_false
This form of the if statement will again evaluate expression an d only execute
statement_true if expression evaluates to tru e. If expression evaluates to
false, however, it will execute statement_false. For example, if you want to as-
sign a string either "odd" or "even" to the variable msg depending on the value of x,
you can write:
if (x % 2 == 1) {
msg = "odd";
}
else {
msg = "even";
}
The code can be represented by the following flowchart.
msg = "even"
x % 2 == 1
false
true
msg = "odd"
Maria: OK, that’s the same as the conditional ope rator.
Professor: Not quite. Th e logic is the same but there are diere nces. First, the
conditional operator returns a value while the if/else statement doesn’t. That’s
because statements don’t return values. As a consequence, statement_true and
statement_false are ac tually statements that should have side eects. Second, be-
cause the conditional operator will operate on expressions and not statements, you
cannot conditionally execute c ontrol statements with it. In short, the conditional oper-
ator can be viewed as a condensed form of the if/else statement.
7.2. Statements 13 3
Truthy and Falsy Values
Professor: The first line of our la st example could also be written shorter, without a
compariso n but with the same result:
if (x % 2)
You see, JavaScript is quite flexible when it co mes to data types. If it doesn’t like
the type , it implicitly converts it so as to match the requir ement. For example, the
expression x % 2 doesn’t retur n eithe r true or false so, technically, we cannot
use it a s a branching condition, which requ ires a Boolean value. However, know-
ing th at JavaScript implicitly converts zero to false and one to tr ue when it re quires
a Boolean value, we can drop the comparison part of the expression in this particular
case.
But zero is not the only value th at converts to false when a Boolean value is expected.
Altogether, there are five such values:
0
undefined
null
NaN
"" //A string without any character
All these five values work as false, and are, together with the f alse value itself,
called falsy values. All the other values are called truthy values. You have to be
careful, though, when leaving out explicit comp arisons, because results are not always
identical as was the case in our last example. For instan ce, this is how you can test
whether x has been given a value:
if (x != null) ...
In this example, the body of the if statement will be executed only if x is n ot n ull or
undefined. In th e following example, however, the b ody will be executed if x is not
false, or any falsy value, for that matter:
if (x) ...
It is really important that you k now exactly what types of values you expect in your
progr am and what you want to a chieve. For clarity, it is almost always better to use
explicit comparisons, although they may not be required.
while Loop
Professor: Another type of branching is going back upon the same statement again
to repeat portions of your code. A control statement that allows such branching is
called a loop, and one repetition of a statement or statements within a loop is called
134 Meeting 7. Controlling Program Flow
an iteration. JavaScript’s funda mental loop is the while statement, which has the
following sy ntax:
while (expression ) statement
If expression is falsy, then statement (also called the loop’s body) is skipped and
the while loop is terminated. T he execution continues with the code immediately
following the while loop . Alternatively, if expression evaluates to a truthy value,
then statement is executed and expression is evaluated again. That r epeats until,
eventually, expression returns a falsy value. Note that statement should leave a
side eect such that it influences the value of expression , or else the loop will never
end if expression starts o truthy.
As an example, let’s multiply two non-negative integer variables x and y by adding x
copies of y together. This is the code:
while (x-- > 0) {
product += y;
}
The result of the multiplication is stored in product. Note that product should be
initially set to zero in ord er for the code to work properly.
The flowchart of the while lo op from the above example looks as follows.
product += y
x-- > 0
false
true
Recall that the dec rement operator (- -) retu rns the old value of a variable when placed
behind it. Therefore , when x is one , it is decremented to z ero, but the old value—
which is one—is used in comp arison and the bo dy is executed once more.
This example was fairly trivial but I suggest you nevertheless try it a t home. You
should add variable declarations and initialize them to proper values. You can even
let the user enter both multiplicands. Also try to figure out what wou ld happen if
product wasn’t initialized, and then run the code to see whether your prediction was
7.2. Statements 13 5
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.113.197