The Logic of a Loop
In this lesson, we will introduce the general logic of a loop.
We'll cover the following
What is iteration?
The ability to repeat tasks rapidly is one of the keys to the power of computing. Whenever we are searching the web, playing video games, or using a bank ATM, we are experiencing the effects of this repetition. In Java, we can repeat a group of statements by using a controlling statement in a process known as iteration. A completely different approach to repetition is called recursion, which we will see in the sequel to this course.
What is a loop?
We use one of three different statements to control the repetition of a group of statements. The controlling statement and the repeated group of statements together are called a loop. This chapter will look at one way to control a loop, and the next chapter will consider two others.
We can discuss the logic of a loop without knowing the syntax of the Java statements that control it. Typically, we consider four steps when we think of a loop, as the figure given below illustrates. The first step involves getting ready to repeat a group of statements. We might initialize a counter or a condition that affects the number of times the statements will repeat or iterate. We might also initialize certain parts of a computation that will be repeated during the loop. This initialization step is performed once.
Get hands-on with 1400+ tech skills courses.