Title: While Loop Examples
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

While Loop Examples

1. Write a program that computes the power function for integers. You will prompt for "base" and "power" and compute
base ^power

A little math review (don't use a base of 0):
if power = 0, then the answer for any base is 1 .
if power < 0, then base ^power = 1 / base ^-power
for example 10^-2 = 1/10 ^2
2. Write a program that generates a random number between 1 and 100. It then asks you to guess the number and returns the answer correct, lower, and higher.
See how many iterations it takes for you to guess the number.
3. Nesting ... Do a multiplication with while loops. Do it also with a do-while.

Programs created on the video:

Power.java - Basic While Loops

import java.util.Scanner;


public class Power {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the Base(double):");
        double base = scan.nextDouble();

        System.out.println("Enter the Power(integer):");
        int power = scan.nextInt();

        double answer = 1;

        if(power >= 0)
        {
            while ( power > 0)
            {
                answer = answer * base;
                power -= 1;
            }
        }
        else
        {
            while ( power < 0)
            {
                answer = answer / base;
                power += 1;
            }
        }



        System.out.println("answer ="+ answer);


    }

}


GuessingGame - Basic While


import java.util.Scanner;


public class GuessingGame {

    public static void main(String[] args) {
        int guessNumber = (int)(1 + 100*Math.random());

        Scanner keyboard = new Scanner(System.in);
        int userGuess;
        boolean guessingGameInProgress = true;

        while (guessingGameInProgress)
        {
            System.out.println("Guess the number: ");
            userGuess = keyboard.nextInt();

            if (userGuess == guessNumber)
                guessingGameInProgress = false; // Number was guessed ... this game is over
            else
            {
                if (userGuess > guessNumber)
                {
                    System.out.println("Too High");
                }
                else if (userGuess < guessNumber)
                    System.out.println("Too Low");
            }

        }

        System.out.println("Congratulations you guessed the number");

    }

}


GuessingGame - do while approach

import java.util.Scanner;


public class GuessingGame2 {

    public static void main(String[] args) {
        int guessNumber = (int)(1 + 100*Math.random());

        Scanner keyboard = new Scanner(System.in);
        int userGuess;

        do
        {
            System.out.println("Guess the number: ");
            userGuess = keyboard.nextInt();

            if (userGuess > guessNumber)
            {
                System.out.println("Too High");
            }
            else if (userGuess < guessNumber)
                System.out.println("Too Low");

        } while (userGuess != guessNumber);

        System.out.println("Congratulations you guessed the number");

    }

}


Multiplication Example - using basic while loops

public class Multiplication {

    public static void main(String[] args) {


        int labelCol =1;
        System.out.print( "    ");
        while (labelCol <= 12)
        {
            System.out.printf("%4d", labelCol);
            labelCol +=1;
        }
        System.out.println();
        String separator = "----";
        labelCol =1;
        System.out.print( "    ");
        while(labelCol <= 12)
        {
            System.out.printf("%4s", separator);
            labelCol+= 1;
        }
        System.out.println();

        int row =1;
        int col =1;

        while (row <= 12)
        {
            System.out.printf("%2d |", row);
            while (col <= 12)
            {
                System.out.printf("%4d", row * col);
                col +=1;
            }
            System.out.println();
            col = 1;
            row += 1;
        }

    }

}




Java Practice(JPHE):

1. (2 points) Create a program with a while loop that prints out the numbers 1 through 8. Your output should be:
1
2
3
4
5
6
7
8
2. (2 points) Create a similar program to the previous example, except that you are to use a "do - while" loop. Again your output should be:
1
2
3
4
5
6
7
8
3. (2 points) What is the output of this program? What is it doing? Create a fixed version of this program.

public class Bad_WhileLoop {

    public static void main(String[] args) {
        int i=3;
        while(i <=7);
        {
            System.out.println(i);
            i += 1;
        }
    }
}


4. (4 points) Write a program that contains a while that does the following:
  - Each time through the loop, the user will be prompted for another number
  - If the number is negative the loop will exit
  - if the number is not negative then you will increment a "count" variable, and add the value of the number to a "total" variable.
  - At the end of the loop, you will print out the average of all numbers entered.

Last Modified: December 23, 2014 3:29 PM