Loops and the While Statement

Read this chapter, which explains while loops. This is in contrast to how a do-while loop works, which we will discuss later. In a do-while loop, the body of the loop is executed at least one time, whereas in a while loop, the loop may never execute if the loop condition is false.

1. Loops

Most modern machines work by endlessly repeating the same motions. The engine in your car cycles through the same motions over and over as it burns gasoline to provide power. Electric motors are similar. They convert electric power into circular motion. Because of their circular motions, these machines keep going as long as you want.

Computer programs use cycles, also. In programming, a cycle is called a loop. When a program has a loop in it, some statements are done over and over as long as is needed to get the work done. Most computer programs execute many millions of program statements each time they are used. Usually the same statements are executed many times. This chapter discusses several kinds of programming loops.

Chapter Topics:

      • The while statement
      • Syntax and semantics of the while statement
      • The loop body
      • Three parts of a loop that must be coordinated
      • Counting loops and the loop control variable

Question 1:

Think of some machines (mechanical or other) that use cycles.

Source: Bradley Kjell, http://programmedlessons.org/Java9/chap19/ch19_01.html
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 License.