Loop Types in Java

In Java, if programmer uses loops to to perform "specific operation" multiple times. The "specific operation" are called "controlled statements" or "body of a loop" which should be kept inside the loop body.

The body of a loop gets executed repeatedly until the condition becomes false or the programmer breaks the loop.

In above diagram, if the body of a loop contains a break; statement then the loop gets break.

Loop Type in Java

While Loop While a given expression is true it repeats the statement in the loop body. Before executing
the loop body it tests the condition for true or false.
Do-while Loop It is like a while loop but it tests the condition after executing the loop body.
For Loop In above two loops we need to write the increment or decrement operation to break the loop after sometime. But in for loop we have an option of incrementing or decrementing outside the loop body.
For Each Loop This loop applies a function to the range of elements in a collection.
Nested Loop When using one or more loops inside a loop is known as nested loop.

Loop Control Statements in Java

By using loop control statements we can change the flow of execution of statements inside the loop body. Java supports two control statements as mentioned below.

 Control Statement
break statement Break terminates immediately the loop statement from executing further and execution reaches just outside the loop body containing the break statement.
continue statement Continue statement is equivalent to going to the very end of the loop immediately by skipping further statements.

Next > < Prev
Scroll to Top