No it's not a good way of coding. The usual way is to use !
to invert the logic:
if(!(k==j && n==i))
//statement
Or you could use De Morgan's Law to invert the logic:
if(k!=j || n!=i)
//statement
Question
I am looping i, j, k and n. I want a statement to execute except when k == j
and n == k
.
If I use the code like this:
if (k != j && n != i)
//statement
The statement will not execute in two cases:
So I used a code like this:
if (k == j && n == i)
;else
//statement
By this code the statement will successfully execute except when k == j && n == i
.
Is semicolon-terminated if-statements is a good way of coding in C++?
Solution
No it's not a good way of coding. The usual way is to use !
to invert the logic:
if(!(k==j && n==i))
//statement
Or you could use De Morgan's Law to invert the logic:
if(k!=j || n!=i)
//statement
OTHER TIPS
Your problem is that you're negating the condition incorrectly. You should just do:
if (!(k==j && n==i))
// statement
Or, by De Morgan's laws:
if (k != j || n != i)
// statement
... is a good way of coding?
No.
You should write
if (!(k == j && n == 1))
{
//statement
}
Putting a semicolon after an if
, for
, or while
is almost always wrong. It is highly unexpected and makes reading your code very difficult.