The Law of Excluded Middle is incredibly useful for formal reasoning about if
statements. The two choices that you have in your code are as follows:
n
is equal to1
, andn
is not equal to1
.
There is no third “middle” state in this logic.
Now let’s go back to your code, and see what happens depending on the value of n
. When it is 1
, your code returns 1
. When n
is not one, your code returns a result of some calculation.
In order for printf("Hello")
to be reached, n
needs to be equal to one and not equal to one at the same time (i.e. n==1 && n!=1
must be true). The law of excluded middle lets you mathematically prove that this expression is false
for all values of n
, meaning that the printf
line is unreachable under any circumstances. Optimizing C compilers will issue a warning, and remove this “dead code” from the executable that they generate.
solved Does a statement after return encounters even execute the statement after it