Читать книгу OCP Oracle Certified Professional Java SE 17 Developer Study Guide - Jeanne Boyarsky - Страница 198
Constructing for Loops
ОглавлениеEven though while
and do
/while
statements are quite powerful, some tasks are so common in writing software that special types of loops were created—for example, iterating over a statement exactly 10 times or iterating over a list of names. You could easily accomplish these tasks with various while
loops that you've seen so far, but they usually require a lot of boilerplate code. Wouldn't it be great if there was a looping structure that could do the same thing in a single line of code?
With that, we present the most convenient repetition control structure, for
loops. There are two types of for
loops, although both use the same for
keyword. The first is referred to as the basic for
loop, and the second is often called the enhanced for
loop. For clarity, we refer to them as the for
loop and the for-each loop, respectively, throughout the book.