I have this code:
IF (x>5 AND y=4) THEN
dbms_output.put_line("Y");
ELSE
dbms_output.put_line("N");
My question is: how do you change the code if you can not use AND as the operator but instead, you have to use OR to have the same logic and get the same result?
You can use DeMorgan's Law to rewrite it:
NOT (p AND q) == (NOT p) OR (NOT q)
So, in your case
IF (x>5 AND y=4) THEN "Y" ELSE "N"
IF (x<=5 OR y<>4) THEN "N" ELSE "Y"
Or and AND operator have completely different logic, you cannot use AND as the operator but instead, you have to use OR to have the same logic and get the same result.
If you change the line to read like this, it will work with only the OR operator:
IF (x<=5 OR y!=4) THEN dbms_output.put_line("N"); ELSE dbms_output.put_line("Y");=
The outcome is the same, but the logic is slightly different.
Related
Is there a LLVM AST matcher for the use of a C conditional? I know there is the hasCondition() option for ifStmt, but that is only good for an if statement. In particular, I'm looking to match for a boolean condition that has no operator (e.g. if (flag), while(flag), or (flag ? x : y)). But I'd also be interested in the more generic case of any conditional.
The closest I could find was for ifStmt, whileStmt or doStmt:
xxxStmt(unless(hasCondition(binaryOperator(isComparisonOperator()))))
which allows me to also check for things like if (!flag)
For the case flag ? x : y, one can match conditionalOperator() then determine if the expression has a comparison operator.
I came across some code in VBA & I'm trying to understand how it works, but I've never encountered this operator.
What exactly does Imp operator do in VBA?
If (a <> 0 Imp b = 0) Then
MsgBox ("IMP Operator Result is : True")
Else
MsgBox ("IMP Operator Result is : False")
End If
Are there examples of when we would use it?
Obviously, this is not a bitwise comparison, but something else.
The documentation from the comment actually is not that good. This is what it says:
The Imp operator performs a bitwise comparison of identically positioned bits in two numeric expressions and sets the corresponding bit in result according to the following table.
If you take a look at the example:
You would see that the result is always True(or 1) in any possible cases but one. The single case, where the result is False (or 0) is when the first operand is True and the second operand is False.
I am reading through the Julia manual right now and ran into my first potential disappointment.
I like being able to code conditional statements tersely. In R I might write:
if (x==y) print("Hello")
In Julia however I think I might need do
if x==y
println("Hello")
end
Or perhaps x==y ? print("Hello") : print("") which is certainly silly.
Is there some formulation in Julia for allowing for single line conditional statements?
You can write if x == y println("Hello") end or, as has become somewhat idiomatic, you can use the short-circuiting behavior of the && operator and write x == y && println("Hello"). In a very similar fashion it is fairly common to check some condition and throw an error if it isn't met by writing something like this: size(A) == size(B) || error("size mismatch").
Was wondering if someone could lend me their expertise. Pretty new to Vb.net and have come across this conditional statement in one of our products. Could someone please confirm the validity of the statement and explain what's going on here? I've tried numerous searches, but I cannot find anything related.
If (IsDBNull(dr("someID")), "0", dr("someID")) = someID.ToString() Then
I have changed the "id" value names as it's code from a commercial product, but the ID's used were all the same variable (ints).
Thanks for any input you can offer on this!
Joe
PS: The reason I can't check this at run time is because of how the product operates.
It is an inline If statement
If(condition,iftrue,iffalse) if condition is true evaluate and return iftrue else iffalse
The If operator in VB.NET 2008 acts as a ternary operator.[ REFERENCE]
Example:
Dim foo as String = If(bar = buz, cat, dog) 'Condition satisfied then it'll return cat else dog.
The statement is checking to see if the dr("SomeID") equals the value someID.ToString. The reason the If is required is because you need to check if the dr("someID") Is Null. If it is 0 is used instead which presumably should not be equal to someID.
It is the same as doing the following:
If Not IsDBNull(dr("someID")) Then
If dr("someID").ToString = someID.ToString Then
End If
End If
I would suggest that something like this would be more appropriate (checking integer values instead of comparing strings)
If(IsDBNull(dr("someID")), 0, CInt(dr("someID"))) = someID Then
I would also suggest Turning Option Strict On as the code you posted should not compile!
Can someone explain the inactive ? #"inactive": #"active"?
The ? : is a boolean conditional structure (wrong term) it seems but I'm not quite getting it. Don't know what it is called so can't look it up.
Seems something like:
someBooleanValue ? if it is false use what is before colon : else use
what is after
I get that it is being used to determine which string to use as the format token (in the code below). I just don't know what this ? : bit is called and what limitations/cautions/abuses there may be with it.
(and isn't ObjC like rilly hard to format in a civilized way)
UIAlertView* av = [[UIAlertView alloc] initWithTitle:#"Hey"
message:[NSString
stringWithFormat:#"While %#, I received a local notification: %#",
inactive ? #"inactive": #"active", n.alertBody]
delegate:nil
cancelButtonTitle:#"OK" otherButtonTitles:nil];
[av show];
This ?: thing is called a conditional operator or a ternary operator.
It's represents a simple condition
if ( CONDITION )
x = a;
else
x = b;
that can be translated to
x = CONDITION ? a : b
From that you can probably derive the functionality you're trying to accomplish/understand. Keep in mind that, although you could probably use it as a substitute to the normal if/else-if/else structure, it is considered bad programming the usage of the ternary operator out of any "assignment related action".
In the wikipedia page for it you can find a great variety of examples of the conditional operators used in different programming languages. Check this one too, the ternary operator page.
Obs: turns out that a ternary operator is not necessarily a conditional expression, but rather any operator that takes three arguments. Since for most of the programming languages the only ternary operator is the inline-if... well, that's what it's usually called.
It is called conditional operator, a kind of ternary operator (as opposed to more familiar binary a+b or unary !flag operators).
Conditional operator takes a condition, evaluates it, and returns its second or third operand depending on that result.
You can read more information here.
The use of this operator can greatly reduce code length when a lot of simple ifs are involved.
It's a ternary operator, but you have it backwards -- if the boolean is true, then do the thing before the colon, otherwise, the one after.
This is called the ternary operator and it works exactly the way you described it:
expression ? value if true : value if false;
For instance, you could use it for something like this to avoid a if - else:
int maxValue = a > b ? a : b;
Edit: #dasblinkenlight is correct, the operator you're talking about is actually called a conditional operator, which is a kind of ternary operator.