Otherwise, the loop will repeat forever, which is called an infinite loop: int n = 3 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. If the condition is true, execute the statements and go back to step 1.If the condition is false, skip the following statements in braces.Evaluate the condition in parentheses, yielding true or false.The flow of execution for a while statement is as follows: While n is greater than 0, print the value of n, and reduce the value of n by 1. Reading the code in English sounds like this: “Start with n set to 3. Using a while statement, we can repeat the same code multiple times: int n = 3 We’ll also take a first look at String methods and solve some interesting problems. In this chapter, you’ll learn how to use while and for loops to add repetition to your code. Repeating tasks without making errors is something that computers do well and people do poorly. Computers are often used to automate repetitive tasks, such as searching for text in documents.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |