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.
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); .
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.
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.
Youi would have to infer that from the form of the grammar; you will find the relevant part here in the JLS.Mushfiq Mammadov wrote: . . . This rule is written in JLS obviously. . . .
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.IfThenElseStatement:
if ( Expression ) StatementNoShortIf else Statement
Campbell Ritchie wrote:
Youi would have to infer that from the form of the grammar; you will find the relevant part here in the JLS.Mushfiq Mammadov wrote: . . . This rule is written in JLS obviously. . . .
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.
Do you mean I would have to read the JLS?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 . . .
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:
...
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
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.Guillermo Ishi wrote:. . . If it was Python it wouldn't compile unless it was indented like below.
. . .
fred rosenberger wrote:There is never any reason to do this:
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.
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.
Campbell Ritchie wrote:Some exam books seem to make the most of that, presenting incorrectly‑indented code in order to confuse the reader.
Liutauras Vilda wrote:
Guilermo, you're mistaken about LINE 11. It belongs to other branch.
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).
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.
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!
Campbell Ritchie wrote:But we are not writing Python. We are writing Java® and have to follow Java® syntax rules.
Be reasonable. You can't destroy everything. Where would you sit? How would you read a tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|