While Loop

loop runs as long as the while condition is true/met

For Loop

loop runs for a set amount of iterations

Recursion Loop

recursive function calls itself repeditly, forming a loop

Nested Iteration

previous 3 types of loops are applicable, loop inside a loop

While Loop Practice

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: Invalid input. Enter a number in the range of 1 through 100: 

For Loop Practice

public class LoopConversion 
{
    public static void main(String[] args) 
    {
        int count ;
        //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

Pop Quiz

public class PopQuiz 
{
    public static void main(String[] args) 
    {
        int count;
        
        for (count=0; count<5; count++)
        {
            System.out.println(count);
        }

        int count2 = 0;

        while (count2 < 5)
        {
            System.out.println(count2);
            count2++ ;
        }
    }
}
PopQuiz.main(null)
0
1
2
3
4
0
1
2
3
4

Homework- Choose one or do both

  • Write a program where the user inputs their monthly budget. The loop should then ask the user to input each of their monthly expenses. These expenses should be kept in a running total. The final output should display if the user is over or under their budget for the month, and by how much.
  • 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.

Also complete the quiz.

HW Part 2

import java.util.Scanner;
 
public class Numberguesser {

    public static void
    guessnumber()
    {
        
        Scanner scanner = new Scanner(System.in);
 
        int number = 1 + (int)(100* Math.random());
 
        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 13
Guess a number:
The number is greater than 19
Guess a number:
You have used all 5 trials.
The number was 32

Quiz This is an image