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

2013/12/11

My for loops are ignored

Vacrin Vacrin

2013/12/11

#
So... ive been trying to do some programming, yet however the for loop never runs, i went through the debugger going step by step, and it skips over the for loop. Any idea?
 public void act() 
    {
        double X,Y;
        X = getX();
        Y = getY();
        Y =(Y * 1.04);
        if(Y > 375){
            System.out.println(Y);
                for(int Z = 0; Z == 15; Z++){
                    Y = Y * .98;
                    System.out.println(Z);
                    setLocation((int)X,(int)Y);
                }
        }
        
        setLocation((int)X,(int)Y);
        
        
    }    
danpost danpost

2013/12/11

#
Before the first run throught the loop the condition 'Z==15' is check; since it is false from the start, because Z is zero, the loop ends.
bourne bourne

2013/12/11

#
Examining the loop, for(int Z = 0; Z == 15; Z++) Z initially is set to 0, then the conditional expression Z == 15 is checked which returns false, exiting the loop. Main point: conditional expression is checked at start as well as after each iteration.
Vacrin Vacrin

2013/12/11

#
Thanks!
You need to login to post a reply.