473,394 Members | 2,100 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 1122
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Frantisek Fuka | last post by:
Can anyone please explain why these two give different results in Python 2.3? >>> 'a' in 'abc' == 1 False >>> ('a' in 'abc') == 1 True I know it's not a good idea to compare boolean with...
16
by: Shawnk | last post by:
I would like to perform various boolean operations on bitmapped (FlagsAttribute) enum types for a state machine design as in; ------------------- enum portState { Unknown, Open,
33
by: Stef Mientki | last post by:
hello, I discovered that boolean evaluation in Python is done "fast" (as soon as the condition is ok, the rest of the expression is ignored). Is this standard behavior or is there a compiler...
7
by: Flavio | last post by:
Hi, I have been playing with set operations lately and came across a kind of surprising result given that it is not mentioned in the standard Python tutorial: with python sets, intersections ...
7
by: Michael Sharman | last post by:
Can anyone confirm if in PHP 0 = FALSE and anything else if TRUE? Or is it more 0 = FALSE and 1 = TRUE? One of the reasons I ask is that if I do; //$count is an incrementing value starting...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.