Loops

while loop

public static void main(String[] args) {
    Integer randomValue = ThreadLocalRandom.current().nextInt(0, 2);
    Boolean condition = randomValue == 1;
    while(condition) {
        randomValue = ThreadLocalRandom.current().nextInt(0, 2);
        System.out.println("The value was 1");
    }
}

do/while loop

public static void main(String[] args) {    
    Boolean condition;
    do {
        Integer randomValue = ThreadLocalRandom.current().nextInt(0, 2);
        System.out.println("The value was 1");
        condition = randomValue == 1;
    } while(condition);
}

Determinate Loops

for (int counter = 0; counter < 10; counter++) {
    System.out.println(counter);
}
int counter = 0
while(counter < 10) {
    System.out.println(counter);
    counter++
}

Nested Loops

for(int i = 0; i < 5; i++) {
    // variable `i` cannot be redeclared in the same scope
    for(int i = 0; i < 5; i++) {
        System.out.println(i);
    }
}
for(int i = 0; i < 5; i++) {
    for(int j = 0; j < 5; j++) {
        System.out.println(j);
    }
}

Statements That Break Control Flow

The same break statement that you use to exit a switch can also be used to break out of a loop

public static void main(String[] args) {
    Double interestRate = 0.01;
    Double balance = 20000D;
    Double payment = 200D;
    Double years = 0D;
    while (years <= 100) {
        balance += payment;
        double interest = balance * interestRate / 100;
        balance += interest;
        if (balance >= goal) {
            break;
        }
        years++;
    }

The continue statement transfers control to the header of the innermost enclosing loop

public static void main(String[] args) {
    Scanner userInputGrabber = new Scanner(System.in);
    Integer goal = 1000;
    Integer sum = 0;
    while (sum < goal) {
        System.out.print("Enter a number: ");
        String userInput = userInputGrabber.nextInt();
        Integer userInputAsInteger = Integer.parseInt(userInput);
        if (userInputAsInteger < 0) {
            continue;
        }
        sum += userInputAsInteger; // not executed if n < 0
    }
}