What is a loop?

A loop is one of the control flow statement that specifies repetition. A loop allows you to execute code repeatedly. In general, we use a loop when we have to execute a set of statements repeatedly until a particular condition is reached.

Loop control

A loop can be controlled in two different ways –

  1. Entry controlled loop
  2. Exit controlled loop

Entry controlled loop

The entry controlled loops are those ones, where the condition is checked before entering into the loop body.

Flow diagram

Exit controlled loop

In exit controlled loop condition is checked after entering into the loop body, that means at least one-time statements inside the loop body will be executed. It is just the opposite of the entry controlled loop.

Flow diagram

Types of loop

There are three different types of loop available in Java –

  1. For loop (Entry controlled)
  2. While loop (Entry controlled)
  3. Do while loop (Exit controlled)
  4. For each loop