Looping / Iterative statements in Java | ICSE Class 9 Computer Applications Notes
On this page
This note covers repetition, loop control, for, while and do while loops, conversion between loop forms, finite and infinite loops, delay loops, multiple counters, break, continue, tracing and simple Java programs.
What is iteration, and why do programs need loops?
Iteration means repetition. A loop is a control structure that repeats a statement or a group of statements. A control structure determines which statements execute and in what order. The order of execution is called flow of control.
Consider printing the first five natural numbers, the counting numbers beginning with one. Separate printing statements can display them. Extending that approach to the first 100,000 natural numbers requires an unwieldy amount of repeated code. A loop expresses the repeated action and its stopping condition together.
Definition: The loop body is the statement or block of statements that the loop repeats. One execution of this body is one iteration.
How is repetition different from selection?
Selection chooses a path according to a condition, a test that is true or false. Repetition returns to an action while its continuation rule allows it. An if statement can choose whether to perform an action, but does not itself repeat that action.
A variable is a named storage location whose value can change. A loop's control variable helps determine whether repetition continues. A counter is a variable used to track progress by regular changes. These changes let successive iterations work with successive values.
Printing a sequence, adding its terms and finding a product all involve repeated work. A sequence is an ordered collection of values; a term is one value in it. An accumulator stores a running result as values are processed.
Before writing a loop, identify the repeated action, the starting state, the continuation test and the change made after each repetition. This separates the task itself from the instructions needed to control it. Printing a value and increasing the counter are different actions.
What do the program wrappers mean?
In the programs below, class declares a class, a named structure containing members such as methods and variables. Loop is the class name. The method main is a named block of executable statements; void means it returns no value. These examples use a main method invoked on a Loop object, an instance of that class.
A block groups statements between opening and closing braces, { and }. Parentheses, ( and ), enclose a method's parameters or a loop's control information. Empty parentheses after main mean that this method takes no parameters, which are named inputs to a method.
How do initialisation, testing and updating control a loop?
Initialisation gives a variable its starting value. The test condition decides whether the loop continues. An update changes a value involved in controlling the loop. The position of the test distinguishes entry-controlled loops from exit-controlled loops.
An entry-controlled loop tests before executing its body. Java's for and while loops belong to this group. An exit-controlled loop tests after executing its body. Java's do while loop belongs to this group.
Which symbols are needed to read the programs?
| Notation | Meaning in these programs |
|---|---|
| int | The Java integer type used for whole-number variables in these examples. |
| = | Assignment: store the right-hand value in the left-hand variable. |
| < and <= | Less than, and less than or equal to, respectively. |
| > | Greater than: true when the left-hand value exceeds the right-hand value. |
| == | Equality test: compare two values without assigning a new value. |
| ++ and -- | Increment and decrement: increase or decrease an integer variable by one. |
| + and * | Addition and multiplication for the numeric operands used here. |
| ; | A statement terminator; also separates the three parts of a for header. |
| , | A comma separates multiple declarations or updates where Java permits them. |
System.out.println displays its argument and then moves to a new line. An argument is a value supplied when a method is called. The dots select members: out belongs to System, and println is called through out. Each displayed value below therefore occupies a separate line.
Rule: A Java loop condition must have a boolean value, meaning true or false. Assignment and comparison perform different jobs; changing a counter is not itself the same operation as testing a boundary.
The header is the part introducing a loop and its control information. Read the header together with the body. An update can be written in a for header, while the corresponding change in a while program is commonly a statement inside the body.
When tracing, record values in execution order. Do not assume that all text in a header executes immediately from left to right on every visit. Initialisation, testing and updating have distinct positions in the loop's execution cycle.
How does a for loop work?
A for loop places initialisation, condition and update together. It is particularly convenient when a counter follows a clear sequence. The body executes only after the condition has been tested and found true. A false first test skips the body.
Syntax: for (initialisation; condition; update) { statements; }
Here the words initialisation, condition, update and statements describe parts to replace with Java code. They are not a complete program. The two semicolons inside the parentheses separate the control parts; they do not represent statements in the body.
What is the execution order?
- Perform the initialisation once when execution reaches the loop.
- Test the condition. If it is false, continue after the loop.
- If the condition is true, execute the loop body.
- After normal completion of the body, perform the update and return to the test.
Worked example 1. Print the first five natural numbers. The integer variable count is the counter, starting at 1 and ending its printed sequence at 5.
class Loop {
void main() {
for (int count = 1; count <= 5; count++) {
System.out.println(count);
}
}
}Answer: Output, with each value on a separate line:
1
2
3
4
5
- The first line declares the class Loop; its opening brace starts the class block.
- The second line declares the main method; its opening brace starts the method body.
- The for line declares count as an integer, stores 1, tests count <= 5 before each iteration and increases count after each normally completed body.
- The println line displays the current count. It executes with count equal to each integer from 1 through 5.
- The next closing brace ends the loop body. After printing 5, the update makes count 6; the next test is false.
- The final two braces close the method and the class. They produce no output.
The final successful condition allows the final printed value. The update then changes the counter again before the unsuccessful test ends repetition. Consequently, the last value tested need not be the last value displayed. Treat the failed test as part of the trace.
How does a while loop test before repeating?
Property: A while loop tests its condition before executing its body
A while loop tests its condition before each execution of its body. Initialisation normally precedes the loop. The body performs the repeated action and any required update. The condition is then tested again using the changed values.
Syntax: while (condition) { statements; }
A while loop suits a process expressed as “continue while this condition holds”. Its syntax does not supply a separate update field. The programmer must ensure that the intended progress actually occurs, whether through a counter update or another change used by the condition.
How does the same counting task look in while form?
Worked example 2. Print the first five natural numbers using count, an integer counter initialised to 1.
class Loop {
void main() {
int count = 1;
while (count <= 5) {
System.out.println(count);
count++;
}
}
}Answer: Output, with each value on a separate line:
1
2
3
4
5
- The first line declares the class Loop; its opening brace starts the class block.
- The second line declares the main method; its opening brace starts the method body.
- The declaration line creates count and assigns its starting value before the loop is reached.
- The while line tests whether count is at most 5. This test occurs before printing, including on the first visit.
- The println line displays the current counter; the following count++ line increases it by one.
- The loop closing brace leads back to the condition. Once count becomes 6, the body is skipped and the loop finishes.
- The final two braces close the method and the class. They produce no output.
What the figure shows
Testing a while loop
Start leads to an Initialisation Statement and then a Test Expression diamond. The True branch enters Body of while Loop and returns to the test. The False branch leads to Statements following the while loop and then Stop.
See Fig. 6.5 in your NCERT textbook
The return arrow goes to the test, not to initialisation. Reinitialising the counter on every pass would undo its progress. Place a running total outside the loop for the same reason when it must retain results accumulated during previous iterations.
How does a do while loop test after execution?
A do while loop executes the body before testing its continuation condition. Once execution reaches this loop, the body is entered at least once. The test then determines whether another iteration begins. This first-entry behaviour differs from for and while.
Syntax: do { statements; } while (condition);
The semicolon after the final condition is required. The word do introduces the body; the later while supplies its continuation test. Keep the test attached to the preceding do block when reading the program. It does not introduce a separate while loop.
How does the counting sequence stay the same?
Worked example 3. Print the first five natural numbers with a do while loop. The integer counter count begins at 1.
class Loop {
void main() {
int count = 1;
do {
System.out.println(count);
count++;
} while (count <= 5);
}
}Answer: Output, with each value on a separate line:
1
2
3
4
5
- The first line declares the class Loop; its opening brace starts the class block.
- The second line declares the main method; its opening brace starts the method body.
- The declaration creates count and stores 1 before entering the loop.
- The do line starts the body immediately, without testing count first.
- The println line displays count; the count++ line then increases it by one.
- The closing line tests count <= 5 after the update. A true result returns to do; after printing 5, the updated value 6 makes the test false.
- The final two braces close the method and the class. They produce no output.
Draw and label
A do while control path
Draw initialisation leading into a body box, then into a condition diamond. Label a return arrow to the body True. Label the exit arrow False and lead it to the statement following the loop.
Do not choose this form merely because it can reproduce a familiar output. Decide whether the task permits the first action before testing. A task that might require no action at all needs that possibility preserved when its loop is rewritten.
How can loops be converted without changing their behaviour?
Interconversion means rewriting a loop in another form while preserving its intended actions. Matching the printed result for one starting value is useful, but the starting condition and the route taken after updates must also agree.
How are for and while related?
For a basic loop without a jump statement, move the for initialisation before while. Keep the same condition and body, then place the for update at the end of the while body. Worked examples 1 and 2 show these corresponding positions.
| Feature | for | while | do while |
|---|---|---|---|
| Condition position | Before the body | Before the body | After the body |
| First condition false | Body skipped | Body skipped | Body entered before testing |
| Typical initialisation position | Inside the header | Before the loop | Before the loop |
| Typical update position | Inside the header | Inside the body | Inside the body |
| Condition punctuation | Followed by a header semicolon | Followed by a closing parenthesis | Closing parenthesis followed by a semicolon |
When does conversion to do while need a guard?
A guard is an initial test that prevents entering a block when its condition is false. An if statement executes its block when the condition is true. Wrapping a do while loop in an if with the original condition can preserve the possibility of zero iterations.
Rule: For a basic while loop, initialise first, test the original condition with if, and enter do while only if that test succeeds. Keep the original body, update and continuation condition in their corresponding execution order.
A jump statement changes the normal control path. The break and continue statements discussed below require special attention during conversion. A continue in a for loop still leads through its update; a continue in while can skip an update placed later in the body.
Check conversion by comparing the first entry, every update, the last successful test and the exit. Also compare what happens when the initial condition is false. Equivalent loops should preserve which values are processed and when processing stops, not merely share similar-looking conditions.
What are loop variations, multiple counters and delay loops?
A loop variation changes how control information is arranged while retaining the loop's rules. A for loop can use more than one counter. Commas separate declarations of variables of the same type in its initialisation and separate expressions in its update part.
How can two counters advance together?
The operator += adds the right-hand value to the variable on its left and stores the result there. In the next program, count counts repetitions while num holds the term to display. Both are integers; their starting values and updates are shown explicitly.
Worked example 4. Display the sequence 10, 20, 30, 40, 50. Use count from 1 through 5, while num starts at 10 and increases by 10.
class Loop {
void main() {
for (int count = 1, num = 10; count <= 5; count++, num += 10) {
System.out.println(num);
}
}
}Answer: Output, with each value on a separate line:
10
20
30
40
50
- The first line declares the class Loop; its opening brace starts the class block.
- The second line declares the main method; its opening brace starts the method body.
- The for line initialises count to 1 and num to 10. Its single condition tests count, not num.
- After each normally completed body, count++ advances the repetition counter and num += 10 advances the displayed sequence.
- The println line displays num. The loop closing brace ends the body and leads to both updates before the next test.
- The final two braces close the method and the class. They produce no output.
How are finite, infinite and delay loops different?
A finite loop finishes after a finite number of iterations. An infinite loop continues without reaching an exit in its control path. A condition that stays true and an absent effective exit can cause unintended endless repetition.
A delay loop performs repeated work mainly to consume execution time. Its body may be empty, represented by an empty block or a lone semicolon. The counter can still initialise, test and update even though the body performs no useful action.
Note: A delay loop's counter does not by itself specify a duration in seconds. Execution time depends on the execution environment. A finite empty loop can finish without printing anything.
In a for loop, control parts may be omitted, but the two separating semicolons remain. An omitted condition permits repetition without a false condition stopping it; an explicit exit can still end it. Omitting an update is valid syntax but requires careful control logic.
How does break terminate a loop early?
Property: break terminates the current loop
break exits the loop when it executes. Statements later in that loop body are skipped, and execution resumes after the loop. In the simple loops here, break is placed inside an if statement so that reaching a particular value triggers the exit.
Rule: An ordinary, unlabelled break, written without a following label name, exits the nearest enclosing loop or switch. A switch is a selection statement. Exiting a loop does not itself terminate the whole program.
Why does the position of the test affect the output?
Worked example 5. Try the integer values num from 1 through 10, but stop when num equals 8. Test before printing.
class Loop {
void main() {
for (int num = 1; num <= 10; num++) {
if (num == 8) {
break;
}
System.out.println(num);
}
}
}Answer: Output, with each value on a separate line:
1
2
3
4
5
6
7
- The first line declares the class Loop; its opening brace starts the class block.
- The second line declares the main method; its opening brace starts the method body.
- The for line initialises num, checks the upper limit and supplies its regular increment.
- The if line compares num with 8. Its opening brace begins the conditional block.
- The break line exits the loop when that comparison is true. The following brace closes the if block.
- The println line runs only on passes that do not execute break. It displays 1 through 7; 8 is tested but not printed.
- The next brace closes the loop body. When break executes, control passes beyond this loop without performing its next update.
- The final two braces close the method and the class. They produce no output.
The declared upper limit is not necessarily the last value processed by the full body. A break can finish the loop before that limit. When tracing, record the precise statement that transfers control instead of completing the remaining lines automatically.
A search can stop when its required result is found; an input loop can stop when a special terminating input is read. The terminating test belongs before any action that should exclude that value. In the worked program, printing belongs after the equality test.
The nearest enclosing rule means the closest surrounding loop or switch in the program structure. It does not mean every enclosing structure. When reading a simple loop with an if inside it, remember that if does not itself become break's target.
How does continue skip part of an iteration?
Property: continue skips the remaining statements of the current iteration
continue skips the remaining statements of the current loop body. It does not, by itself, finish the loop. Control proceeds along the loop's continuation path, where the update or condition decides what happens next.
Where does control go in each loop form?
In a basic for loop, continue leads to the update and then the condition. In while, it leads to the condition. In do while, it leads to the condition at the end. These paths matter when an update is written inside the body.
Worked example 6. Use the integer counter num from 1 through 6. Skip printing when num equals 3, then continue with later values.
class Loop {
void main() {
for (int num = 1; num <= 6; num++) {
if (num == 3) {
continue;
}
System.out.println(num);
}
}
}Answer: Output, with each value on a separate line:
1
2
4
5
6
- The first line declares the class Loop; its opening brace starts the class block.
- The second line declares the main method; its opening brace starts the method body.
- The for line establishes num, the upper limit 6 and an increment after each completed or continued iteration.
- The if line tests num == 3. When it is true, the continue line skips the remaining body and transfers control to the for update.
- The brace following continue closes the if block. The println line displays num on the other iterations.
- The final loop brace ends the body. The sequence continues after the skipped value and finishes when the for condition becomes false.
- The final two braces close the method and the class. They produce no output.
| Feature | break | continue |
|---|---|---|
| Effect on the current body | Skips the remaining statements | Skips the remaining statements |
| Effect on the loop | Exits the target loop | Follows the continuation path |
| Basic for update | Skipped on the exit path | Executed before the next condition test |
Note: In while, placing continue before the only counter update can repeatedly return to the condition with an unchanged value. Check that every intended repeating path makes the progress required for termination.
For a correct trace, stop reading the current body as soon as continue executes. Resume at the appropriate update or test. Do not print a skipped statement's value, but do not discard later iterations unless the loop's continuation test also fails.
How can tracing verify products and descending sequences?
A dry run is a manual execution of a program, recording variable values and output in order. It checks whether the implemented actions match the task. A trace should distinguish initial values, condition results, updates and printed output.
How does a product accumulator work?
The factorial of a positive integer is the product of the integers from 1 through that integer. The factorial of zero is 1. A product accumulator starts at 1 so that multiplying the first factor preserves it; starting at zero would keep the product zero.
Worked example 7. Calculate the factorial of 5. The integer variable fact holds the running product, and the integer counter i supplies factors from 1 through 5.
class Loop {
void main() {
int fact = 1;
for (int i = 1; i <= 5; i++) {
fact = fact * i;
}
System.out.println(fact);
}
}Answer: Output, with each value on a separate line:
120
- The first line declares the class Loop; its opening brace starts the class block.
- The second line declares the main method; its opening brace starts the method body.
- The declaration creates fact with initial value 1 before any multiplication.
- The for line makes i pass through the factors 1 to 5, increasing by one after each iteration.
- The assignment line multiplies the current fact by i and stores the new product back in fact.
- The loop closing brace ends each multiplication pass. The println line lies after the loop, so it displays the final product 120 once.
- The final two braces close the method and the class. They produce no output.
How is a decreasing counter traced?
The operator -= subtracts the right-hand value from the variable on its left and stores the result. A decreasing counter needs a condition compatible with its direction. In the next program, a is an integer whose initial value and decrement are both specified.
Worked example 8. Starting with a equal to 110, print it while it exceeds 100, reducing it by 2 after each print.
class Loop {
void main() {
int a = 110;
while (a > 100) {
System.out.println(a);
a -= 2;
}
}
}Answer: Output, with each value on a separate line:
110
108
106
104
102
- The first line declares the class Loop; its opening brace starts the class block.
- The second line declares the main method; its opening brace starts the method body.
- The declaration creates a and gives it the starting value 110.
- The while line tests a > 100 before each iteration. The println line displays a only when that comparison is true.
- The a -= 2 line reduces a by 2 after printing. The closing loop brace sends control back to the test.
- After 102 is printed, the update makes a equal to 100. The next test is false, so 100 is not printed.
- The final two braces close the method and the class. They produce no output.
A boundary is a limiting value used by a condition. Check whether equality belongs inside the loop's allowed values. The distinction between greater than and greater than or equal to changes whether the boundary is processed.
Finally, check the location of each printing statement. Printing inside a loop displays intermediate values; printing after it displays a final result. A correct arithmetic operation in the wrong position can therefore produce output that does not match the requested task.
Glossary
- Iteration — One repetition of a loop body during a program's execution.
- Loop body — The statement or block repeatedly executed under a loop's control.
- Control variable — A variable whose value helps determine whether a loop continues.
- Initialisation — Giving a variable its starting value before its intended processing begins.
- Condition — A boolean test whose true or false result controls execution.
- Update — A change to a variable as the loop progresses through repetitions.
- Entry-controlled loop — A loop that checks its continuation condition before executing its body.
- Exit-controlled loop — A loop that checks its continuation condition after executing its body.
- Accumulator — A variable storing a running result while successive values are processed.
- Finite loop — A loop that finishes after a finite number of iterations.
- Infinite loop — A loop that continues without reaching an exit in its execution path.
- Delay loop — A loop used mainly to consume execution time through repeated operations.
- break — A jump statement that exits its nearest enclosing loop or switch.
- continue — A jump statement that skips the remainder of the current loop body.
- Dry run — Manual execution that records changing variable values and the resulting output.
Common errors and misconceptions
- Misconception: Every loop executes its body before testing. Correct: for and while test first and can skip the body entirely. do while enters the body before testing its continuation condition.
- Misconception: The for initialisation repeats with every iteration. Correct: It runs once on entry. The condition and update occupy the repeating control path.
- Misconception: A semicolon after a while header is harmless punctuation. Correct: It makes the loop body an empty statement. A following block is then outside that loop; the do while ending semicolon has a different role.
- Misconception: break and continue both finish the loop. Correct: break exits its target; continue skips the remaining current body and follows the continuation path.
- Misconception: Moving a for update to the end of while always preserves behaviour. Correct: A continue can skip that statement in while. Check all repeating paths during conversion.
- Misconception: The last printed counter is necessarily its value when the loop exits. Correct: An update can change the counter before the failed test. Keep output separate from later state changes.
- Misconception: A product accumulator should start at zero. Correct: Multiplication by zero keeps the product zero. Initialise the factorial accumulator to one before multiplying its factors.
- Misconception: A fixed loop count guarantees a fixed delay in seconds. Correct: The elapsed time depends on the execution environment as well as the work performed.
Exam-style questions with model answers
Q1. Distinguish entry-controlled and exit-controlled loops, naming the Java loops in each group. [2 marks]
- An entry-controlled loop tests before the body, so an initially false condition skips the body. Java for and while are entry-controlled.
- An exit-controlled loop tests after the body, so the first entry precedes testing. Java do while is exit-controlled.
Q2. Trace this complete method-body fragment: int count = 1; while (count <= 5) { System.out.println(count); count++; } State the output, the update and the exit value. Here count is an integer counter. [3 marks]
- The output is 1, 2, 3, 4 and 5, each on a separate line, because the printing statement executes while count is at most 5.
- After every print, count++ increases count by one. The initial assignment to 1 occurs before the loop and is not repeated.
- After printing 5, the update changes count to 6. The condition is then false, so the loop exits with count equal to 6.
Q3. Explain the execution of this complete method-body fragment: int count = 1; do { System.out.println(count); count++; } while (count <= 5); Here count is an integer counter. Give five separate steps including the output. [5 marks]
- The declaration creates the integer counter count and stores 1 before the do while loop begins. This initialisation occurs once on entry to the fragment.
- The do block is entered before any continuation test. Its printing statement displays the current value of count and moves to a new line.
- The count++ statement increases count by one after each displayed value. The condition therefore examines the updated counter, not the value just printed.
- The test count <= 5 repeats the body while the updated counter is at most 5. The complete output is 1, 2, 3, 4, 5 on separate lines.
- After printing 5, the update produces 6 and the test fails. Execution continues after the loop; the semicolon following the final condition completes its syntax.
Q4. For this complete method-body fragment, explain the output and exit: for (int num = 1; num <= 10; num++) { if (num == 8) { break; } System.out.println(num); } Here num is an integer counter. [3 marks]
- The output consists of the integers 1 through 7, each on a separate line. For those values, the equality test against 8 is false and printing occurs.
- When num becomes 8, the if condition is true and break executes. The printing statement later in the body is skipped, so 8 is not displayed.
- Control passes after the loop. The remaining permitted values through 10 are not processed, and the for update is not executed on the break path.
Q5. For this complete method-body fragment, explain the output and control transfer: for (int num = 1; num <= 6; num++) { if (num == 3) { continue; } System.out.println(num); } Here num is an integer counter. [3 marks]
- The output is 1, 2, 4, 5 and 6 on separate lines. The printing statement executes for every allowed counter value except 3.
- At num equal to 3, continue skips the rest of the current body. It does not leave the loop, so later permitted values can still be processed.
- Control reaches num++ in the for header, then tests num <= 6 again. Repetition ends after printing 6 and updating num to 7.
Q6. Trace this complete method-body fragment: int fact = 1; for (int i = 1; i <= 5; i++) { fact = fact * i; } System.out.println(fact); Here fact is an integer product accumulator and i is an integer factor counter. Explain the initial value, multiplication, control and output. [4 marks]
- The accumulator fact starts at 1, the value that leaves the first multiplied factor unchanged. It is initialised before the loop so that subsequent products are retained.
- The counter i begins at 1 and increases by one after each iteration. Its condition includes 5, so the factors processed are 1 through 5.
- Each assignment replaces fact with its previous value multiplied by the current factor i. The resulting final product is the factorial of 5.
- The output is 120, printed once. The println statement is after the loop, so it displays the completed product rather than printing after every multiplication.
Q7. Trace this complete method-body fragment: int a = 110; while (a > 100) { System.out.println(a); a -= 2; } Here a is an integer counter, and -= 2 decreases it by 2. State the output and explain why 100 is excluded. [3 marks]
- The program displays 110, 108, 106, 104 and 102 on separate lines. Each displayed value satisfies the condition a > 100 at the start of its iteration.
- The statement a -= 2 decreases a after each print. After 102 is displayed, this update changes the counter to 100 before the next condition test.
- The condition uses greater than rather than greater than or equal to. It is false at 100, so the body is not entered and that boundary value is not printed.
Key takeaways
- A loop expresses repeated work through a body and a control path that decides whether execution continues.
- for and while test before the body; do while tests after entering its body for the first time.
- Trace initialisation, condition, body and update in their actual execution order, keeping printed output separate from changing variables.
- Convert loops by preserving first-entry behaviour, updates and exit paths, including any path taken by continue.
- break exits its target loop, while continue skips the remaining current body and follows the loop's continuation path.
- Multiple counters can share a for header, with comma-separated initialisations and updates but one continuation condition.
- A finite delay loop may have an empty body, but its iteration count does not guarantee a duration in seconds.
- Initialise a product accumulator to one, preserve it between iterations, and print after the loop when only the final result is wanted.
Test yourself
Why is repeating a printing instruction in a loop useful?
It expresses a repeated action once and controls successive values through the counter, avoiding a separate printing statement for every value.
Which executes first in a for loop: initialisation or the condition?
Initialisation executes first and runs once on entry. The condition is then tested before the body can execute.
What happens if a while condition is false on its first test?
The loop body is skipped entirely, and execution proceeds to the statement after the loop.
Why does do while end with a semicolon?
The semicolon completes the do while statement after its trailing condition; it is required by this loop's syntax.
Where does continue transfer control in a basic for loop?
It transfers control to the update part, followed by the condition test that determines whether another iteration begins.
Does break necessarily stop the whole program?
No. An ordinary break exits the nearest enclosing loop or switch, and execution can continue after that structure.
Why can a while update after continue be a problem?
The update may be skipped, leaving the control variable unchanged and allowing an unintended repeating path to continue indefinitely.
Why should a factorial product accumulator begin at one?
One preserves the first factor under multiplication. Beginning at zero would keep the accumulated product zero during subsequent multiplications.
