Loop: A Block of instructions execute repeatedly as long the condition is valid.
Note: Block executes only once whereas Loop executes until condition become False
Java Supports 3 types of Loops:
- For Loop
- While Loop
- Do-While Loop
For Loop: We use for loop only when we know the number of repetitions. For example,
- Print 1 to 10 numbers
- Print Array elements
- Print Multiplication table
- Print String character by character in reverse order
While loop: We use while loop when we don’t know the number of repetitions.
- Display contents of File
- Display records of Database table
Do while Loop: Execute a block at least once and repeat based on the condition. ATM transaction: When we swipe the ATM card, it starts the first transaction. Once the first transaction has been completed, it asks the customer to continue with another transaction and quit.