2.6 Always the Same with Loops
Loops are used to process certain statements over and over again. A loop includes a loop condition and the loop’s body. The loop condition, a Boolean expression, decides under which condition the repetition is executed. Depending on the loop condition, the body can be executed several times. For this purpose, the loop condition is checked in each loop pass. The result decides whether the body is traversed another time ( true) or the loop is terminated ( false ). Java provides four types of loops.
|
Loop Type |
Syntax |
|---|---|
|
while loop |
while ( condition ) statement |
|
do- while loop |
do statement while ( condition ); |
|
Simple for loop |
for ( initialization ; condition ... |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access