int counter = 0;
boolean isRunning = true;

while (isRunning) {
    counter++;
    if (counter == 5) {
        isRunning = false;
    }
}

System.out.println("Counter stopped at: " + counter);