Vince
Vince

Reputation: 39

How to check if a variable is between 0 and 100?

I am making something in C# that requires the variable g to be between 0 and 100. When g is less than 100, one action must be performed But when it is 100 it needs to perform a different action. Here is the code. It is always displaying the same thing.

if (g > 0 || g < 100) {
            name = "Working";

        }else {
            name = "Done";
        } 

Any help would be greatly appreciated.

Upvotes: 1

Views: 911

Answers (2)

MPelletier
MPelletier

Reputation: 16687

You can try this:

if (g >= 0 && g < 100)
{
    name = "Working";
}
else if (g == 100)
{
    name = "Done";
}
else
{
    name = "what";
}

if clauses can be nested in this way, even several times, just after else. It is often done if there are many checks or checks on other variables in the if clause.

Upvotes: 9

aquinas
aquinas

Reputation: 23796

You're saying between 0 and 100 in your question, but your code says OR. In other words, is g > 0 or is it less than 100? That's ALWAYS true. Change your || to an &&. Also, you probably want this:

if (g >=0 && g <100){
 name="Working";
}
else
  name="done";
}

Upvotes: 5

Related Questions