If statement conditions

Quick tutorial today, but it’s a good thing to remember if you’re an intermediate (up with intermediates!) and are stuck for how to handle some constricts you want on your game.

First of all, basic equal to:

  1. if(variable1==3){
  2. //dosomething
  3. }

And basic not equal to:

  1. if(variable!=3){
  2. //dosomethingelse
  3. }

This not equal to (!) is a good thing to remember, as it’ll come in handy in some situations where the alternatives are a ton of else if statements (which not only are tedious to type, but will slow your game down with too many).

Now we can look at greater than/less than symbols, I’m sure you’ll remember them from maths (be it number lines, or linear programming ;))

Greater than:

  1. if(variable1>3){
  2. //doyetsomethingelse
  3. }

Less than:

  1. if(variable1<3){
  2. //dosomethingelseyetagain
  3. }

These come in handy for gameovers (health, lives, time <0) and level progressions (score >100).

And of course, these can be combined with the not/equal to symbols.

Greater than or equal to:

  1. if(variable1>=3){
  2. //doyetanothersomething
  3. }

Not less than (same as > I suppose, but hey, it adds content to this post!):

  1. if(variable!<3){
  2. //lastthingtodohonest
  3. }

Reference table
== : Equal to
!= : Not Equal to
> : Greater than
< : Less than
>= : Greater than or equal to
<= : Less than or equal to

So there you have it, use your newfound skills at using conditions well!

-->

2 Responses to “If statement conditions”

  1. ZackAttack667 Says:

    This tutorial helped me out a lot. It is the perfect thing for reminding me how all of those equalities or inequalities work. I just started out on a pretty large game project, so this will definitely help me to keep the size and complexity of the coding down. Thanks a lot Tazzydevil and Frozenhaddock!

  2. Tazzydevil XIII Says:

    Glad to help!

Leave a Reply