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

While Loops


while (boolean_expression)
    statement;

while (boolean_expression)
{
   statement;
   ....
   statement;
}


More concrete examples:


// The following loop prints out:
// 6789

int x = 5;
while (x < 9)
{
    x += 1;
    System.out.print(x);
}



The boolean_expression must evaluate to a boolean.
1. Demo some while loops incrementing up and incrementing down in various step sizes.
2. Introduce the idea of a do ... while();
3. WhileDemo example from the book.
4. while loops will execute no statements if the boolean_expression evaluates to false on the first iteration.
5. ***Demo the above example with a boolean variable in the while part. Setting a boolean flag to true, or false
6. Prompt the user for starting number and ending number and print out all of the even numbers in between.

Program created on the video:


public class WhileExamples {


    public static void main(String[] args) {
        // while (boolean expression)
        //    statement;

        int x = 100;

        // We can use  ==  !=   <   >   <=   >=
        while (x < 100)
        {
            System.out.println(x);
            x += 1;
        }
        System.out.println("********");
        do
        {
            System.out.println(x);
            x += 1;
        } while (x < 100);


        int y = 40;
        boolean  continueLooping= true;

        System.out.println("********   Using boolean continueLooping *******");

        while (continueLooping)
        {
            System.out.println(y);
            y += 2;
            continueLooping =   y < 60;
        }

        //while(true)
        //{
            // code on top

            // if (some condition)
            //     break;

            // code on bottom
        //}

        System.out.println("Done!");
    }

}


Averager.java (example from chapter 3 of textbook)


import java.util.Scanner;

public class Averager
{
    public static void main(String[] args)
    {
      Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter a list of nonnegative scores.");
        System.out.println("Mark the end with a negative number.");
        System.out.println("I will compute their average.");

        double next, sum = 0;
        int count = 0;

        next = keyboard.nextDouble( );
        while(next >= 0)
        {
            sum = sum + next;
            count++;
            next = keyboard.nextDouble( );
        }

        if (count == 0)
            System.out.println("No scores entered.");
        else
        {
            double average = sum/count;
            System.out.println(count + " scores read.");
            System.out.println("The average is " + average);
        }
    }
}



WhileDemo.java (from chapter 3 of textbook)



public class WhileDemo
{
    public static void main(String[] args)
    {
        int countDown;

        System.out.println("First while loop:");
        countDown = 3;
        while (countDown > 0)
        {
            System.out.println("Hello");
            countDown = countDown - 1;
        }

        System.out.println("Second while loop:");
        countDown = 0;
        while (countDown > 0)
        {
            System.out.println("Hello");
            countDown = countDown - 1;
        }

        System.out.println("First do-while loop:");
        countDown = 3;
        do
        {
            System.out.println("Hello");
            countDown = countDown - 1;
        }while (countDown > 0);

        System.out.println("Second do-while loop:");
        countDown = 0;
        do
        {
            System.out.println("Hello");
            countDown = countDown - 1;
        }while (countDown > 0);
    }
}


Last Modified: July 19, 2013 4:05 PM