Practice-It! logo

Self-Check 5.1: whileLoops

Added by: Will Beebe on 2010/06/16
Keywords: while loops

NOTE: This problem seems to come from an oudated edition of the Building Java Programs textbook. Please make sure that you are solving the problem from the right edition of the textbook that you are using in your class. (Click here to jump to the current edition's version of this problem instead.)

For each of the following while loops, how many times will the loop execute its body? Remember that "zero," "infinity," and "unknown" are legal answers.

a)
int x = 1;
while (x < 100) {
    System.out.print(x + " ");
    x += 10;
}
b)
int max = 10;
while (max < 10) {
    System.out.println("count down: " + max);
    max--;
}
c)
int x = 250;
while (x % 3 != 0) {
    System.out.println(x);
}
d)
int x = 2;
while (x < 200) {
    System.out.print(x + " ");
     x *= x;
}
e)
String word = "a";
while (word.length() < 10) {
    word = "b" + word + "b";
}
System.out.println(word);
f)
int x = 100;
while (x > 0) {
    System.out.println(x / 10);
    x = x / 2;
}
a.
b.
c.
d.
e.
f.

You must log in before you can solve this problem.

user
Log In

If you do not understand how to solve a problem or why your solution code doesn't work, please contact your TA or instructor.

If something seems wrong with the Practice-It system itself (errors, slow performance, incorrect problem descriptions/tests, etc.), please contact us.

Is there a problem?
Contact a Practice-It administrator.