# Complex if statement

Ranch Hand
Posts: 187
25
It is given the following code.
I am confused with this if statement. I have learned that if there is no braces of if statement only one statement is included to this if which comes after if. In this if statement I think that:
• the first if can include only one statement and this statement is second if.
• the second if can include only one statement and this statement is third if.
• the third if can include only one statement and this statement is System.out.println(1); .

• As this rule I supposed that the first else statement can't be included to the third if and this else statement belong to the first if statement. But it is impossible because if it is true else if can't come after else. As well this code does compile fine.
I wrote this code in Netbeans and the view of code was clear after pressing alt+shift+F.

But I can't understand everything clearly yet. What is my mistake? Which rule do I understand wrong? How can I figure out this if statement with correct way?

Roel De Nijs
Sheriff
Posts: 10662
144
• 1
Mushfiq Mammadov wrote:I have learned that if there is no braces of if statement only one statement is included to this if which comes after if.

That's indeed true!

Mushfiq Mammadov wrote:In this if statement I think that:
• the first if can include only one statement and this statement is second if.
• the second if can include only one statement and this statement is third if.
• the third if can include only one statement and this statement is System.out.println(1); .

• Until here you are absolute ly spot-on!

Mushfiq Mammadov wrote:As this rule I supposed that the first else statement can't be included to the third if and this else statement belong to the first if statement. But it is impossible because if it is true else if can't come after else. As well this code does compile fine.

Now you have to match the else statements with the appropriate if statement. If curly braces are used, it's pretty simple. If no curly braces are used, the rule is also very simple: an else clause belongs to the innermost if to which it might possibly belong. So in this code snippetit seems (based on the indentation) that the else statement belongs to the first if statement. But that's wrong! The else statement belongs to the innermost if statement, which is the second if statement. So this code snippet is the improved version of the previous code snippet (using proper indentation and curly braces).

Now let's see if you can apply this rule to your own complex if statement and end up with the same result as NetBeans.

Hope it helps!
Kind regards,
Roel

PS. You'll find the same statement/rule about else statements in the JLS.

Ranch Hand
Posts: 187
25
Roel De Nijs wrote: If no curly braces are used, the rule is also very simple: an else clause belongs to the innermost if to which it might possibly belong.

Perfect That is it which I want to know. This rule is written in JLS obviously. It is an important fact. Thanks a lot, Roel!

Roel De Nijs
Sheriff
Posts: 10662
144
As always: glad I could help!

Campbell Ritchie
Sheriff
Posts: 51344
86
Mushfiq Mammadov wrote: . . . This rule is written in JLS obviously. . . .
Youi would have to infer that from the form of the grammar; you will find the relevant part here in the JLS.
IfThenElseStatement:
if ( Expression ) StatementNoShortIf else Statement
As you see, there is only one statement intervening between the (expression) and the else. So the else corresponds to the last if before it, and that is the most deeply nested if. You would have to search the JLS to find out what “no short if” means.

Roel De Nijs
Sheriff
Posts: 10662
144
Campbell Ritchie wrote:
Mushfiq Mammadov wrote: . . . This rule is written in JLS obviously. . . .
Youi would have to infer that from the form of the grammar; you will find the relevant part here in the JLS.

Why would you try to do something really difficult and hard (infer something from the grammar) when you can have it for free without any effort As I mentioned in my 1st post (in this thread), this rule is clearly mentioned in the JLS as well
JLS, 14.5. Statements wrote:The Java programming language, like C and C++ and many programming languages before them, arbitrarily decrees that an else clause belongs to the innermost if to which it might possibly belong.

Campbell Ritchie
Sheriff
Posts: 51344
86
Roel De Nijs wrote: . . . Why would you try to do something really difficult and hard . . . when you can have it for free without any effort . . .
Do you mean I would have to read the JLS?

I was really caught out there, wasn't I.

Ranch Hand
Posts: 187
25
Campbell Ritchie wrote:You would have to infer that from the form of the grammar; you will find the relevant part here in the JLS.

...
The following are repeated from §14.9 to make the presentation here clearer:
...

But I must admit that "14.5. Statements" was more helpful for me than that other

fred rosenberger
lowercase baba
Bartender
Posts: 12263
36
There is never any reason to do this:

All you need is

Liutauras Vilda
Bartender
Posts: 2766
111
And here is the example, what kind of dangerous exist when writing "== true":
Can you notice what is likely wrong here?

Guillermo Ishi
Ranch Hand
Posts: 789
It will help you if you intent it correctly. If it was Python it wouldn't compile unless it was indented like below.

Campbell Ritchie
Sheriff
Posts: 51344
86
Guillermo Ishi wrote:. . . If it was Python it wouldn't compile unless it was indented like below.
. . .
Unfortunately, Java® code will compile if it has confusing indentation. Some exam books seem to make the most of that, presenting incorrectly‑indented code in order to confuse the reader.

Roel De Nijs
Sheriff
Posts: 10662
144
fred rosenberger wrote:There is never any reason to do this:

True! In actual application code you won't write code like that, but you could encounter it on a certification exam. Just like you could encounter the more tricky version with the assignment operator (already posted by Liutauras).

Roel De Nijs
Sheriff
Posts: 10662
144
Guillermo Ishi wrote:It will help you if you intent it correctly. If it was Python it wouldn't compile unless it was indented like below.

I am not familiar with Python so I don't argue about the so-called correct indentation in Python.

I just want to make sure there's no confusion about what's correct in Java. And the provided code snippet is NOT the correct indentation in Java! The last else statement belongs to the else if statement (and thus to the second if statement, not the first if statement).

Liutauras Vilda
Bartender
Posts: 2766
111
Guillermo Ishi wrote:It will help you if you intent it correctly. If it was Python it wouldn't compile unless it was indented like below.

Guilermo, you're mistaken about LINE 11. It belongs to other branch.

Roel De Nijs
Sheriff
Posts: 10662
144
Campbell Ritchie wrote:Some exam books seem to make the most of that, presenting incorrectly‑indented code in order to confuse the reader.

It's not to confuse the reader, but to prepare the reader for potential exam questions with poorly indented code...

Guillermo Ishi
Ranch Hand
Posts: 789
Liutauras Vilda wrote:
Guilermo, you're mistaken about LINE 11. It belongs to other branch.

LOL. You're right. I didn't notice the "else if" stuck in there. Somebody mentioned the rule is the else matches the innermost possible if. Also, "else if" cannot follow "else" in in the same block. It also seems that if there is a "else if" preceeding a loose else, the loose else goes with the else if.
Rules combined, you get:

Guillermo Ishi
Ranch Hand
Posts: 789
Roel De Nijs wrote:
I am not familiar with Python so I don't argue about the so-called correct indentation in Python...the provided code snippet is NOT the correct indentation in Java! The last else statement belongs to the else if statement (and thus to the second if statement, not the first if statement).

That is true. I just didn't notice one of the elses was really an else if. In Python there are no brackets, so the blocks depend only on indentation; indenting isn't just for clarity. It's okay, but it does make it harder to insert a temporary line to test something.

Roel De Nijs
Sheriff
Posts: 10662
144
Guillermo Ishi wrote:In Python there are no brackets, so the blocks depend only on indentation; indenting isn't just for clarity. It's okay, but it does make it harder to insert a temporary line to test something.

Long live the curly braces!

Guillermo Ishi
Ranch Hand
Posts: 789
Roel De Nijs wrote:
Guillermo Ishi wrote:In Python there are no brackets, so the blocks depend only on indentation; indenting isn't just for clarity. It's okay, but it does make it harder to insert a temporary line to test something.

Long live the curly braces!

In P. you could indent the last else to where it works out in Java, or you could indent it to belong to the outer if. What you see is what you get

Campbell Ritchie
Sheriff
Posts: 51344
86
But we are not writing Python. We are writing Java® and have to follow Java® syntax rules.

Guillermo Ishi
Ranch Hand
Posts: 789
Campbell Ritchie wrote:But we are not writing Python. We are writing Java® and have to follow Java® syntax rules.

Campbell Ritchie
Sheriff
Posts: 51344
86
That you have to tell people what the Java® rules are. It doesn't help anybody to say it would work differently in a different language. We see lots of Java® beginners who get dangerously confused because they think something works in Java® the same way it does in language X.

In most instances, language X seems to be C++.