3 Types

Loops help simplify repetitive code, almost all of them use conditions that need to be changed within the code for the loop to stop.

While Loops

While a specified condition is met, the loop will keep running until the loops condition is no longer true.

For Loop

For each thing in a sequence of code for each part as long as a condition is met, if the condition is met as false, the condition will no longer be true and thus stop.

Recursion

Similar to For and While loops but you run and run the function inside itself over and over again. They have conditions using if/else and keeps running until the if statement no longer is true and thus finally ends with the else statement in which the function is no longer true.

Nested Iteration

Not a method but it's a technique to simplify code more. You put a loop within a loop and it runs two conditions where once one is no longer run, it runs again through the upper loop

//while loop
import java.util.Scanner;

public class Checker 
{
    public static void main(String[] args) 
    {
        int number;  
	
        // Create a Scanner object for keyboard input.  
        Scanner keyboard = new Scanner(System.in);  
             
        // Get a number from the user.  
        System.out.print("Enter a number in the range of 1 through 100: ");  
        number = keyboard.nextInt();  

        while (number > 100 || number < 0)
        {  
           System.out.print("Invalid input. Enter a number in the range " +  
                            "of 1 through 100: ");  
           number = keyboard.nextInt();  
        } 
    }
}

Checker.main(null);
Enter a number in the range of 1 through 100: Invalid input. Enter a number in the range of 1 through 100: Invalid input. Enter a number in the range of 1 through 100: Invalid input. Enter a number in the range of 1 through 100: 
public class LoopConversion 
{
    public static void main(String[] args) 
    {
        //convert to for loop
        for (int count = 0; count < 5; count++)
        {
            System.out.println("count is " + count);
        }
    }
}

LoopConversion.main(null);
count is 0
count is 1
count is 2
count is 3
count is 4
public class Clock  
{  
    public static void main(String[] args)  
    {  
        for(int hours = 1; hours <= 12; hours++)  
        {  
            for (int minutes = 0; minutes <= 59; minutes++)  
            {  
                for (int seconds = 0; seconds <= 59; seconds++) 
                {  
                    System.out.printf("%02d:%02d:%02d\n", hours, minutes, seconds); 
                } 
            } 
        } 
    } 
}
for (int i = 0; i < 5; i++){
    System.out.println(i);
}
0
1
2
3
4
int i = 0;
while (i < 5){
    System.out.println(i);
    i++;
}
0
1
2
3
4
public void recursion(num1, num2)
{
    if(num1 < num2){
        System.out.println(num1);

        num1++;
        recursion(num1, num2);
    }
    else{
        System.out.println(num2);
    }
}

recursion(0, 5);
|   public void recursion(num1, num2)
<identifier> expected

|   public void recursion(num1, num2)
<identifier> expected