tags:

views:

2665

answers:

11

Is there a neither, nor syntax?

I have found it by reading your post and it's if(!){ //do code }.

http://bcsy.hostzi.com

+1  A: 

No, there isn't.

Paul Mitchell
A: 

I think you should have a look at http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B and focus on "Logical operators" part.

Jacek
+10  A: 

To encode "if neither A nor B":

if (!A && !B) { ... } //if (not A) and (not B)

or:

if (!(A || B)) { ... } //if not (A or B)
ChrisW
AKA De Morgan's law in the other answer.
Potatoswatter
+1  A: 

no. You can achieve the same using if in conjunction with ! (not), && (and) and || (or)

Johannes Rudolph
+1  A: 
if ( x == 1 )
{ // do this }

else if ( x == 2 )
{ // do this }

else { // do this if it's neither 2 nor 1 }

the last else is the same as:

if ( x != 1 && x != 2 ) { // do something }
Filip Ekberg
+38  A: 

While there isn't a built in syntax to do this, I'd suggest you take a look at Supported logical operators and then carefully study De Morgan's laws. Sufficient knowledge in these two fields will allow you to write any logical statement in if-else if syntax.

EDIT: Just to completely answer your question (all though is has been done in other answers) you could write a neither - nor statement like this:

if (!A && !B) { DoStuff(); }
Rekreativc
+1 for De Morgan's law, something every programmer should know.
R Samuel Klatchko
AraK
Thanks for that - I knew the laws, but didn't know they were named after anybody :-)
James Hopkin
Just wanted to add that you can change that operator to `if (!(A || B)) { DoStuff(); }` and it will work exactly the same.
voyager
+48  A: 

Oh ... you wanted the "ain't" keyword?

VB adds Ain't Keyword

(Newswire 8-19-2004)

Microsoft has announced that Visual Basic will add the "Ain't" keyword to the language. According to a source on the VB.NET team "With VB, we want the language to work the way you think. Extensive usability studies have demonstrated to us the benifit of adding Ain't to the language."

Addition of the keyword would allow such syntax as

If ThisThing Ain't Nothing Then

According the source "We're just trying to keep up with advances in the English language which, as you know, is changing almost as fast as technology itself." The VB team believes that ain't is poised to finally be a fully supported keyword in the English language, and they feel that if they don't include the keyword in this release, they may fall well behind English before their next chance to update VB. However, hotly debated is what "Ain't" should equate to. In it's most popular form, the above line of code would translate to:

If ThisThing Is Nothing Then

However, everyone's 2nd grade english teacher has made it clear that "Ain't Nothing" actually means "Is Something", as it's a double-negative. Meaning the correct equivelant would be

If ThisThing IsNot Nothing Then

Microsoft is in no hurry to rush through this decision, state sources, "Look, between VB.NET Beta 1 and Beta 2, we had to change the definition of 'true'. We don't want to go through that again."

However language purists declare that this whole approach is misguided, noting that "Ain't" is a contraction for "am not", and saying "If ThisThing Am Not Nothing" is just poor grammar. Better alternatives, they say, would include resurecting i'n't, as in "If ThisThing I'n't Nothing". But even this may not be far enough states linguist Jacque Leblanc, "I insist that the perpetuation of the double negative is the root cause of this issue, but as of yet, no one is really willing to discuss the obvious elephant in the room. The true solution would be to allow 'If ThisItem Is Something Then.'"

Microsoft is also reported to be experimenting with "AsIf", "Maybe", and "Totally". In addition, "Catch" will likely be replaced with "Doh!", and "Finally" will be replaced with "Whatever".

Jeff Atwood
yea thats awesome aint lol yall are cool
H4cKL0rD
ROFLMAO, partly because it's ridiculous, and partly because there's a little voice telling me they might just do this! "Wordup" is the new "Hello World".
Bernhard Hofmann
Careful Jeff, that's dangerously close to FUN :)
Tim Long
VB = Vernacular Basic. =) It reminds me something: http://en.wikipedia.org/wiki/Shakespeare_(programming_language)
overrider
Like in "VBA ain't a real programming language" xD
Helper Method
+6  A: 

Here you go:

class neither_t
{
    bool lhv;
    neither_t(bool lhv): lhv(lhv) {}
public:
    bool nor(bool rhv) const
    {
        return !lhv && !rhv;
    }
    friend neither_t neither(bool lhv);
};

neither_t neither(bool lhv)
{
    return neither_t(lhv);
}

#include <cstdio>

int main()
{
    int x = 3;
    if (neither(x == 1).nor(x == 2)) {
        puts("OK");
    }
}
UncleBens
You got C in my C++! :P
Bill
A: 

Yes. The && and || operators in C do perform flow control, and all expressions are statements, so && and || form flow control statements. The terms of the expression are evaluated until its value is known, so && will execute a series of true expressions and || will execute a series of false expressions. As || (OR) keeps going as long as its arguments are false (NOT), it can be called a neither-nor statement.

bool a = fun1(), b = fun2();
a || b || ( cerr << "neither A nor B is true" << endl );
!a && !b && ( cerr << "De Morgan says neither A nor B is true" << endl );

"But," you say, "that's not really a flow control statement. It only affects flow control within one statement." Fair nuff. The throw operator is actually also an expression with type void. So we can extend this, erm, style to cover blocks of several lines.

try {
    a || b || ( throw logic_error( "neither a nor b is true" ), false );
} catch( logic_error &exc ) {
    cerr << exc.what() << endl;
    cerr << "now with added madness!" << endl;
}

I hope that's what you wanted…

Potatoswatter
A: 

Ruby does have some of this kind of syntactic sugar:

  • unless is the equivalent of if !
  • collection.empty? can be used as the equivalent of !collection.any?
micho
A: 

do you mean unless from perl?

$a = 12;
unless($a >= 0) {
  print "a is negative\n";
} elsif ($a == 0) {
  print "a is equal to 0\n";  
} else {
  print "a is positive\n";  
}
# it prints: a is positive

Strangely there is no else unless, only (the equivalent of) else if.

Marius