In the first example, we can prove that the loop terminates when n is positive.īut in general, it is not so easy to tell whether a loop terminates. This example will print the number 3 forever, or at least until you terminate the program.Īn endless source of amusement for computer scientists is the observation that the directions on shampoo, “Lather, rinse, repeat,” are an infinite loop. Each row of the following table shows a Java code segment featuring a for-loop and the output it produces when executed. Otherwise, the loop will repeat forever, which is called an infinite loop: int n = 3 The purpose of the loop is to pass sequentially through the numbers 1, 2, 3,, n, and for each number to perform a particular action. The body of the loop should change the value of one or more variables so that, eventually, the condition becomes false and the loop terminates. This type of flow is called a loop, because the last step “loops back around” to the first.įigure 6.1 shows this idea using a flowchart.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |