How do i check wether a int is between two values when comparing it in an if statement, similar to this
if (num >= 1 && num <= 100 && bool != false) {System.out.print(true);}
Thanks.
How do i check wether a int is between two values when comparing it in an if statement, similar to this
if (num >= 1 && num <= 100 && bool != false) {System.out.print(true);}
Thanks.
Well, this looks like it should work. What are you looking for exactly - a more efficient way to do this?
int num =0;
if (num >= 1 && num <= 100) {
//do something
}
bool != false
is very confusing. Not only is it a double negative, but comparing a boolean value to true or false is redundant. Neither does the name give any hint as to its purpose. Something like the following would be clearer.
if(checkRange && (num >= 1 && num <= 100))
That question is quite weird since the answer is in the code you have provided. To check if the int is between two values just use this code.
if(num >= min && num <= max)
The more effective way - when you have multiple intervals is to use Interval Tree.
int j=3;
for (int i=0;i<=10;i++) {
if(j.equals(i)) {
System.out.println("Given value is between 0-10");
}
}