Repetition in Java

Explains how to write while, for, and do while loops in a Java program.

Prerequisites

Objectives

Purpose of Repetition

Sometimes it is necessary to repeat a block of code many times. It may seem like a simple solution to copy a block of code and paste it as many times as you need, but there is an alternative to this code bloating idea. In most programming languages there is some type of mechanism for repeating a block of code. This is known as repetition or a loop. Some examples of when you want the computer to repeat a block of code are:

while Loops

for Loops

do while Loops

for each Loops

Nested Loops