Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
CSC71001 Module 8 - Page 1
Module 8
More about Loops
Introduction
Note that it is assumed that you have completed the work up until this point, and are familiar
with creating objects, adding code, using setImage and other Greenfoot class and object
methods. If you have not completed the work up until now, please go back and make sure you
are familiar with it all, before going further.
This week we will not be using your textbook. We will instead be working on some example files
to concentrate on the mechanics of looping and using loops to produce various results.
Study materials
Materials required:
? The free Greenfoot programming environment;
? Files in "Module08" folder on MySCU site;
? Internet access to access your lecture, and any other material.
CSC71001 Module 8 - Page 2
Objectives
On completion of this topic, you should be able to:
? create and use for loops
? create and use constant values
? create use nested loops (loops within loops)
? build different patterns of objects in the world, using loops and other coding constructs
Concepts
? for loops
? initialisation
? terminating condition
? increment
? decrement
? variable++
? variable--
? constant
? static final
CSC71001 Module 8 - Page 3
Building an army
Before you start
Watch 'Concept Video 8-1 Review of Module 7
Our purpose this week will be to build an “army” of objects in various configurations, using loops.
Along the way, we will use various concepts that you have encountered in the previous modules,
as well as new concepts.
Activity 8-1 A sample scenario
Open the scenario 8-1.gfar from the MySCU site.
Write down the classes in the scenario:
____________________________________________________________________________
Open the Alien class in the editor. Fill out the comment at the top of the class. You will notice that
this class doesn't do anything (yet).
Open the SpaceWorld class. Change the comment at the top of the class to your name and your
version number.
Adding an alien
The first step is to add some aliens into our scenario.
To add objects to our world, we use the addObject method inherited from the World class.
If you check the Greenfoot documentation (Help Menu -> Greenfoot Class Documentation), you
will see the signature of the addObject method:
addObject(Actor object, int x, int y)
We can add a new Alien object at a random location in the world using the following code:
addObject(new Alien(), Greenfoot.getRandomNumber(600),
Greenfoot.getRandomNumber(400));
Activity 8-2 Adding an object
Using the scenario from Activity 8-1, add code in the SpaceWorld constructor to add one object in
a random location in the world.
Test by pressing the reset button several times.
CSC71001 Module 8 - Page 4
Adding more aliens
We could now add more aliens manually, by adding more lines of code (repeating them). There is
a simpler way to do this – a loop. If you remember in the last module we looked at while loops,
which repeat until a particular condition is true.
While loops have an initialisation, a condition that will terminate the loop when it becomes false
and an increment or decrement that will change the loop counter/variable.
A while loop always looks like this:
//declaration of a loop variable
while (condition)
{
// activity of the loop
// incrementing or decrementing the loop variable
}
An example is:
int i = 0;
int sum = 0;
while (i < 6)
{
sum = sum + i;
i = i + 1;
}
Activity 8-3
1. What would the value of sum be at the end of this loop? _____________
2. What would the value of i be at the end of this loop? ______________
3. How many times is the body of the loop executed? _______________
If we wanted to use a while loop to create a number of objects, we could combine the code that
we have used in Activity 8-2, with a while loop.
Activity 8-4 Adding several random aliens using a while loop
Using the scenario from Activity 8-2, alter the SpaceWorld constructor with a while loop to add 8
aliens in random locations in the world.
Test by pressing the reset button several times.
Watch 'Concept Video 8-2 Debugging While Loops
CSC71001 Module 8 - Page 5
Increments and Decrements
Until now, we have been adding one to the current value of a variable using:
i = i + 1;
We have been deleting one from the current value of a variable using:
i = i – 1;
Looking more carefully at this, we can see that we are querying the current value of a particular
variable, adding (or subtracting) one to it, and then storing the result in the same variable,
overwriting the previous value. This activity is so common, that there is a shortcut provided in
Java and many other languages for this operation.
i = i + 1; can be shortened to i++;
i = i – 1; can be shortened to i—-;
Our code in the SpaceWorld constructor can be shortened to:
int alienCount = 0;
while (alienCount < 8)
{
addObject(new Alien(), Greenfoot.getRandomNumber(600),
Greenfoot.getRandomNumber(400));
alienCount++;
}
Activity 8-5 Adding several random aliens using shortened code
Using the scenario from Activity 8-4, alter the SpaceWorld constructor while loop to use the simpler
increment. Test that it is still working correctly by pressing the reset button several times.
We can now refactor this code by creating a new SpaceWorld method called "createAliens".
Create a new method createAliens that does not return anything, and does not have any
parameters. Place the alien creation code in this method, and call it from the SpaceWorld
constructor. Test your code with the reset button several times.
CSC71001 Module 8 - Page 6
Many things can go wrong with While loops. If we forget to initialise the loop variable, or forget
to increment the loop variable, we can easily end up with an infinite loop, or one that does not
execute at all.
Next we are going to look at a different type of loop – a FOR loop. If you understand the
mechanics of the WHILE loop, then you shouldn’t have any trouble with the FOR loop.