15

Is there a neither A nor B syntax?

MD XF
8,1899 gold badges45 silver badges74 bronze badges
asked Jan 13, 2010 at 7:56
3
  • 6
    Though it seems to make sense in English it is hard to really tell as English is such an imprecise language. Do you have a mathematical definition of the functionality you want. If you can define this 'neither' and 'nor' in terms of true and false then the rest should be trivial. Commented Jan 13, 2010 at 8:14
  • 5
    Intuitively that would translate to neither(x) { /* doesn't happen */ } nor(y) { /* doesn't happen */ } for me... Probably not what you intended. Commented Jan 13, 2010 at 8:29
  • 1
    actually, as Rekreativc mentions, the comparable feature to "Neither Nor' Isn't "if-else" is "and" (or "both-and" if you prefer) or "or" ("either-or") so you're really looking for a logical operator not a statement keyword... Commented Jan 13, 2010 at 17:21

10 Answers 10

109

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

Aaron Thoma
4,0461 gold badge40 silver badges38 bronze badges
answered Jan 13, 2010 at 8:54
Sign up to request clarification or add additional context in comments.

1 Comment

They really wrote "In it's most popular form" instead of its when the topic is language (with apostrophes).
64

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(); }
Jordan Gray
16.5k3 gold badges56 silver badges70 bronze badges
answered Jan 13, 2010 at 8:02

4 Comments

+1 for De Morgan's law, something every programmer should know.
Exactly, you don't need more than (&&, ~) OR (||, ~) to represent any logical connection.
Thanks for that - I knew the laws, but didn't know they were named after anybody :-)
Just wanted to add that you can change that operator to if (!(A || B)) { DoStuff(); } and it will work exactly the same.
16

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)
answered Jan 13, 2010 at 8:00

1 Comment

AKA De Morgan's law in the other answer.
14

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");
 }
}
answered Jan 13, 2010 at 9:54

1 Comment

I'm glad you didn't misname it either. that's a misuse in american english that bugs me.
4

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.

answered Jun 22, 2010 at 7:28

Comments

3

No, there isn't.

answered Jan 13, 2010 at 7:59

Comments

1

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

answered Jan 13, 2010 at 8:00

Comments

1
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 }
answered Jan 13, 2010 at 8:00

Comments

0

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

answered Jan 13, 2010 at 17:13

Comments

0

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?
answered Jan 16, 2010 at 14:25

1 Comment

I really miss the unless keyword, surprised python doesn't have it, the way ternaries are eschewed by the BDFL.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.