CHAPTER 5

LOOPS

The real strength of computers and computer programs is in looping. A computer can execute thousands of instructions in no time. If we have to code each and every instruction, we will need days to write a single program. The few instructions we write are repeated hundreds of time faithfully by the computer. For this purpose, we need loops.

In Java, just like C or C++, there are three structures for looping around a given code. They are

  • for loop
  • while loop
  • do-while loop

Statements break and continue are used mainly in these structures. Therefore, we will be discussing these statements too. Let us start with the for loop statement.

5.1 for Loop

for loop is the most useful statement (facility) in any programming language. A typical ...

Get Programming with Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.