if statements

Hello

As I write this I realise that my question is most likely not specific to C++, but I encountered the "problem" while writing C++ with openFrameworks.

My question is simple: Why do these two if statements produce different results?

Statement 1 (doesn't work):
if (exploded) {
age += explodeRate;
if (age > timetodie) { exploded = false; }
}

Statement 2 (works):
if (exploded) {
if (age < timetodie) { age += explodeRate; }
else { exploded = false; }
}

I use the variable age to help calculate the size of an explosion. With statement 1 the explosions will forever expand, and with statement 2 they'll stop when I want them to stop.

Errr.. Read that wrong.. Nevermind
Last edited on
In Statement 1, if exploded is true, age is always incremented by explodeRate, whereas in Statement 2, age is only incremented if it less than timetodie.

In Statement 1, if age == timetodie, exploded is not altered.
closed account (z05DSL3A)
In Statement 1, if age == timetodie, exploded is not altered.
In Statement 2, if age == timetodie, exploded is not altered.
Last edited on
@Grey Wolf : huh ?
Statemet 1 :
1
2
3
4
if ( age > timetodie )         // if age == timetodie, it evaluates to false
{
       exploded = false;     // since it is false, exploded is not altered.
}


Statment 2
1
2
3
4
5
6
if  ( age < timetodie )       // if age == timetodie, it evaluates to false and else is executed
       ......
else
{
       exploded = false;    // exploded IS altered
}
@crazyguy101

you are confusing the == operator with the operators < ,> <= ,>=

with statement 1 assuming age is 1 and time to die is 1, because 1 is not greater than 1 the condition evaluates to false , and it skips the body of control statement.
however if he said something like
1
2
3
4
5
if( age >= timetodie) 
{
       return false;
}


then what you said would be true becasuse 1 is greater than or equal to 1


@mandarin
looking at your problem ,what are your test values for age, timetodie, and explodeRate
Last edited on
closed account (z05DSL3A)
crazzyguy101 wrote:
Grey Wolf : huh ?

Long day (week) at work = misreading and misunderstanding. :0)
Topic archived. No new replies allowed.