Then it's a syntax error, but I get what you mean.
intfactorial(intn){int fallback =1;/* This breaks if you remove the comment,
* so leave it in.
*/if(n ==0){Handle0! specially.
n =1;*0! is the same as 1!.returnfactorial(n);}else{return n *factorial(n -1);}return fallback;}
vs (what you probably mean)
intfactorial(intn){int fallback =1;/* This breaks if you remove the comment,
* so leave it in.
*/if(n ==0){
n =1;returnfactorial(n);}else{return n *factorial(n -1);}return fallback;}
vs what the rest was thinking of
intfactorial(intn){int fallback =1;/* This breaks if you remove the comment,
* so leave it in.
*/if(n ==0){}else{return n *factorial(n -1);}return fallback;}
int factorial(int n) {
int fallback = 1; /* This breaks if you remove the comment,
* so leave it in.
*/
if (n == 0) {
} else {
return n * factorial(n - 1);
}
return fallback; }
The comment behind the if removes the whole if-statement, if it is uncommented what I described above happens.
Without the comment:
int factorial(int n) {
int fallback = 1; /* This breaks if you remove the comment,
* so leave it in.
*/
if (n == 0) {
n = 1;
return factorial(n);
} else {
return n * factorial(n - 1);
}
return fallback; }
I didn't know it will compile without a statement following if....
The comment behind the if removes the whole if-statement
It only removes the block for the if/true path - not the whole if/else construct. Your code matches, your words not. Sorry to be nitpicky here, but that's important as it is very easy to cause misunderstandings.
View all comments
Show parent comment
64
u/Kengaro Mar 24 '20 edited Mar 24 '20
Endless loop:
Ends up with factorial 1, which makes 1 * factorial(0), which calls factorial(1)....