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

2016/6/19

for loop

smcgee smcgee

2016/6/19

#
1
2
3
4
5
6
7
private void example()
{
int j=10
for (int i=0; i<10; i++)
{
j=j – i;
}
Can you tell me what the following code would do to the values of i and j?
danpost danpost

2016/6/19

#
Well, 'i' starts at zero and 'j' at 10. So, since 'i' increments at the end of each cycle of the loop and 'j' decrements inside the loop, when the condition for the loop to run a cycle ( 'i<10') is checked the sum of 'i' and 'j' will always be ten (the difference starts at 10 and the rate of change in each is one unit, but in opposite directions, offsetting each other).
SPower SPower

2016/6/19

#
@danpost Actually, it wouldn't. j decreases by i each cycle, so you'd get the pairs (10, 0), (9, 1), (7, 2), (4, 3), etc. @smcgee A simple way to figure out what's happening to the values overtime, you can log the values by adding this line into the loop (after changing the value of j):
1
Systm.out.println("i: "+i+" j: "+j);
danpost danpost

2016/6/19

#
@SPower, I though that 'i' at the end of line 6 was a '1'. However, the pairs would then be using (i, j) pairings: (0, 10), (1, 10), (2, 9), (3, 7), (4, 4), (5, 0), (6, -5), (7, -11), (8, -18), (9, -26), (10, -35) or: j = 10 - i * ( i - 1 ) / 2
SPower SPower

2016/6/19

#
@danpost, no, it wasn't. Line 6 reads (copy pasted): j=j – i;
danpost danpost

2016/6/19

#
SPower wrote...
@danpost, no, it wasn't. Line 6 reads (copy pasted): j=j – i;
Yes, I understand that now -- and at the time of my last posting.
SPower SPower

2016/6/19

#
Sorry, I misread your reply. Turns out I'm capable of that too :/. Sorry again.
danpost danpost

2016/6/19

#
SPower wrote...
Sorry, I misread your reply. Turns out I'm capable of that too :/. Sorry again.
No need to be sorry. No human is perfect -- and nothing will be held against you here. I do appreciate the correction, at any rate.
smcgee smcgee

2016/6/20

#
Does this mean it is an infinite loop?
smcgee smcgee

2016/6/20

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void act()
    {
       exampleAgain(); // Add your action code here.
    }  
 
    private void exampleAgain()
    {
        int j=10;
        for (int i=0; i<10; i++)
        {
            j=j-i;
            getWorld().addObject(new Bee(), 200, 200);
        }
    }
I tested out the code using an addObject method but it does not seem to do anything. What am I missing in this code? Thanks -
danpost danpost

2016/6/20

#
smcgee wrote...
Does this mean it is an infinite loop?
Absolutely not. The loop will iterate ten times as per the value of 'i'.
I tested out the code using an addObject method but it does not seem to do anything. What am I missing in this code?
You probably only see one Bee object added into the world per act, where actually ten are added, one over top of another. However, you appear to be calling the method every act cycle, unconditionally. This will mean the loop will complete about 50 times per second; placing about 500 Bee objects in the world per second. In what class is this code located and why would you want to continuously add 10 Bee objects in the world per act cycle? I foresee lagging and possibly an OutOfMemoryException happening.
smcgee smcgee

2016/6/20

#
Thank you for that explanation - very helpful.
You need to login to post a reply.