您的位置:首页 > 编程语言 > PHP开发

JHTP小结_第五章_控制语句Part 2-逻辑运算符

2016-06-21 15:35 429 查看


Chapter 5 Control Statements: Part 2; Logical Operators

Summary

Section 5.2Essentials of Counter-Controlled Repetition

• Counter-controlledrepetition (p. 153) requires a control variable, the initial value of thecontrol variable, the increment by which the control variable
is modified eachtime through the loop (also known as each iteration of the loop) and theloop-continuation condition that determines whether looping should continue.

• You can declare avariable and initialize it in the same statement.

Section 5.3
for
RepetitionStatement


• The
while
statement canbe used to implement any counter-controlled loop.

• The
for
statement (p.155) specifies all the details of counter-controlled repetition in its header

• When the
for
statementbegins executing, its control variable is declared and initialized. If the loop-continuationcondition is initially true, the body executes. After executing
the loop’sbody, the increment expression executes. Then the loop-continuation test isperformed again to determine whether the program should continue with the nextiteration of the loop.

• The general format ofthe
for
statement is
for
(initialization;
loopContinuationCondition;
increment)
statement where the
initialization
expressionnames the loop’s control variable and provides its initial value,
loopContinuationCondition
determineswhether the loop should continue executing and
increment modifies thecontrol variable’s value, so that
the loop-continuation condition eventuallybecomes false. The two semicolons in the
for
header arerequired.

• Most
for
statementscan be represented with equivalent
while
statements asfollows:

initialization;

while
(loopContinuationCondition)

{

statement

increment;

}

• Typically,
for
statementsare used for counter-controlled repetition and
while
statementsfor sentinel-controlled repetition.

• If the
initialization
expression inthe
for
header declares the control variable, the control variable canbe used only in that
for
statement—it will not exist outside the
for
statement.

• The expressions in a
for
header areoptional. If the
loopContinuationCondition
is omitted, Java assumesthat it’s always true, thus creating an infinite loop. You might omit the
initialization
expression ifthe control variable is initialized before the loop. You might omit the
increment
expression ifthe increment is calculated with statements in the loop’s body or if noincrement is needed.

• The incrementexpression in a
for
acts as if it’s a standalone statement at the end of the
for’s body.

• A
for
statement cancount downward by using a negative increment—i.e., a decrement (p. 158).

• If theloop-continuation condition is initially
false, the
for
statement’sbody does not execute.

Section 5.4Examples Using the
for
Statement


• Java treatsfloating-point constants like
1000.0
and
0.05
as type
double. Similarly,Java treats whole-number constants like
7
and
-22
as type
int.

• The format specifier
%4s
outputs a
String
in a fieldwidth (p. 161) of 4—that is,
printf
displays the value withat least 4 character positions. If the value to be output is less than 4character positions wide, the value is right justified (p. 161)
in the field bydefault. If the value is greater than 4 character positions wide, the fieldwidth expands to accommodate the appropriate number of characters. To leftjustify (p. 161) the value, use a negative integer to specify the field width.


Math.pow(x,
y)
(p. 162)calculates the value of
x
raised to the
yth
power. The method receives two
double
arguments andreturns a
double
value.

• The comma (,)
formattingflag (p. 162) in a format specifier indicates that a floating-point value shouldbe output with a grouping separator (p. 162). The actual separator used isspecific to the user’s locale (i.e., country). In the United States, the numberwill have commas
separating every three digits and a decimal point separatingthe fractional part of the number, as in 1,234.45.

• The
.
in a formatspecifier indicates that the integer to its right is the number’s precision.

Section 5.5
do…whileRepetition
Statement


• The
do…while
statement (p.163) is similar to the
while
statement. In the
while, the programtests the loop-continuation condition at the
beginning of the loop, beforeexecuting its body; if the condition is false, the body never executes. The
do…while
statementtests the loop-continuation condition
after
executing theloop’s body; therefore, the body always executes at least once.

Section 5.6
switchMultiple-Selection
Statement


• The
switch
statement (p.165) performs different actions based on the possible values of a constant integralexpression (a constant value of type
byte,
short,
int
or
char, but not
long), or a
String.

• The end-of-fileindicator is a system-dependent keystroke combination that terminates userinput.

On UNIX/Linux/Mac OS Xsystems, end-of-file is entered by typing the sequence
<Ctrl>d
on a line byitself. This notation means to simultaneously press both the
Ctrl
key and the
d
key.

On Windows systems, enterend-of-file by typing
<Ctrl> z.


Scanner
method
hasNext
(p. 168)determines whether there’s more data to input. This method returns the
boolean
value
true
if there’smore data; otherwise, it returns
false. As long asthe end-of-file indicator has not been typed,
method hasNext
will return
true.

• The
switch
statementconsists of a block that contains a sequence of
case
labels (p.168) and an optional
default
case (p. 168).

• In a
switch, the programevaluates the controlling expression and compares
its value with each case
label. If amatch occurs, the program executes the statements for that
case.

• Listing casesconsecutively with no statements between them enables the cases to perform the sameset of statements.

• Every value you wish totest in a
switch
must be listed in a separate
case
label.

• Each
case
can havemultiple statements, and these need not be placed in braces.

• A
case’s statementstypically end with a
break
statement (p. 168) that terminates the
switch’s execution.

• Without
break
statements,each time a match occurs in the
switch, the statements for thatcase and subsequent cases execute
until a break
statement orthe end of the
switch
is encountered.

• If no match occursbetween the controlling expression’s value and a
case
label, theoptional
default
case executes. If no match occurs and the
switch
does notcontain a
default
case, program control simply continues with the first statementafter the
switch.

Section 5.7 Class
AutoPolicyCase Study:
Strings in
switchStatements



Strings can be usedin a
switch
statement’s controlling expression and
case
labels.

Section 5.8
breakand
continue
Statements


• The
break
statement,when executed in a
while,
for,
do…while
or
switch, causes immediate exit from that statement.

• The
continue
statement (p.174), when executed in a
while,
for
or
do…while,
skips the loop’s remaining body statements and proceeds withits next iteration. In
while
and
do…while
statements, the program evaluates the loop-continuation testimmediately. In a
for
statement, the increment expression executes, then the programevaluates the loop-continuation test.

Section 5.9Logical Operators

• Simple conditions areexpressed in terms of the relational operators
>,
<,
>=
and
<=
and theequality operators
==
and
!=, and each expression tests only one condition.

• Logical operators (p.176) enable you to form more complex conditions by combining simple conditions.

The logical operators are&&
(conditional AND),
||
(conditionalOR),
&
(boolean logical

AND),
|
(booleanlogical inclusive OR),
^
(boolean logical exclusive OR) and
!
(logicalNOT).

• To ensure that twoconditions are
both
true, use the
&&
(conditional AND)operator. If either or both of the simple conditions are false, the entireexpression is false.

• To ensure that either
or
both of twoconditions are true, use the
||
(conditional OR)operator, which evaluates to true if either or both of its simple conditionsare true.

Self-Review Exercises

• A condition using
&&or
||
operators (p. 176) uses short-circuit evaluation (p. 178)—they’reevaluated only until it’s known whether the condition is true or false.

• The
&
and
|
operators (p.178) work identically to the
&&
and
||
operators butalways evaluate both operands.

• A simple conditioncontaining the boolean logical exclusive OR (^;
p. 179)operator is true
if and only if one of its operands is
true
and the other is
false. If bothoperands are
true
or both are
false,
the entirecondition is
false. This operator is also guaranteed to evaluate both of itsoperands.

• The unary
!
(logical NOT;p. 179) operator “reverses” the value of a condition.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: