There is an if-else, is there a Neither Nor statement?
开发者_如何学编程Is there a neither A nor B
syntax?
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 ThenAccording 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 ThenHowever, 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 ThenMicrosoft 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
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(); }
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)
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");
}
}
No, there isn't.
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
.
no. You can achieve the same using if
in conjunction with !
(not), &&
(and) and ||
(or)
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 }
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…
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?
精彩评论