Is there a way to break out of an if statement without "hacking the code"?

Started by
16 comments, last by Satharis 9 years, 12 months ago
Even taking a quick peek at the code I'd say you can probably significantly streamline the branching just by following the advice about breaking some of the code into more functions. Leveling up for example could quite easily be a function in itself.
Advertisement
Have to agree; what you really want here is a state machine, or at least several functions.
Part of your problem is that you're trying to handle event-based stuff in a polling update loop. You can do it that way, but it will make your life much easier if you break things like incrementing XP into their own function. That way, instead of checking every time it goes through the input loop, it'll only get called when the character actually needs to check for leveling up.
[source lang="java"]
public void levelUp() {
if(level >= maxLevel) {
return;
}
int hp_per_level; // defining this so it's not a magic number
maxHp = min(maxHp + hp_per_level, maxHpIncrease);
life = min(life + hp_per_level, maxHp);
levelUpLogo.play();
}
// Call this whenever your character adds XP
public void addXP(int exp_to_add) {
// check for leveling
int xp_per_level = 15; // defining this so it's not a magic number
exp += exp_to_add;
if (exp > xp_per_level) {
levelUp(); // level up
exp = 0; // this resets exp to 0 after every level, you may want to have it instead be: exp -= xp_per_level;
}
}
[/source]
Obviously the correct way to fix this is to refactor your code. You shouldn't need to exit an if without it completing but if you really wanted to do it the hacky way in Java you can use a labeled break which is effectively a goto.

The only place I've seen labeled breaks actually used in production code was when doing J2ME ports of megadrive games where we had the original ASM code in comments and then the Java was a line by line reimplementation including jumps and inclined data.

Have to agree; what you really want here is a state machine, or at least several functions.

Part of your problem is that you're trying to handle event-based stuff in a polling update loop. You can do it that way, but it will make your life much easier if you break things like incrementing XP into their own function. That way, instead of checking every time it goes through the input loop, it'll only get called when the character actually needs to check for leveling up.

[source lang="java"]
public void levelUp() {
if(level >= maxLevel) {
return;
}
int hp_per_level; // defining this so it's not a magic number
maxHp = min(maxHp + hp_per_level, maxHpIncrease);
life = min(life + hp_per_level, maxHp);
levelUpLogo.play();
}

// Call this whenever your character adds XP
public void addXP(int exp_to_add) {
// check for leveling
int xp_per_level = 15; // defining this so it's not a magic number
exp += exp_to_add;

if (exp > xp_per_level) {
levelUp(); // level up
exp = 0; // this resets exp to 0 after every level, you may want to have it instead be: exp -= xp_per_level;
}
}
[/source]


Interesting, I notice a return statement that returns no value in the if statement. What or where will the code be when the code reaches return.

Interesting, I notice a return statement that returns no value in the if statement. What or where will the code be when the code reaches return.


That's a very confused question. `return' quits the function, whether it returns with a value or without it. The execution will continue wherever the function was called.

Interesting, I notice a return statement that returns no value in the if statement. What or where will the code be when the code reaches return.


That's a very confused question. `return' quits the function, whether it returns with a value or without it. The execution will continue wherever the function was called.

Specifically, addXP will be called [em]first[/em]. If exp is greater than 15, it will call levelUp(). The levelUp() function will check to see if maxLevel has been reached. If it has, it will return from there with no return value because these are void functions. The return takes it back to the addXp() function at the point it was called, and the next line is executed, in this case setting exp to 0.

Step through your running code in a debugger, it will give you a much better idea of what is going on under the hood.

Interesting, I notice a return statement that returns no value in the if statement. What or where will the code be when the code reaches return.


That's a very confused question. `return' quits the function, whether it returns with a value or without it. The execution will continue wherever the function was called.

Oh Okay. That was the answer I was looking for. Apologies for rewording the question incorrectly.

Oh Okay. That was the answer I was looking for. Apologies for rewording the question incorrectly.


Just think of it like returning a value, at any point in your code a function might return a bool or something, you've probably seen or used code like this before.

if (somecondition)
{
    dosomestuff;
}
else
{
    otherstuff;
    if (someothercondition)
    {
        return true;
    }
}

return false;
In a void function you can just return without a value, so for all intents and purposes it basically works like a break statement out of the function.

This topic is closed to new replies.

Advertisement