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

2015/6/20

should I use if or while

whitestag whitestag

2015/6/20

#
I got this problem since a few days. I'm doing a game for my final exams and I have to present it infront of a comittee. My teacher said that I should try to build in at least one while, for and do while loop. But this is actually a huge problem for me . I had the whole game together only with if statements. Now I'm actually forcing in some while and for loops but I don't think they make sense there. Could somebody please check if they make sense there because if they ask me why I put it there I just can't say because you wanted me to :D here for example in my opinion if would be much better
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
zaehler++;
 
        while (zaehler == k){
            Actor Burg = new Burg();
            //Schloss erstellen
          
            GreenfootImage image= Burg.getImage();
             
            //Bildbereich
            addObject(Burg, getWidth(), Greenfoot.getRandomNumber(116)+200);
             
             
             
            // Abstand zwischen den Schlössern
           
            
           zaehler= 0;
           k= Greenfoot.getRandomNumber(100)+20;
             
}
its the same as when I write
1
2
3
while(Greenfoot.isKeyDown("up")== true){
           dy = fliegen;
       }
it just doesn't feel right for me as I would only use while if it would make sense to loop it. I think this one is the only part were it would make a little bit of sense but actually if it happend once the game stops so no sense
1
2
3
while( getY() > getWorld().getHeight()) {
           GameOveranzeigen();
        }
1
2
3
4
5
6
7
8
public void GameOveranzeigen()
    {
        Game_Over Game_Over = new Game_Over();
        getWorld().addObject(Game_Over, getWorld().getWidth()/2, getWorld().getHeight()/2);
         
        Greenfoot.stop();
     
}
It drives me crazy that I build them in but don't see the sense in them could somebody please help me to get maybe an idea for a do while or for. In my game there is a unicorn which falls down if you don't press the up button. You have to collect little images so the score gets up and there are some Castles and Clouds you have to avoid. Any idea just for where it could be actually usefull to build one in?
miningmanna miningmanna

2015/6/20

#
If your using your methods in the act method you should use if statements and no whiles, because the act() method updates every frame
danpost danpost

2015/6/21

#
At least in the first example, you are changing the values of 'k' and 'zaehler' to different values inside the block -- so, the loop will be exited after the first iteration. With the 'isKeyDown' example, your program will appear to 'freeze' until the key is released. The loop will continue to execute as long as the key is being pressed, not giving any other code a chance to execute in the meantime. In the 'game over' example, you will end up being stuck in the loop permanently and one Game_Over object will be placed on top of of another Game_Over object continuously until you get an 'out of memory (or Java Heap Space) exception. The condition for the 'while' loop to execute is not changed to exit the loop.
danpost danpost

2015/6/21

#
If you have more than one 'for' loop in the program, you should be able to convert one of them to a 'while' loop easily enough. For example:
1
2
3
for (int n=0; n<10; n++) {
    // some code
}
can be converted to this
1
2
3
4
5
int n = 0;
while (n < 10) {
    // some code
    n++
};
The 'do-while' loop is a bit different than the 'while' and 'for' loops in that the condition to continue the loop is checked at the end of the block instead of at the beginning. So one iteration of the block must be allowed to execute. It would be alright to convert the above into a 'do-while' loop because we know that we need the code to execute at least once (ten times, actually):
1
2
3
4
5
int n = 0;
do {
    // some code
    n++;
} while (n < 10);
If we were using variable values to control the loop and we did not know for sure that one loop will always be necessary, then this type of loop would not be the right choice. I guess what you should have asked in the title is 'Should I use for or while' (not 'if' or 'while') because 'if' is not a loop and should probably not be compared to one.
You need to login to post a reply.