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 –
- Entry controlled loop
- 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 –
- For loop (Entry controlled)
- While loop (Entry controlled)
- Do while loop (Exit controlled)
- For each loop