Iterations

  • make code shorter - simplify repeating code into simpler section

While Loop

  • while a condition is met, code will keep
  • do while loop - execute not matter what

For Loop

  • for as long as a condition is true, continue printing

Recursion Loop

  • own function that repeats over and over again

Nested Iteration

  • not a official loop
  • put loop inside loop - can use any of the pervious loops
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 < 1 || number > 100)
        {  
           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: 
public class LoopConversion 
{
    public static void main(String[] args) 
    {
        int count = 0;
        //convert to for loop
        for (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 ForLoop
{
    public static void main(String[] args) 
    {
        int i = 0;
        //convert to for loop
        for (i=0; i<5; i++)
        {
            System.out.println(i);
        }

    }
}
ForLoop.main(null)
0
1
2
3
4
public class WhileLoop
{
    public static void main(String[] args) 
    {
        int i = 0;
        //convert to for loop
        while (i<5)
        {
            System.out.println(i);
            i++;
        }

    }
}
WhileLoop.main(null)
0
1
2
3
4

Homework 1

Write a program where a random number is generated. Then the user tries to guess the number. If they guess too high display something to let them know, and same for if they guess a number that is too low. The loop must iterate until the number is guessed correctly.

import java.util.Scanner;
 
public class NumberGuesser { // creation of a class, capitalize internal words

    public static void
    guessnumber()
    {
        
        Scanner scanner = new Scanner(System.in);
 
        int number = 1 + (int)(100* Math.random()); // use of random number generator using math class
 
        int i, guess;
 
        System.out.println(
            "A number is chosen between 1 to 100."
            + "Guess the number"
            + " within 5 trials.");
 

        for (i = 0; i < 5; i++) {
 
            System.out.println(
                "Guess a number:");
 

            guess = scanner.nextInt();

            if (number == guess) {
                System.out.println("You guessed the number.");
                break;
            }
            else if (number > guess&& i != 5 - 1) {
                System.out.println("The number is greater than " + guess);
            }
            else if (number < guess && i != 5 - 1) {
                System.out.println("The number is less than " + guess);
            }
        }
 
        if (i == 5) {
            System.out.println(
                "You have used all 5 trials.");
 
            System.out.println(
                "The number was " + number);
        }
    }
 
    // Driver Code
    public static void
    main(String arg[])
    {
 
        // Function Call
        guessnumber();
    }
}

NumberGuesser.main(null);
A number is chosen between 1 to 100.Guess the number within 5 trials.
Guess a number:
The number is less than 50
Guess a number:
The number is greater than 25
Guess a number:
The number is greater than 37
Guess a number:
The number is greater than 45
Guess a number:
You guessed the number.

Quiz

This is an image