Iteration

  • ++ / -- operator, increase before or after a
  • while loops, for, recursion
  • while - while something is true the loop will keep recurring
  • for - for each thing in a sequence, the code will repeat
  • recursion loop - calls itself to repeat
  • nested iteration - putting a loop inside of a 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 < 1 || )
        {  
           System.out.print("Invalid input. Enter a number in the range " +  
                            "of 1 through 100: ");  
           number = keyboard.nextInt();  
        } 
    }
}
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);
            count++;
        }
    }
}
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.print(i);
}
01234
int i=0; 
while(i<5){
    System.out.print(i);

    i++;
}
01234

Homework

-