Without curly braces, the compiler makes the code 6 lines of code starting with the first if test a single block. That means the second else statement is floating without an if statement to call its own.
Janet Smith wrote:I've tried both of these out by tying to compile them and both behave as each test says they will. The first example won't compile and the 2nd one will. It could be that I need to step away and come back later with fresh eyes but at the moment I can't see the difference between them. It looks like both of these have a 2nd floating else that does not correspond to an if. But the 2nd one clearly does correspond to an if. Can anyone shed any light?
Janet Smith wrote: I can't see the difference between them. It looks like both of these have a 2nd floating else that does not correspond to an if. But the 2nd one clearly does correspond to an if. Can anyone shed any light?
Janet Smith wrote:Ok, from both your comments looking at the code again. In the code snippet that does not compile we have more than one line of code after the first else statement. If we wanted this included in the else we’d surround that with curly braces.
I can then see if I add something similar to the code snippet that does compile I get the same issue.
And I can see if I surround the two lines beneath the else in curly braces both code snippets compile.
Janet Smith wrote:As code that is executing after the if statement is complete so we then have an else that is floating around with no corresponding if?
If so that is a tricky one. I think once I got the first question I just had it in my head that any appearance of if else else would cause a compiler failure. So its interesting to come across this one.