why can i type-- if(a>5)... instead of if(10>a>5)



 DEVELOP > c-Plus-Plus > why can i type-- if(a>5)... instead of if(10>a>5)

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 18 Oct 2006 06:08:22 AM
Object: why can i type-- if(a>5)... instead of if(10>a>5)
The following is correct...
#include <iostream>
using namespace std;
int main()
{
double BMI,height,weight;
cout<<"Welcome to the BMI calculator!\n\n";
cout<<"Enter your weight(kilos): ";
cin>>weight;
cout<<"Enter your height(meters): ";
cin>>height;
BMI =3D weight/height/height;
cout<<"BMI: "<<BMI<<endl ;
if ( BMI > 34.9)
cout<<"=E9=87=8D=E5=BA=A6=E8=82=A5=E8=83=96";
else
if (BMI>29.9)
cout<<"=E4=B8=AD=E5=BA=A6=E8=82=A5=E8=83=96";
else
if (BMI>26.9)
cout<<"=E8=BC=95=E5=BA=A6=E8=82=A5=E8=83=96";
else
if (BMI>23.9)
cout<<"=E9=81=8E=E9=87=8D";
else
if (BMI>=3D18.5)
cout<<"=E9=81=A9=E4=B8=AD";
else
cout<<"=E9=81=8E=E8=BC=95";
system("pause");
return 0;
}
The following is wrong...
#include <iostream>
using namespace std;
int main()
{
double BMI,height,weight;
cout<<"Welcome to the BMI calculator!\n\n";
cout<<"Enter your weight(kilos): ";
cin>>weight;
cout<<"Enter your height(meters): ";
cin>>height;
BMI =3D weight/height/height;
cout<<"BMI: "<<BMI<<endl ;
if ( BMI > 34.9)
cout<<"=E9=87=8D=E5=BA=A6=E8=82=A5=E8=83=96";
else
if (34.9>=3DBMI>29.9)
cout<<"=E4=B8=AD=E5=BA=A6=E8=82=A5=E8=83=96";
else
if (29.9>=3DBMI>26.9)
cout<<"=E8=BC=95=E5=BA=A6=E8=82=A5=E8=83=96";
else
if (26.9>=3DBMI>23.9)
cout<<"=E9=81=8E=E9=87=8D";
else
if (23.9>=3DBMI>=3D18.5)
cout<<"=E9=81=A9=E4=B8=AD";
else
cout<<"=E9=81=8E=E8=BC=95";
system("pause");
=20
return 0;
}
BUT WHY CAN'T THE SECOND ONE FUNCTION??
.

User: "Jim Langston"

Title: Re: why can i type-- if(a>5)... instead of if(10>a>5) 18 Oct 2006 05:41:48 PM
<jienweiwu@yahoo.com.tw> wrote in message
news:1161169702.808425.203530@f16g2000cwb.googlegroups.com...
The following is correct...
#include <iostream>
using namespace std;
int main()
{
double BMI,height,weight;
cout<<"Welcome to the BMI calculator!\n\n";
cout<<"Enter your weight(kilos): ";
cin>>weight;
cout<<"Enter your height(meters): ";
cin>>height;
BMI = weight/height/height;
cout<<"BMI: "<<BMI<<endl ;
if ( BMI > 34.9)
cout<<"????";
else
if (BMI>29.9)
cout<<"????";
else
if (BMI>26.9)
cout<<"????";
else
if (BMI>23.9)
cout<<"??";
else
if (BMI>=18.5)
cout<<"??";
else
cout<<"??";
system("pause");
return 0;
}
The following is wrong...
#include <iostream>
using namespace std;
int main()
{
double BMI,height,weight;
cout<<"Welcome to the BMI calculator!\n\n";
cout<<"Enter your weight(kilos): ";
cin>>weight;
cout<<"Enter your height(meters): ";
cin>>height;
BMI = weight/height/height;
cout<<"BMI: "<<BMI<<endl ;
if ( BMI > 34.9)
cout<<"????";
else
if (34.9>=BMI>29.9)
cout<<"????";
else
if (29.9>=BMI>26.9)
cout<<"????";
else
if (26.9>=BMI>23.9)
cout<<"??";
else
if (23.9>=BMI>=18.5)
cout<<"??";
else
cout<<"??";
system("pause");
return 0;
}
BUT WHY CAN'T THE SECOND ONE FUNCTION??
Becuase like someone said:
if ( 23.9 >= BMI >= 18.5 )
will become:
if ( ( 23.9 >= BMI) >= 18.5 )
if ( ( true ) >= 18.5 )
oops, can't compare the boolean true to a number.
Again, like others have said, you want to use the && staement.
23.9 >= BMI >= 38.5
is saying, if 23.9 is greater than BMI AND BMI is greater than 38.5 so write
it that way.
if ( 23.9 >= BMI && BMI >= 38.5 )
Now you are comparing two bools.
if ( ( 23.9 >= BMI) && ( BMI >= 38.5) )
if ( (true) && (true ) )
and we can and two boolean values.
Also, as again, as someone has mentined, put your if statement on the same
line as your else so the indenting doesn't go way off.
if ( BMI > 34.9)
cout<<"????";
else if (34.9>=BMI && BMI > 29.9)
cout<<"????";
else if (29.9>=BMI && BMI > 26.9)
cout<<"????";
else if (26.9>=BMI && BMI >23.9)
cout<<"??";
else if (23.9>=BMI && BMI >=18.5)
cout<<"??";
else
cout<<"??";
There, isn't that much easier to read?
Now, given that, your logical statements are a bit off. Because in the
first if you have already asked if BMI is greater than 34.9, the else if
already knows that BMI has to be less than 34.9 so you dont' need to check
for it again. Same with the others.
.

User: "Bart"

Title: Re: why can i type-- if(a>5)... instead of if(10>a>5) 18 Oct 2006 07:03:48 AM
wrote:

The following is correct...
#include <iostream>
using namespace std;
int main()
{
double BMI,height,weight;

cout<<"Welcome to the BMI calculator!\n\n";

cout<<"Enter your weight(kilos): ";
cin>>weight;

cout<<"Enter your height(meters): ";
cin>>height;

Better check your input.

BMI =3D weight/height/height;

Better avoid code where operator associativity can make things
confusing.
BMI =3D weight / (height * height);

cout<<"=E9=87=8D=E5=BA=A6=E8=82=A5=E8=83=96";

Better avoid confusing users by prompting them in English and giving an
answer in Chineese.

if ( BMI > 34.9)
cout<<"=E9=87=8D=E5=BA=A6=E8=82=A5=E8=83=96";
else
if (34.9>=3DBMI>29.9)
cout<<"=E4=B8=AD=E5=BA=A6=E8=82=A5=E8=83=96";

You can write if (BMI <=3D 34.9 && BMI > 29.9)
But since this is in an else clause you don't need to recheck the first
condition and it is enough to write if (BMI > 29.9)
Also it is customary to indent long else-if lists like this:
if(...)
...
else if(...)
...
else if(...)
...
else
...
Regards,
Bart.
.

User: "F.J.K."

Title: Re: why can i type-- if(a>5)... instead of if(10>a>5) 18 Oct 2006 07:05:24 AM
wrote:

if (26.9>=BMI>23.9)
BUT WHY CAN'T THE SECOND ONE FUNCTION??

Because boolean operators are secondary operators, not ternary
operators.
So you wrote the equivalent of:
bool condition1 = (26.9>=BMI);
bool condition2 = (static_cast<double>(condition1)>=23.9);
if (condition2) {
//...
//this is unreachable code, because condition1 is either true or
false and will be
//converted to 1.0 resp. 0.0, which is both NOT >=23.9
}
You should really read a good textbook on the language basics, it will
help you safe so much time in the long run. Of course you should also
read advanced textbooks like Scott Meyers "Effective C++", but first
you need to learn some basic syntax for beginners.
.

User: "Ron Natalie"

Title: Re: why can i type-- if(a>5)... instead of if(10>a>5) 18 Oct 2006 07:12:44 AM
wrote:

}
BUT WHY CAN'T THE SECOND ONE FUNCTION??

C++ relational operators don't work the way you think.
A < B < C
The result of a relational operator is the boolean
value true or false.
The above expression is two relational operators, effectively
bool temp = A < B;
bool answer = temp < C;
So the second part of the expression compares true or false from
the first half with the value of C. Since bool is another
numeric type, this will work if C is a numeric as well, but
gives you something you weren't expecting.
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER