&& in while

Hello, please does anyone see the mistake, the program should write only those a%i==0 which are bigger then 4 and smaller then 8 (so ,only 6!) Why is it writting all a%i==0, if I have condition inside "while"? Thank a lot

[code]
#include <iostream>
using namespace std;

int main() {
int a=72;

int i=1;
while (i<=a)
{
if((a%i==0)&&(4<i<8))
{
cout<<i<<endl;

}
i++;
}
return 0;
}
< is a binary operator that returns a bool (true or false).

4<i<8 is the same as (4<i)<8.
(4<i) will give you true or false. When you compare a bool with an int, true will be treated as 1 and false as 0, both of which is less than 8 so that means the if condition will always be true.

What you need to do is to split it into two separate expressions and combine them with the && operator: 4 < i && i < 8
Last edited on
MANY THANKS!!!
Topic archived. No new replies allowed.