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

2015/7/12

problem with if loop (is loop right im german ?)

jaki jaki

2015/7/12

#
hey i ve a problem
 if (isTouching(bonusleben.class)&&(xb=true)){
            xb=false;
   
            W.removeObjects(W.getObjects(bonuslive.class));
            world.lebenadd();
            getWorld().addObject(new bonuslive(),1000,400);
            getWorld().addObject(new bonuslive(),1200,400);

        }
this if loop should go only onec or ? but if he is touching a bonuslive again its also working although xb=false; pls help me
danpost danpost

2015/7/12

#
The code given could not have been copy/pasted into your post (at least, it seems that is the case). You have non-matching class names, 'bonusleben' and 'bonuslive', as well as three different ways to reference the world, 'W', 'world' and 'getWorld()'. The condition, 'xb=true' in your 'if' statement will always be 'true'; 'xb=true' is setting the value of 'xb' to 'true', not checking to see if 'xb' is 'true'. A double equal sign is used for conditional equality, not a single one. For boolean expressions, you do not even need to compare with '== true'. That is:
if (xb == true);
// is equivalent to
if (xb);

// and
if (xb != true);
// is equivalent to
if (xb == false);
// and equivalent to
if (!xb);
Super_Hippo Super_Hippo

2015/7/12

#
To the question in the title: It isn't a loop. Loops are 'for', 'while' and 'do...while', not 'if'.
jaki jaki

2015/7/13

#
@danpost ok ty @dsper_hippo yeah im german but what is right (if clause) ?
danpost danpost

2015/7/13

#
jaki wrote...
@danpost ok ty
you are welcome.
@dsper_hippo yeah im german but what is right (if clause) ?
'if' is a keyword by itself; The conditional expression is between the round brackets; The 'if' keyword with the conditional expression, together, is the 'if statement', or 'if clause' The code between the curly brackets after the 'if' statement is the 'if code-block'
davmac davmac

2015/7/13

#
yeah im german but what is right (if clause) ?
As for and while statements are examples of loops, an if statement is an example of selection. However the term isn't used much (and seems to have gone out of fashion, actually). People sometimes refer to them as conditional statements. If you're talking specifically about an "if" statement, it's probably best just to call it that: an "if" statement.
You need to login to post a reply.