Loops

Figure 182. Why loops? Slide presentation Create comment in forum
Objective

Execute the same statement multiple times.

Solution

Copy / paste the statement in question:

System.out.println("Do not copy!");
System.out.println("Do not copy!");
System.out.println("Do not copy!");
System.out.println("Do not copy!");
Problem

Only works if number of repetitions is known at compile time.


Figure 183. Hard to parameterize Slide presentation Create comment in forum

Limited workaround for repetition count given at runtime:

int repetitions;

repetitions = 3; // Simulated dynamic value
                 // Runtime, not compile-time

switch(repetitions) {
    case 5: System.out.println("Do not copy!");
    case 4: System.out.println("Do not copy!");
    case 3: System.out.println("Do not copy!");
    case 2: System.out.println("Do not copy!");
    case 1: System.out.println("Do not copy!");
}

Problem: Clumsy and limited (predefined maximum).