|
-
February 12th, 2009, 10:30 PM
#1
Is this possible?
[code\]
int a=5;
if (a==7||6||5||4||3||2)
instead of doing
int a=5;
if (a==7||a==6||a==5||a==4||a==3||a==2)
[code/]
is this possible and if not how can you make it possible?
-
February 12th, 2009, 10:56 PM
#2
Re: Is this possible?
Code:
int a=5;
if (a==7||6||5||4||3||2)
is not going to do what you want it to (the condition will always be true). Instead do
Code:
if( a >= 2 && a <=7 )
// Do something
or use a switch statement
Code:
switch( i )
{
case 7:
case 6:
case 5:
case 4:
case 3:
case 2:
//Do something
break;
default:
break;
};
-
February 13th, 2009, 08:18 AM
#3
Re: Is this possible?
 Originally Posted by E-man96
[code\]
int a=5;
if (a==7||6||5||4||3||2)
instead of doing
int a=5;
if (a==7||a==6||a==5||a==4||a==3||a==2)
[code/]
is this possible and if not how can you make it possible?
No it's not.
What do you have against white space?
-
February 13th, 2009, 08:35 AM
#4
Re: Is this possible?
You may overload operator "||". But it is not recommanded.
-
February 13th, 2009, 08:36 AM
#5
Re: Is this possible?
 Originally Posted by E-man96
[code\]
int a=5;
if (a==7||6||5||4||3||2)
instead of doing
int a=5;
if (a==7||a==6||a==5||a==4||a==3||a==2)
[code/]
is this possible
No it's not possible. C++ does not understand colloquial English. Just because English speakers shorten their "or's" by combining everything into one sentence doesn't mean it works for C++.
Every comparison must be explicitly stated, one at a time.
and if not how can you make it possible?
Didn't you do that already?
Code:
int a=5;
if (a==7||a==6||a==5||a==4||a==3||a==2)
Regards,
Paul McKenzie
Last edited by Paul McKenzie; February 13th, 2009 at 12:24 PM.
-
February 13th, 2009, 08:40 AM
#6
Re: Is this possible?
 Originally Posted by Paul McKenzie
C++ does not understand colloquial English. Just because English speakers shorten their "or's" by combing everything into one sentence doesn't mean it works for C++.
There are languages that do work that way though. C++ Just doesn't happen to be one of them.
-
February 13th, 2009, 12:27 PM
#7
Re: Is this possible?
 Originally Posted by GCDEF
There are languages that do work that way though. C++ Just doesn't happen to be one of them.
Maybe, but most high-level (and low-level) languages work only by specifying in "long hand" all of the conditions.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|