number of repetitions The counter value is increased or decreased each time the loop body is executed The loop terminates when the counter reaches a certain value Example: print “hello world” ten times
to terminate the loop The body of the loop is executed until the sentinel is found For example, a program that continues calculating the interest rate and stops after the last customer We can choose the sentinel to be the characters ‘n’ or ‘N’
in if statements The difference in the flowchart is that the loop goes back to evaluating the condition false true condition initialization loop body update
the while loop header is always true It is important to verify in the body of the loop that there is a way for the condition to become false at some point Sentinel or counting value
bypass the remainder of the current pass through a loop The loop does not terminate because a continue statement is encountered The loop goes back to evaluate the condition This statement can be included within any of the loops that we know
input Use greater-than sign (>) for output Can use both as well % ./program > output.txt % ./program < input.txt % ./program < input.txt > output.txt
EOF (end of file) to end the program EOF is Ctrl-D on UNIX or Mac, Ctrl-Z on PC Can do this with I/O redirection from a file % ./program < electricity.txt