Читать книгу OCP Oracle Certified Professional Java SE 17 Developer Study Guide - Jeanne Boyarsky - Страница 194

Writing while Loops

Оглавление

A common practice when writing software is doing the same task some number of times. You could use the decision structures we have presented so far to accomplish this, but that's going to be a pretty long chain of if or else statements, especially if you have to execute the same thing 100 times or more.

Enter loops! A loop is a repetitive control structure that can execute a statement of code multiple times in succession. By using variables that can be assigned new values, each repetition of the statement may be different. The following loop executes exactly 10 times:

int counter = 0; while (counter < 10) { double price = counter * 10; System.out.println(price); counter++; }

If you don't follow this code, don't panic—we cover it shortly. In this section, we're going to discuss the while loop and its two forms. In the next section, we move on to for loops, which have their roots in while loops.

OCP Oracle Certified Professional Java SE 17 Developer Study Guide

Подняться наверх