# NOP in 'C'?

This is a discussion on NOP in 'C'? within the C Programming forums, part of the General Programming Boards category; How do I write a No Operation in 'C'? The code looks like this. Code: ( if ((A == 1) ...

1. ## NOP in 'C'?

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;
}
)```

2. Just don't do anything. (And for that matter, why even check? Why not go to the next case?)

3. The closest you can get is a null statement:
Code:
```if ( condition )
;
else if ( condition ) {
/* Do something */
}```

4. Ideally, rewrite the code to not do "reverse checking" - instead:
Code:
```if ((A != 1) && (B != 2))
{
if ((C == 3) || (D == 4))
{
... do stuff ...
}
}```
Alternatively, as one condition [you can array the values in any order, as long as the parenthesized expressions are retained]:
Code:
```if (((C == 3) || (D == 4)) && (A != 1) && (B != 2))
{
... do stuff ...
}```
--
Mats

5. 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:
Code:
```if (a) {
if (b) {
foo();
}
}

// to

if (a && b) {
foo();
}```
or from:
Code:
```if (a) {
foo();
}
else if (b) {
foo();
}

// to

if (a || b) {
foo();
}```

6. Originally Posted by tabstop
Just don't do anything. (And for that matter, why even check? Why not go to the next case?)

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.

7. Originally Posted by danlee58
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

8. 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 :
Code:
```(
if ( !( (A == 1) && (B == 2) ) && (C == 3 || D == 4))
{
A = C;
B = D;
}
)```
Or if there are additional else's:
Code:
```if (  !( (A == 1) && (B == 2) )  )
{
if(C == 3 || D == 4))
{
A = C;
B = D;
} else ...
)```

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

10. Originally Posted by danlee58
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..
Isn't that just the logical flow? Also, in your first example you are not doing anything of the sort. If you want to exit, break. Otherwise the apple hits the ground. The NOP is definitely a no-op...

11. Originally Posted by danlee58
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.
If your compiler doesn't accept ; as the true part of an if, it is extremely extremely broken. I would venture that it is highly more likely that you have messed up the braces.

12. Originally Posted by danlee58
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.
Prelude's example should work. If you tried that and it didn't, post the smallest sample of code that demonstrates the failure.

The normal solution is still to negate the do nothing condition, and put the other alternatives in that if block.

13. I can't see what's so hard?
Code:
```if ( condition A )
Do Nothing;
else if ( condition B )
Do Something;```
It would make so much more sense to do:
Code:
```if ( (Not condition A) && (condition B) )
Do Something;```
i.e. what matsp said.

14. Code:
```if ( (A != 1 && B != 2) &&
(C == 3 || D == 4) )
{
A = C;
B = D;
}```

15. @matsp & Elysia

You don't seem to be applying Demorgan's correctly:
!( (A == 1) && (B == 2) )

is the same as

A!=1 || B!=2

Page 1 of 2 12 Last