Boolean Operator question

P

promet

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
 
I

Ian Collins

promet said:
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?
 
R

Robbie Hatley

promet said:
(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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top