Dangling else problem is common problem that happens when using nested if statements with single line statement.
Following program introduces dangling else problem.
#include "../helpers/stdout.h"
int main(int argc, char const *argv[])
{
if (true)
if (false)
print("Inner if");
else
print("where do I belong");
return 0;
}If we see the program, we should expect nothing to be printed. However, we see where do I belong when we execute the program. Why is that? It is because else statement becomes part of inner if statement.
To avoid this problem, we should explicitly use compound statements.