Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

There is an if-else, is there a Neither Nor statement?

Tags:

c++

Is there a neither A nor B syntax?

like image 972
H4cKL0rD Avatar asked Jan 13 '10 07:01

H4cKL0rD


5 Answers

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".

source: http://web.archive.org/web/20050308014055/http://ea.3leaf.com/2004/08/vb_adds_aint_ke.html

like image 172
Jeff Atwood Avatar answered Nov 09 '22 09:11

Jeff Atwood


While there isn't a built-in syntax to do this, I'd suggest you take a look at the list of 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: To completely answer your question (although this has been done already in other answers), you could write a neither–nor statement like this:

if (!A && !B) { DoStuff(); }
like image 24
David Božjak Avatar answered Nov 09 '22 08:11

David Božjak


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)
like image 31
ChrisW Avatar answered Nov 09 '22 07:11

ChrisW


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");
    }
}
like image 13
UncleBens Avatar answered Nov 09 '22 08:11

UncleBens


No, there isn't.

like image 3
Paul Mitchell Avatar answered Nov 09 '22 07:11

Paul Mitchell