C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1

The teacher had a lot of sick students that day! If all 25 students had shown up, the for loop would
have ensured that exactly 25 test scores were asked for. However, because only five students took the
test, the teacher had to let the program know, via a negative number in this case, that she was done
entering the scores and that she now wanted an average.


Tip

To print the percent sign at the end of the final average, two % characters have to be
used in the printf() control string. C interprets a percent sign as a control code
unless you put two of them together, as done in this program. Then it still interprets the
first percent sign as a control code for the second. In other words, the percent sign is a
control code for itself.

Warning

break simply offers an early termination of a while, do-while, or for loop.
break can’t exit from if, which isn’t a loop statement. Figure 16.1 helps show the
action of break.

FIGURE 16.1 break terminates a loop earlier than usual.

Let’s continue Working


Whereas break causes a loop to break early, continue forces a loop to continue early. (So
that’s why they’re named that way!) Depending on the complexity of your for, while, or do-
while loop, you might not want to execute the entire body of the loop every iteration. continue
says, in effect, “C, please ignore the rest of this loop’s body this iteration of the loop. Go back up to
the top of the loop and start the next loop cycle.”

Free download pdf