|
Looping
A while loop can be used to repeat a statement multiple times. The general format of a while loop is
while (boolean_expression)
statement
When a while loop is encountered the boolean expression is evaluated. If it evaluates as true then the while loop's statement is performed, and then the flow goes back to the top of the loop (i.e., loops back) to evaluate the boolean expression again. If it evaluates as true then the while loop's statement is performed again. This process will repeat (loop) until the expression evaluates as false. It is possible to write loop whose expression never evaluates as false -- this results a loop known as an infinite loop.
The while loop's statement is offered referred to as its body. One flow of execution through the body is called one iteration of the loop.
There different kinds of loops. All of the loops in this section are counting loops. A counting loop typically has a variable (e.g., count) which is given a starting value (such as 1) just before the loop is entered. The while loop's condition is written so that it evaluates as true until the variable reaches a certain value, such as 10. The body of the loop should change the counting variable so that it works its way towards the end value. In the example shown below, a value of one is added to the count variable each time through the loop. Eventually the value exceeds ten and the loop ends. Therefore, the three most important values of for a counting loop are count's starting value, ending value, and increment amount.
The while loop's body can either be a simple statement or a compound statement (such as several statements grouped together with curly braces). Here is another examle of a while loop.
public class DemoLoop1
{
public static void main(String [] args)
{
int count = 1; // Starting value: 1
while (count <= 10)
{
System.out.println("count: " + count); // Ending value: 10
count = count + 1; // Increment amount: 1
}
}
}
When run the above code generates the following output:
count: 1
count: 2
count: 3
count: 4
count: 5
count: 6
count: 7
count: 8
count: 9
count: 10
The following example generates two lines of output during each interation of the loop:
public class DemoLoop1B
{
public static void main(String [] args)
{
int count = 1;
while (count <= 5)
{
System.out.println("And");
System.out.println(count);
count = count + 1;
}
}
}
The above loop processes the statements in its body five times, generating the following output:
And
1
And
2
And
3
And
4
And
5 Learning By Solving Problems
Sometimes it is useful to see examples that don't work. Fixing problems with faulty often helps people to remember those problems, and to avoid those same problems in the future.
The rest of this page contains several small problems. You should be able to figure out the answers on pencil and paper. To verify your answer, copy the problem's Java code and paste it into jGRASP and run it -- compare the answer generated in jGRASP to what you figured out using pencil and paper. The code below will not run by itself -- it needs to be pasted into main method inside a class, such as the VerifyIt class given below:
public class VerifyIt
{
public static void main(String [] args)
{
// *** Paste the code here, then run this in jGRASP ***
}
}
For most of the problems all you need to do is look at the output generated when you run the code in jGRASP. It can also be very useful to step through the instructions using the jGRASP's debugger.
Loops With Some Subtle Problems
What is the output of the code shown below?
int count = 1;
while (count <= 10)
System.out.println("A");
Does the modified version of the program, shown below, fix the problem?
int count = 1;
while (count <= 10)
System.out.println("A");
count++;
If we want to print the two lines
A
B
ten times, does the following code accomplish this?
int count = 1;
while (count <= 10)
System.out.println("A");
System.out.println("B");
If not, what needs to be changed?
Here is another loop that is not quite right -- what is the problem with this loop?
int count = 1;
while (count <= 10)
{
System.out.println("A");
System.out.println("B");
count = 11;
}
Exercises
-
What is the output of the following code?
int index = 3;
while (index <= 5)
{
System.out.println("index: " + index);
index++;
}
-
What is the output of the following code?
int num = 2;
while (num < 5)
num++;
System.out.println("num: " + num);
-
What is the output of the following code?
int num = 9;
while (num < 5)
num++;
System.out.println("num: " + num);
-
What is the output of the following code?
int x = 20;
while (true)
{
System.out.println("A");
x++;
}
System.out.println("x is: " + x);
-
What is the output of the following code?
int num = 2;
while (num > 0)
{
System.out.println("num: " + num);
num--;
}
-
What is the output of the following code?
int num = 3;
while (num - 5 < 0)
{
System.out.println("num: " + num);
num++;
}
More Loops With Problems
Each of the loops in this section have some kind of problem. Follow the
code and identify the problem. Also show the output of the code. Note that some of the problems below are related to the fact that Java considers an extra semicolon to be a complete "empty" statement. For example, the main method, below, contains four statements (two calls to the println method and two empty statements):
public class VerifyIt
{
public static void main(String [] args)
{
System.out.println("Hello");
;
System.out.println("There");
;
}
}
The empty statements are not as obvious when they are tacked onto the end of a non-empty statement. The program below is the exact same program as above, except the empty statements are placed at the end of the println statements:
public class VerifyIt
{
public static void main(String [] args)
{
System.out.println("Hello");;
System.out.println("There");;
}
}
-
What is the output of the following code?
int num = 10;
while (num < 10)
{
System.out.println("num: " + num);
num++;
}
System.out.println("done: " + num);
-
What is the output of the following code?
int num = 7;
while (num < 10);
{
System.out.println("num: " + num);
num++;
}
System.out.println("done: " + num);
-
What is the output of the following code?
int num = 9;
while (num < 10)
;
System.out.println("num: " + num);
num++;
System.out.println("done: " + num);
Different Ways To Increment A Loop
Loops can start and stop their "counter" variable at various values, and
increment or decrement the counter by amounts other than one.
-
What is the output of the following code?
int num = 1;
while (num < 4)
{
System.out.println(num);
num += 2;
}
System.out.println("done: " + num);
-
What is the output of the following code?
int num = 6;
while (num > 2)
{
System.out.println(num);
num--;
}
System.out.println("done: " + num);
-
What is the output of the following code?
int num = 6;
while (num > 2)
{
System.out.println(num);
num -= 2;
}
System.out.println("done: " + num);
-
What is the output of the following code?
int num = 100;
while (num < 110)
{
System.out.println(num);
num += 3;
}
System.out.println("done: " + num);
Quick Check
This slightly longer example uses the concepts presented
in the preceding sections.
-
Show the output of the following code. Draw appropriate "memory pictures" of the variables and their contents.
int num = 1, val = 2;
while (num < 3)
{
num = val * 2;
System.out.println(num);
}
while (num > 1)
{
System.out.println(num);
num--;
}
Loops And Method Calls
One of the following problems uses copies of a passed variable, and one problem has methods that all access one shared instance variable.
-
Show the output of the following code. Draw appropriate "memory pictures" of the variables and their contents.
public class DemoLoop2
{
public static void main(String [] args)
{
HasMethods hm = new HasMethods();
hm.fun();
}
}
class HasMethods
{
public void fun()
{
int num = 1;
while (num < 3)
{
other(num);
num++;
}
System.out.println("done: " + num);
}
public void other(int x)
{
System.out.println("other: " + x);
}
}
-
Show the output of the following code. Draw appropriate "memory pictures" of the variables and their contents.
public class DemoLoop3
{
public static void main(String [] args)
{
MoreMethods mm = new MoreMethods();
mm.first();
}
}
class MoreMethods
{
int val = 1;
public void first()
{
while (val <= 5)
{
fnc1();
water();
System.out.println("val: " + val);
}
}
void fnc1()
{
val += 6;
System.out.println("fnc1: " + val);
}
void water()
{
System.out.println("water");
other();
}
void other()
{
val -= 3;
System.out.println("other: " + val);
}
}
|