Is there a way to declare, assign and compare a variable to an expression in an if construction in such a way that it is only defined in the scope of the if construction?
The first snippet works because you are declaring and defining i in if's conditional expression. It will always evaluated to true.
The second snippet does not compile because you are comparing a non-primary (because i is declared and defined here) expression in conditional expression with a primary expression.