logo Practice-It logo

BJP4 Self-Check 5.8: doWhileLoops

Language/Type: Java do/while
Author: Will Beebe (on 2016/09/08)

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

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

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.