© Ron Dai 2019
R. DaiLearn Java with Mathhttps://doi.org/10.1007/978-1-4842-5209-3_9

9. Loop Structure – while Loop

Ron Dai1 
(1)
Seattle, WA, USA
 
This is another way for the loop structure (Figure 9-1).
        while(i == 0) {
              <do something>;    //the variable "i" may be updated in this code block.
        }
../images/485723_1_En_9_Chapter/485723_1_En_9_Fig1_HTML.png
Figure 9-1

The while loop

The state may be updated within the “Do something” process.

Question: What will happen if the state is never changed?

Answer: it will be an “infinite loop,” meaning the program will run forever until it crashes or is terminated by the user.

Note

You may have noticed the // next to the <do something> line. This identifies a comment left by the developer. You should use comments to annotate your code so it’s easier to understand for future readers (who could be you, so be kind to your future self). The compiler will ignore all comments when compiling your Java programs.

Example

How many times will the loop execute its body?
int x = 1;
while (x < 100) {
    System.out.print(x + " ");
    x += 10;
}

Answer: ten times (when x = 1, 11, 21, 31, 41, 51, 61, 71, 81, 91)

Example

How many times will the loop execute its body?
int max = 10;
while (max < 10) {
    System.out.println("count down: " + max);
    max--;
}

Answer: zero.

Both the for loop and while loop are loop structures to accomplish a repetitive job (i.e., <do something>). The for loop has provided an easy way to assign an initial counter value and to define how the counter value is incremented (or decremented) within the same line of code, while the while loop requires the user to define them in separate lines. The following two examples have an equivalent functionality.
for(int i = 0; i < 10; i++) {
        <do something>
}
int i = 0;
while(i < 10) {
        <do something>
        i++;
}
There is a good reason why we need the while loop option, in addition to the for loop. This example shows one of many circumstances when we prefer the while loop over the for loop.
        boolean flag = true;
        while(flag) {
               < commit planned operations , during which time the flag may be updated upon a certain codition change, e.g. the operation is completed, or failed for some reason.>
        }

The do-while Loop

Java also provides a do-while loop structure:
do {
     <do something>
} while (expression);
The difference between do-while and while is that do-while evaluates its Boolean expression at the bottom of the loop instead of the top. Therefore, the statements within the do block (a.k.a. <do something>) are always executed at least once. You may try the following program to see a demo.
class DoWhileDemo {
    public static void main(String[] args){
        int count = 1;
        do {
            System.out.println("Count is: " + count);
            count++;
        } while (count < 1);
    }
}

Lab Work

  1. 1.

    Use the while loop to output “Hello!” 10 times.

     
  2. 2.

    Use the while loop to print out all integers from 1 to 25, inclusively.

     
  3. 3.
    Explain what the following code snippet is trying to do.
    int n = 5;
    while (n == 5) {
           n = n + 1;
           System.out.println(n);
           n--;
    }
     

Problems

  1. 1.
    How many times will the loop execute its body?
    int x = 250;
    while (x % 3 != 0) {
        System.out.println(x);
    }
     
  2. 2.
    How many times will the loop execute its body?
    int x = 2;
    while (x < 200) {
        System.out.print(x + " ");
         x *= x;
    }
     
  3. 3.
    How many times will the loop execute its body?
    String word = "a";
    while (word.length() < 10) {
        word = "b" + word + "b";
    }
    System.out.println(word);
     
  4. 4.
    How many times will the loop execute its body?
    int x = 100;
    while (x > 1) {
        System.out.println(x / 10);
        x = x / 2;
    }
     
  5. 5.
    Given the static method runWhileLoop(), what is its output when x = 10? You may want to copy this method to your test class to try.
    public static void runWhileLoop(int x) {
        int y = 1;
        int z = 0;
        while (2 * y <= x) {
            y = y * 2;
            z++;
        }
        System.out.println(y + " " + z);
    }
     
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.82.23