470,815 Members | 1,292 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

Boolean Operator question

Hi,

I'm trying to understand if statements. I've written the following
code:

#include <iostream>

using namespace std;

int main()
{

int age;

cout<<"Please tell me your age: \n\n";
cin>> age;
cin.ignore();
if (age < 50)
{
cout<<"You're a regular spring chicken, congratulations!\n";
}
else if ( age >= 50 && age <=70)
{
cout<<"You're still looking great, lay off the beer though!\n";
}
else if ( age >= 70 && age <= 100)
{
cout<<"You're entering the twilight of your years, make sure that your
will is in order!\n";
}
else if ( age >= 100)
{
cout<<"Daaamn Methuselah! Whether you're lucky or diligent
congratulations! Here's to clean living...\n";
}

cin.get();
}
It compiles and runs fine, but any age entered over 70 resolves to the
"You're still looking great, lay off the beer though!\n"; statement. In
my understanding the following "else if" statements should report the
other responses. Clearly I'm mistaken though. Could someone offer some
clarity here? Thanks.

promet

Jul 1 '06 #1
2 1044
promet wrote:
Hi,

I'm trying to understand if statements. I've written the following
code:

#include <iostream>

using namespace std;

int main()
{

int age;

cout<<"Please tell me your age: \n\n";
cin>> age;
cin.ignore();
if (age < 50)
{
cout<<"You're a regular spring chicken, congratulations!\n";
}
else if ( age >= 50 && age <=70)
{
cout<<"You're still looking great, lay off the beer though!\n";
}
else if ( age >= 70 && age <= 100)
{
cout<<"You're entering the twilight of your years, make sure that your
will is in order!\n";
}
else if ( age >= 100)
{
cout<<"Daaamn Methuselah! Whether you're lucky or diligent
congratulations! Here's to clean living...\n";
}

cin.get();
}
It compiles and runs fine, but any age entered over 70 resolves to the
"You're still looking great, lay off the beer though!\n"; statement. In
my understanding the following "else if" statements should report the
other responses. Clearly I'm mistaken though. Could someone offer some
clarity here? Thanks.

Does it? It shouldn't.

You do have too many <= or >= though one limit should be a < or >, which
response do you expect for 70?

--
Ian Collins.
Jul 1 '06 #2
"promet" <pr*******@sbcglobal.netwrote:
(question about if/else if/ else)
For one thing, your intervals overlap. For another, you fail to
quite understand what "else if" means. Here, I'll take care of
the overlaps and reformat to show just what those else-ifs are
doing:

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int age;
std::cout<<"Please tell me your age: \n\n";
cin >age;
cin.ignore();
if (age < 50)
{
cout << "... spring chicken ..." << endl;
}
else // age is 50 or more
{
if ( age >= 50 && age <70)
{
cout << "... lay off the beer ..." << endl;
}
else // age is 70 or more
{
if ( age >= 70 && age < 100)
{
cout << "... twilight ..." << endl;
}
else // age is 100 or more
{
cout << "... Daaamn Methuselah! ..." << endl;
}
}
}
return 0;
}

That's what "else if" is really doing for you. It just elides
the "{" between the "else" and the "if" for more readibility.
But in effect, it's nested rather than flat.

--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 2 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Frantisek Fuka | last post: by
16 posts views Thread by Shawnk | last post: by
33 posts views Thread by Stef Mientki | last post: by
7 posts views Thread by Flavio | last post: by
7 posts views Thread by Michael Sharman | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.