This site requires JavaScript, please enable it in your browser!
Greenfoot back
rockon411
rockon411 wrote ...

2017/3/30

Testing a For Loop

rockon411 rockon411

2017/3/30

#
I have a for loop
for (Object obj : getNeighbors(1, true, Minnow.class)) 
        {
            getWorld().remove((Minnow)obj);
        }
and I was wondering how to test it. My understanding of loops is kind of fuzzy, so I don't know how to set up a test and any help would be greatly appreciated.
danpost danpost

2017/3/30

#
What exactly would you be testing it for? what information should the test provide ?
rockon411 rockon411

2017/3/30

#
Ok so I literally don't understand loops at all but for method with if statements or while statements we have to make sure we set up the situation to test both options. Ex if this is the method
public void go()
{
    if (sky = blue)
    {
        move();
    }
}
Then I'll have to test methods
public void testGo1()
{
    sky = blue;
    move();
}

public void testGo2()
{
    sky = yellow; 
    move();
}
So this is what my beginner brain understands. An explanation of what the for loop means would probably help.
danpost danpost

2017/3/30

#
If you have done while statements, then you can do a for statement. The following two codes are equivalent:
int i;
for (i = 0; i<10; i++)
{
    // whatever
}

// and
int i=0;
while (i < 10)
{
    // whatever
    i = i + 1;
}
rockon411 rockon411

2017/3/30

#
Thanks!
HemantoPaul HemantoPaul

2017/3/31

#
How do i use loop for a Hungry-wombat to move and eatLeaf at the same time if there are leaves consecutively?
Nosson1459 Nosson1459

2017/3/31

#
HemantoPaul wrote...
How do i use loop for a Hungry-wombat to move and eatLeaf at the same time if there are leaves consecutively?
Start a new discussion and explain yourself there.
You need to login to post a reply.