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; } )
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 */ }
My best code is written with the delete key.
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
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(); }
Last edited by iMalc; 01-24-2009 at 03:14 PM.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
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 ... )
Last edited by King Mir; 01-24-2009 at 09:29 PM.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
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.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
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;
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
@matsp & Elysia
You don't seem to be applying Demorgan's correctly:
!( (A == 1) && (B == 2) )
is the same as
A!=1 || B!=2
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.