-4

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.

  • 4
    What's the question? Your code seems to already do what you want. – flyx Feb 12 '15 at 11:00
  • http://stackoverflow.com/questions/17095324/fastest-way-in-c-to-determine-if-an-integer-is-between-two-integers-inclusive?rq=1 – phuclv Feb 12 '15 at 12:45

4 Answers4

2

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             
}
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
ling_jan
  • 99
  • 4
1
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))
Neil Masson
  • 2,609
  • 1
  • 15
  • 23
0

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.

Jimmy
  • 276
  • 1
  • 8
0
int j=3;
for (int i=0;i<=10;i++) {
    if(j.equals(i)) {
        System.out.println("Given value is between 0-10");
    }
}
MichaelS
  • 5,941
  • 6
  • 31
  • 46
Vinayak Dwivedi
  • 33
  • 1
  • 1
  • 9