Java Reference
In-Depth Information
START
STEP 1: (initialization)
facProd = number
curFactor = number - 1
STEP 2: (processing)
facProd = facProd * curFactor
curFactor = curFactor - 1
STEP 3:
is
curFactor > 1
?
YES
NO
factorial = facProd
END
Figure 10-1 Factorial Flowchart
Programmers note:
In the factorial calculation we test for a factor greater than 1 to termi-
natetheloop.Thiseliminatesmultiplyingby1,whichisatrivialopera-
tion.
For Loop
TheforloopisthesimplestiterativeconstructofJava.Theforlooprepeats
theexecutionofaprogramstatementorstatementblockafixednumberof
times. A typical for loop consists of the following steps:
1. An initialization step that assigns an initial value to the loop variable.
2. Oneormoreprocessingstatements.Itisinthisstepwherethecalculations
take place and the loop variable is updated.
3. A test expression that determines the termination of the loop.
 
Search WWH ::




Custom Search