# Trying to understand how to use % as an operator

Please take a moment to consider if this thread is worth bumping.

## Recommended Posts

So i'm writing a script for a hud, and I think I got the right understanding of how the % sign works as an operator

integer num = 1200;

integer remainder = num % 1000;//would return 200, right?

I have this snippet that I'm trying to calculate the next experience level based on different differences between levels after certain experience levels, if that makes sense

look at http://www.7seasfishing.com/stats.html so you can see what experience levels I mean

you can see that the difference between the first few levels is 40, 60 and 130, not too complicated

after 500, the level raises every 500 until 5000, then it's every 1000 until 14000, then every 2000 up to 40000 (was told it's every 10000 after that)

On the following snippet, I'm getting an error that reads "Not all code paths return a value", but I can see what I need to fix

```integer getnextxp()
{
if((exp < 5000)&&(exp > 250))
{
integer subtr = (exp + 500) % 500;
integer num = (exp + 500) - subtr;
return num;
}
if((exp < 250)&&(exp > 120)){return 250;}
if((exp < 120) && (exp > 60)){return 120;}
if((exp < 60)&&(exp > 20)){return 60;}
if(exp < 20){return 20;}
if(exp > 50000)
{
integer subtr = (exp + 10000) % 10000;
integer num = (exp + 10000) - subtr;
return num;
}
if((exp > 5000)&&(exp < 14000))
{
integer subtr = (exp + 1000) % 1000;
integer num = (exp + 1000) - subtr;
return num;
}
if((exp > 14000)&&(exp < 50000))
{
integer subtr = (exp + 2000) % 2000;
integer num = (exp + 2000) - subtr;
return num;
}
}```
##### Share on other sites

nevermind, i figured it out. instead of having the return function in each of the if statements, i moved it to the end, and had each if statement calculate it

`integer getnextxp(){    integer num;    if((exp < 5000)&&(exp > 250))    {        integer subtr = (exp + 500) % 500;        num = (exp + 500) - subtr;    }    if((exp < 250)&&(exp > 120)){return 250;}    if((exp < 120) && (exp > 60)){return 120;}    if((exp < 60)&&(exp > 20)){return 60;}    if(exp < 20){return 20;}    if(exp > 50000)    {        integer subtr = (exp + 10000) % 10000;        num = (exp + 10000) - subtr;    }    if((exp > 5000)&&(exp < 14000))    {        integer subtr = (exp + 1000) % 1000;        num = (exp + 1000) - subtr;    }    if((exp > 14000)&&(exp < 50000))    {        integer subtr = (exp + 2000) % 2000;        num = (exp + 2000) - subtr;    }    return num;}`
##### Share on other sites

i also changed some of the tests to >= instead of just >, as it was returning 0 if it was equal to

I also noticed they had some level errors, and added in tests for those as well

`integer getnextxp(){    integer num;    if((exp < 5000)&&(exp >= 250))    {        integer subtr = (exp + 500) % 500;        num = (exp + 500) - subtr;    }    if((exp < 250)&&(exp >= 120)){return 250;}    if((exp < 120) && (exp >= 60)){return 120;}    if((exp < 60)&&(exp >= 20)){return 60;}    if(exp < 20){return 20;}    if(exp > 50000)    {        integer subtr = (exp + 10000) % 10000;        num = (exp + 10000) - subtr;    }    if((exp >= 5000)&&(exp < 14000))    {        integer subtr = (exp + 1000) % 1000;        num = (exp + 1000) - subtr;    }    if((exp >= 14000)&&(exp < 50000))    {        integer subtr = (exp + 2000) % 2000;        num = (exp + 2000) - subtr;    }    if((exp < 37000)&&(exp >= 36000)){return 37000;}    if((exp < 40000)&&(exp >= 37000)){return 40000;}    return num;}`

##### Share on other sites

A bit overcomplicated. The usage of "else" simplifies this kind of chained compares alot.

`integer getnextxp(){	integer num;		if (exp<20) num=20;	else if (exp<60) num=60;	else if (exp<120) num=120;	else if (exp<250) num=250;	else if (exp<5000) num=(exp/500+1)*500;	else if (exp<14000) num=(exp/1000+1)*1000;	else if (exp<36000) num=(exp/2000+1)*2000;	else if (exp<37000) num=37000;	else if (exp<40000) num=40000;	else if (exp<50000) num=(exp/2000+1)*2000;	else num=(exp/10000+1)*10000;		return num;}`
• 1
##### Share on other sites

Thanks, that works perfectly. I knew mine was a bit jumbled, was going to work on organizing it after getting it to work. Guess I didn't think about it rounding when casting a division to an integer, or dividing integers