If statement

Pages: 12
Dec 17, 2009 at 3:19am
How could I write this as one logical extended if statement? I am confused on this. Thanks.




if ((age >= 18) && (age < 65))
cout << "Adult" << endl;

if (age > 64)
cout << "Senior" << endl;

if (age < 18)
cout << "Minor" << endl;
Dec 17, 2009 at 3:24am
i don't see a way you could.... but i could be wrong ha :D
Dec 17, 2009 at 4:03am
You can't have one if statement because there are 3 outcomes. At best you can have 2 if statements:

1
2
3
4
5
6
if(age > 64)
  // senior
else if(age >= 18)
  // adult
else
  // minor 
Dec 17, 2009 at 5:00am
here is an extended if statement (if/else/else) , technically it is a single if statement

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include <iostream>;
using namespace std;

int main()
{
int a;

cout << "enter Age:\n";
cin >> a;
if(a>=18 &a <=64)cout << "adult\n"; 
else if(a<=17)cout<< "minor\n"; 
else cout<< "senior\n";


return 0;
}


Last edited on Dec 17, 2009 at 5:04am
Dec 17, 2009 at 5:07am
technically it is a single if statement


Can you explain?

I count two statements that have "if" in them.
Dec 17, 2009 at 5:21am
sure, the original 'if' statement is the catalyst, the else statements are conditional expressions related to the original, as soon as the first else/if expression in the ladder is found true, then all following else ifs in the ladder are disregarded(even if they are true as well), therefore they are inherent to the original if statement and not independent, if no else statement is found true it the default statement at the end of the ladder is usually just that a default output and why the program works. it IS an extended if expression
Last edited on Dec 17, 2009 at 5:23am
Dec 17, 2009 at 5:22am
but the two ifs are still two different statements, are they not?


Whatever. I'm nitpicking about something that really doesn't matter.

Nevermind me. Goodnight!
Dec 17, 2009 at 5:40am
lol ok, , and have a good night btw :D but tell me why this program wont display all if's statements they are all true? because they are part of an extended if statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include <iostream>;
using namespace std;

int main()
{
int a;

a=4;

     if(a==4)cout << "you wont read anything beyond this even though the following if statements are all true\n"; 
else if(a==4)cout << "just\n"; 
else if(a==4)cout << "making\n"; 
else if(a==4)cout <<"my \n";
else if(a==4)cout << "point\n"; 
else if(a==4)cout <<"lol\n";

return 0;
}

Dec 17, 2009 at 5:43am
What you are doing is basically this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>;
using namespace std;

int main()
{
    int a = 4;
    if(a == 4) //#1
    else {
        if(a == 4) //#2
        else {
            //etc
        }
    }
    return 0;
}
Dec 17, 2009 at 5:44am
yes
Dec 17, 2009 at 2:00pm
 
std::cout << ( age < 18 ? "Minor" : age > 65 ? "Senior" : "Adult" ) << std::endl;

Dec 18, 2009 at 1:46am
Conditional operator pwns.
Dec 18, 2009 at 3:13am
oh that's cool, I thought it could only be used in cases of if/else but I'm guessing if the above works then you can have multiple if/else if/else if/else if/else if/else statements. assuming you stay approx around/below the 80 char limit.
Dec 18, 2009 at 3:19am
What 80 characters limit?

EDIT:
because they are part of an extended if statement.
There's no such thing as "extended if statement". Only the first true block is executed because all the other if statements are inside the false block of the outermost (i.e. the first) if statement.
Last edited on Dec 18, 2009 at 3:22am
Dec 18, 2009 at 3:30am
80 char limit for printers. I usually try to keep my code below this. so for example a cout statement that is 300 chars long can be broken up and put on 4-5lines; eg.

1
2
3
4
cout << "Hello World, My name is " << name << ". I like to dance on the moon with "
      << alien_Name << endl << "Because he teaches me lots of cool things like: " 
      << move1 << " and " << move2 << " and " << move78 << "." << endl 
      << "Also he has great alien weed, and a " << alien_spaceship << endl;


as far as C++ goes, you can pretty much have a 5000 line program on the 1 line. But it's pretty damn poor practice and virtually impossible to read.

I guess the question should be asked can you include whitespace (newlines) within extended if's?
condition ? function : condition ?
function : condition ? function : function
Last edited on Dec 18, 2009 at 3:37am
Dec 18, 2009 at 3:48am
people actually print sourcecode?

I find that restricting myself to 80 char line width is a little absurd. Especially considering the resolution and size of modern monitors.
Dec 18, 2009 at 3:50am
I guess the question should be asked can you include whitespace (newlines) within extended if's?
Duh.

extended if
Grr...
Last edited on Dec 18, 2009 at 3:51am
Dec 18, 2009 at 3:58am
what do you call it then, if not 'extended if'?

people actually print sourcecode?

yep, people use text books too.
Last edited on Dec 18, 2009 at 4:02am
Dec 18, 2009 at 4:38am
Trinary operator, ternary operator, or conditional operator.

Limiting yourself to 80 characters doesn't make sense unless you expect to read your code on a console or you're going to print it with a dot matrix printer.
Dec 18, 2009 at 10:33am
Just about all code stays under 80 anyway, well make that 100. But very very rarely do I see other peoples code go beyond 100.

and apart from that I do use VI/VIM under cygwin.

typically when printing code I do it horizontally anyway so the printer doesn't word wrap lines. I think horizontally bumps it up to around 120 mark.
Pages: 12