How do I write a No Operation in 'C'?
The code looks like this.
Code:
(
if ((A == 1) && (B == 2)
{
NOP;
}
else
if ((C == 3) || (D == 4))
{
A = C;
B = D;
}
)
Printable View
How do I write a No Operation in 'C'?
The code looks like this.
Code:
(
if ((A == 1) && (B == 2)
{
NOP;
}
else
if ((C == 3) || (D == 4))
{
A = C;
B = D;
}
)
Just don't do anything. (And for that matter, why even check? Why not go to the next case?)
The closest you can get is a null statement:
Code:if ( condition )
;
else if ( condition ) {
/* Do something */
}
Ideally, rewrite the code to not do "reverse checking" - instead:
Alternatively, as one condition [you can array the values in any order, as long as the parenthesized expressions are retained]:Code:if ((A != 1) && (B != 2))
{
if ((C == 3) || (D == 4))
{
... do stuff ...
}
}
--Code:if (((C == 3) || (D == 4)) && (A != 1) && (B != 2))
{
... do stuff ...
}
Mats
What matsp has used there is De Morgan's laws
Basically if you ever have an expression formed of two sub-expressions, you can modify the expression as follows and end up with an expression that is exactly equivalent.
Negate both sub-expressions, change the operator between then from 'and' to 'or' or vice-versa, negate the entire expression.
E.g.
(a && b) gives (!(!a || !b))
(x>0 || y<0) gives (!(!(x>0) && !(y<0))) or better yet, simplify that to (!(x<=0 && y>=0))
!(!c || d==42) gives !!(!!c && !(d==42)). Remove the double !'s and simplify to (c && d!=42)
The other useful transformations here are to go from:or from:Code:if (a) {
if (b) {
foo();
}
}
// to
if (a && b) {
foo();
}
Code:if (a) {
foo();
}
else if (b) {
foo();
}
// to
if (a || b) {
foo();
}
The next case is an else. The point of the code is if the first statement is true, then nothing happens. The else statement is not executed.
Maybe by making the inverse of the first statement anded with the second is a way to handle it.
I like the Null Statement as Prelude suggested best. I can comment it and have readable code, where DeMorganizing it makes the code unreadable.
I suggested a form that is not unreadable, I beleive.
In my programming life, I have never used a null-statement in a if-statement [aside from when macro expansion turns statements into nothing]. It's a bit like starting a statement with the excpetion, like this "except for ice-cream, our deserts are served warm". It is better (more straightforward and clear) to say "all our deserts are served warm, except ice-cream". If you want something to happen only when a != 1 and b != 2, then say so. Don't say "if a = 1 and b = 2 do nothing, else do ..."
If you feel you must do that, then make sure you make a comment, like you say.
--
Mats
You don't have to actually apply demorgan's rules if they make the code less clear. Just but the condition in parens and use !, as in :
Or if there are additional else's:Code:(
if ( !( (A == 1) && (B == 2) ) && (C == 3 || D == 4))
{
A = C;
B = D;
}
)
Code:if ( !( (A == 1) && (B == 2) ) )
{
if(C == 3 || D == 4))
{
A = C;
B = D;
} else ...
)
Well, my Compiler doesn't seem to like a Null IF statement, unless I'm using the wrong braces.
matsp, Your suggestion is not unreadable, but this a very complex nested IF-ELSE. The code that I showed was a simplified example. The execution has to exit the IF-Else at several points where a condition is true, but continue to fall through where conditions are false. I'm concerned that I'll never get it right if I make too many changes to the Intuitive statements.
I might have to do that, if the Compiler doesn't accept the Null If.
I can't see what's so hard?
Instead of:
It would make so much more sense to do:Code:if ( condition A )
Do Nothing;
else if ( condition B )
Do Something;
i.e. what matsp said.Code:if ( (Not condition A) && (condition B) )
Do Something;
Code:if ( (A != 1 && B != 2) &&
(C == 3 || D == 4) )
{
A = C;
B = D;
}
@matsp & Elysia
You don't seem to be applying Demorgan's correctly:
!( (A == 1) && (B == 2) )
is the same as
A!=1 || B!=2