Thread: 'if' 'alway's true.

1. 'if' 'alway's true.

Hello all, i have a problem with an 'if' statement alway's being true. This is what i'm working on...

Code:
```if (totalpoints > 150)  //when totalpoints exceed's 350, this need's to be false
{
printf("\n\n\nYou have earned %d points, you are at level 2\n\n\n", totalpoints);
gl_c = 2;
}
else if (totalpoints > 350)
{
printf("\n\n\nYou have earned %d points, you are at level 3\n\n\n", totalpoints);
gl_c = 4;
}```
I've seen some interesting 'if' and 'else if' statement's before, what can i look for to increase my knowledge of these statement's, boolean algebra?
Any help would be much appreaciated.
Thank's.

2. As all numbers over 350 are also more than 150, the second boolean expression will never work.......

Try this..

Code:
```if (totalpoints > 150 &&  totalpoints < 350)  //when totalpoints exceed's 350, this need's to be false
{
printf("\n\n\nYou have earned %d points, you are at level 2\n\n\n", totalpoints);
gl_c = 2;
}
else if (totalpoints > 350)
{
printf("\n\n\nYou have earned %d points, you are at level 3\n\n\n", totalpoints);
gl_c = 4;
}```

3. Code:
```if (totalpoints > 150)  //when totalpoints exceed's 350, this need's to be false
{
printf("\n\n\nYou have earned %d points, you are at level 2\n\n\n", totalpoints);
gl_c = 2;
}
else if (totalpoints > 350)
{
printf("\n\n\nYou have earned %d points, you are at level 3\n\n\n", totalpoints);
gl_c = 4;
}```
Remove the 'else' part

OR

Code:
```if (totalpoints > 150 && totalpoints < 350)  //when totalpoints exceed's 350, this need's to be false
{
printf("\n\n\nYou have earned %d points, you are at level 2\n\n\n", totalpoints);
gl_c = 2;
}
else if (totalpoints > 350)
{
printf("\n\n\nYou have earned %d points, you are at level 3\n\n\n", totalpoints);
gl_c = 4;
}```

4. Thank's alot, i'm not quite sure how you learned that, i guess i do a search for boolean expression and see what i come up with.

5. And what if totalpoints is 350 ???
Code:
```if (totalpoints > 150 && totalpoints <= 350)  //when totalpoints exceed's 350, this need's to be false
{
printf("\n\n\nYou have earned %d points, you are at level 2\n\n\n", totalpoints);
gl_c = 2;
}
else if (totalpoints > 350)
{
printf("\n\n\nYou have earned %d points, you are at level 3\n\n\n", totalpoints);
gl_c = 4;
}```

6. Or you could change the places of comparison.

Code:
```/* totalpoints -> <350,...> */
if (totalpoints > 350)
{
printf("\n\n\nYou have earned %d points, you are at level 3\n\n\n", totalpoints);
gl_c = 4;
}
/* totalpoints -> <150,350] */
else if (totalpoints > 150)
{
printf("\n\n\nYou have earned %d points, you are at level 2\n\n\n", totalpoints);
gl_c = 2;
}```

7. Anyone searching for more info on boolean expression's look here
I got this site from google.

Popular pages Recent additions