How to repeat action every x number?

Anyone know how to code this thing? I can code it with if statement but want to score it till 1 million. that nonsense if i make it like below. Please solution for this newbie -.-

   int score;
    
    if(score > 0 && score < 100){
    //condition 1
    }else if(score >100 && score < 200){
    //condition 2
    }else if(score >200 && score < 300){
    //condition 1
    }else if(score >300 && score < 400){
    //condition 2
    } //repeat this to 1 million

Modulo ( % ) is probably the way to go.

Modulo is a mathematical operation that restricts an integer to the range of 0 to mod-1, in other words, the result is the remainder of a division. It’s an important and very interesting “tool”, e.g. cryptography often relies on it (in a more complex way of course) in its important steps.

Some simple examples:

  • 55 % 100 = 55
  • 124 % 100 = 24
  • 36 % 5 = 1
  • 30 % 5 = 0
  • 0 % any integer (except 0 of course) is always 0
  • any integer % 1 is always 0

Note, that it works a bit differently with negative numbers, at least in mathematics. Programming languages like C#, Java, … often treat negative numbers just like positive numbers when using modulo.

In many programming languages, (-7)%10 will be 7, in number theory (modular arithmetic), the result is 3. I’m not sure why programming languages use this “wrong” implmentation, who knows, maybe there’s a reason for it.

Back to your question, it could be something along these lines

(you left out every (k100)th number, so I treat them seperately*)

int tmp = score % 200;
if(tmp == 0 || tmp == 100) //could also be written as " if(tmp % 100 == 0) "
{
    // do stuff when score is 0, 100, 200, 300, 400, 500, 600 
}
else if(tmp < 100)
{
     // score is 1 - 99, 201 - 299, 401 - 499, ....
     // condition 1
}
else
{
     // score is 101 - 199, 301 - 399, 501 - 599, ...
     // condition 2
}

Assuming you’re doing the same action every time you hit over a new multiple of a hundred.

Would something like this work for you?

//Initialization
int triggers = 0;
int score;

//Inside loop    
if( (score / 100) >= triggers ) {

//Do action
triggers++;

}

It looks like you want to alternate between condition 1 and condition 2, every 100 score points. Assuming this is correct:

//first divide by your range
int score_divided_by_100 = score/100;
//now determine if the result is an odd or even number
if ( score_divided_by_100 % 2 == 0)  // the % is the modulo operator, which yields the remainder of a division operation
{
//condition 1
}
else
{
//condition 2
}

You would HAVE to use another “if” statement to check that the score does not exceed one million.

Here’s an alternative version off the top of my head (not tested) for less numbers if you ever need to add more specific conditions later on in addition to other answers.

    private int score = 500;
    private int scoreRange = 100;

    private void Awake() {
        // Call with as many values as you want, checks against all of them and returns true if score is in the range of any of them, for example below values are 0-100, 200-300, 400-500, etc.
        Debug.Log(IsScoreInRange(100, 300, 500, 700, 900, 1100, 1300, 1500));
    }

    private bool IsScoreInRange(params int[] limits) {
        foreach(var limit in limits) {
            var remainder = limit % score;
            if(remainder == 0 || remainder < (limit - scoreRange)) return true;
        }

        return false;
    }