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

JHTP小结_第四章_控制语句Part 1-赋值、++、--运算符

2016-06-17 10:25 627 查看


Summary

Section 4.1Introduction

• Before writing aprogram to solve a problem, you must have a thorough understanding of the problemand a carefully planned approach to solving it. You must
also understand thebuilding blocks that are available and employ proven program-constructiontechniques.

Section 4.2Algorithms

• Any computing problemcan be solved by executing a series of actions (p. 102) in a specific order.

• A procedure for solvinga problem in terms of the actions to execute and the order in which they executeis called an algorithm (p. 102).

• Specifying the order inwhich statements execute in a program is called program control (p. 102).

Section 4.3Pseudocode

• Pseudocode (p. 103) isan informal language that helps you develop algorithms without having to worryabout the strict details of Java language syntax.

• The pseudocode we usein this book is similar to everyday English—it’s convenient and user friendly,but it’s not an actual computer programming language.
You may, of course, useyour own native language(s) to develop your own pseudocode.

• Pseudocode helps you “thinkout” a program before attempting to write it in a programming language, such asJava.

• Carefully preparedpseudocode can easily be converted to a corresponding Java program.

Section 4.4Control Structures

• Normally, statements ina program are executed one after the other in the order in which they’re written.This process is called sequential execution (p.
103).

• Various Java statementsenable you to specify that the next statement to execute is not necessarily thenext one in sequence. This is called transfer of control
(p. 103).

• Bohm and Jacopinidemonstrated that all programs could be written in terms of only three control structures(p. 103)—the sequence structure, the selection
structure and the repetitionstructure.

• The term “controlstructures” comes from the field of computer science. The
Java LanguageSpecification
refers to “control structures” as “control statements” (p. 104).

• The sequence structureis built into Java. Unless directed otherwise, the computer executes Javastatements one after the other in the order in which they’re
written—that is,in sequence.

• Anywhere a singleaction may be placed, several actions may be placed in sequence.

• Activity diagrams (p.104) are part of the UML. An activity diagram models the workflow (p. 104; alsocalled the activity) of a portion of a software system.

• Activity diagrams arecomposed of symbols (p. 104)—such as action-state symbols, diamonds and smallcircles—that are connected by transition arrows, which
represent the flow ofthe activity.

• Action states (p. 104)contain action expressions that specify particular actions to perform.

• The arrows in anactivity diagram represent transitions, which indicate the order in which the actionsrepresented by the action states occur.

• The solid circlelocated at the top of an activity diagram represents the activity’s initialstate (p. 104)—the beginning of the workflow before the program
performs themodeled actions.

• The solid circlesurrounded by a hollow circle that appears at the bottom of the diagramrepresents the final state (p. 104)—the end of the workflow after
the programperforms its actions.

• Rectangles with theirupper-right corners folded over are UML notes (p. 104)—explanatory remarks

that describe the purposeof symbols in the diagram.

• Java has three types ofselection statements (p. 105).

• The
if
single-selectionstatement (p. 105) selects or ignores one or more actions.

• The
if…else
double-selectionstatement selects between two actions or groups of actions.

• The
switch
statement iscalled a multiple-selection statement (p. 105) because it selects among manydifferent actions or groups of actions.

• Java provides the
while,
do…while
and
for
repetition(also called iteration or looping) statements that enable programs to performstatements repeatedly as long as a loop-continuation condition remains true.

• The
while
and
for
statementsperform the action(s) in their bodies zero or more times—if the loop-continuationcondition (p. 105) is initially false, the action(s) will not execute.
The do…while
statementperforms the action(s) in its body one or more times.

• The words
if,
else,
switch,
while,
do
and
for
are Javakeywords. Keywords cannot be used as identifiers, such as variable names.

• Every program is formedby combining as many sequence, selection and repetition statements (p. 105) asis appropriate for the algorithm the program implements.

•Single-entry/single-exit control statements (p. 105) are attached to oneanother by connecting the exit point of one to the entry point of the next.This is
known as control-statement stacking.

• A control statement mayalso be nested (p. 105) inside another control statement.

Section 4.5
if
Single-SelectionStatement


• Programs use selectionstatements to choose among alternative courses of action.

• The single-selection
if
statement’sactivity diagram contains the diamond symbol, which indicates that a decisionis to be made. The workflow follows a path determined by the symbol’sassociated
guard conditions (p. 106). If a guard condition is true, theworkflow enters the action state to which the corresponding transition arrowpoints.

• The
if
statement isa single-entry/single-exit control statement.

Section 4.6
if…elseDouble-Selection
Statement


• The
if
single-selectionstatement performs an indicated action only when the condition is
true.

• The
if…else
double-selection(p. 105) statement performs one action when the condition is true and anotheraction when the condition is false.

• A program can testmultiple cases with nested
if…else
statements(p. 107).

• The conditionaloperator (p. 110;
?:)
is Java’sonly ternary operator—it takes three operands. Together, the operands and the
?:
symbol form aconditional expression (p. 110).

• The Java compilerassociates an
else
with the immediately preceding
if
unless toldto do otherwise by the placement of braces.

• The
if
statementexpects one statement in its body. To include several statements in the body ofan
if
(or the body of an
else
for an
if…else
statement),enclose the statements in braces.

• A block (p. 110) ofstatements can be placed anywhere that a single statement can be placed.

• A logic error (p. 110)has its effect at execution time. A fatal logic error (p. 110) causes a programto fail and terminate prematurely. A nonfatal logic
error (p. 110) allows aprogram to continue executing, but causes it to produce incorrect results.

• Just as a block can beplaced anywhere a single statement can be placed, you can also use an empty statement,represented by placing a semicolon (;)
where astatement would normally be.

Section 4.8
whileRepetition Statement


• The
while
repetitionstatement (p. 114) allows you to specify that a program should repeat an actionwhile some condition remains true.

• The UML’s merge (p.114) symbol joins two flows of activity into one.

• The decision and mergesymbols can be distinguished by the number of incoming and outgoing transitionarrows. A decision symbol has one transition arrow pointing
to the diamond andtwo or more transition arrows pointing out from the diamond to indicatepossible transitions from that point. Each transition arrow pointing out of adecision symbol has a guard condition. A merge symbol has two or moretransition arrows pointing
to the diamond and only one transition arrowpointing from the diamond, to indicate multiple activity flows merging tocontinue the activity. None of the transition arrows associated with a mergesymbol has a guard condition.

Section 4.9Formulating Algorithms: Counter-Controlled Repetition

• Counter-controlledrepetition (p. 115) uses a variable called a counter (or control variable) to

control the number oftimes a set of statements execute.

• Counter-controlledrepetition is often called definite repetition (p. 115), because the number of repetitionsis known before the loop begins executing.

• A total (p. 115) is avariable used to accumulate the sum of several values. Variables used to store totalsare normally initialized to zero before being
used in a program.

• A local variable’sdeclaration must appear before the variable is used in that method. A localvariable

cannot be accessedoutside the method in which it’s declared.

• Dividing two integersresults in integer division—the calculation’s fractional part is truncated.

Section 4.10Formulating Algorithms: Sentinel-Controlled Repetition

• In sentinel-controlledrepetition (p. 119), a special value called a sentinel value (also called asignal

value, a dummy value or aflag value) is used to indicate “end of data entry.”

• A sentinel value mustbe chosen that cannot be confused with an acceptable input value.

• Top-down, stepwise refinement(p. 120) is essential to the development of well-structured programs.

• Division by zero is alogic error.

• To perform afloating-point calculation with integer values, cast one of the integers totype
double.

• Java knows how toevaluate only arithmetic expressions in which the operands’ types areidentical.

To ensure this, Javaperforms an operation called promotion on selected operands.

• The unary cast operatoris formed by placing parentheses around the name of a type.

Section 4.12Compound Assignment Operators

• The compound assignmentoperators (p. 131) abbreviate assignment expressions. Statements of

the form
variable
=
variable operatorexpression;

where
operator
is one of thebinary operators
+,
-,
*,
/
or
%, can be written in the form

variable operator=
expression;

• The
+=
operator addsthe value of the expression on the right of the operator to the value of the

variable on the left ofthe operator and stores the result in the variable on the left of the operator.

Section 4.13Increment and Decrement Operators

• The unary incrementoperator,
++, and the unary decrement operator,
--, add 1 to orsubtract 1

from the value of anumeric variable (p. 131).

• An increment ordecrement operator that’s prefixed (p. 131) to a variable is the prefixincrement

or prefix decrementoperator, respectively. An increment or decrement operator that’s postfixed

(p. 131) to a variable isthe postfix increment or postfix decrement operator, respectively.

• Using the prefixincrement or decrement operator to add or subtract 1 is known aspreincrementing

or predecrementing,respectively.

• Preincrementing orpredecrementing a variable causes the variable to be incremented or decremented

by 1; then the new valueof the variable is used in the expression in which it appears.

• Using the postfixincrement or decrement operator to add or subtract 1 is known aspostincrementing

or postdecrementing,respectively.

• Postincrementing orpostdecrementing the variable causes its value to be used in the expression

in which it appears; thenthe variable’s value is incremented or decremented by 1.
• When incrementing ordecrementing a variable in a statement by itself, the prefix and postfix

increment have the sameeffect, and the prefix and postfix decrement have the same effect.

Section 4.14Primitive Types

• Java requires allvariables to have a type. Thus, Java is referred to as a strongly typedlanguage

(p. 134).

• Java uses Unicodecharacters and IEEE 754 floating-point numbers.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: