BabyboB
BabyboB

Reputation: 55

What is Short if statements in java?

what is short if statement and no short if statement. I found this in Java Language Specification document. It says as follows

"Statements are thus grammatically divided into two categories: those that might end in an if statement that has no else clause (a "short if statement") and those that definitely do not.

Only statements that definitely do not end in a short if statement may appear as an immediate substatement before the keyword else in an if statement that does have an else clause.

This simple rule prevents the "dangling else" problem. The execution behavior of a statement with the "no short if" restriction is identical to the execution behavior of the same kind of statement without the "no short if" restriction; the distinction is drawn purely to resolve the syntactic difficulty."

Can anyone please explain this ?

Upvotes: 1

Views: 784

Answers (4)

OneCricketeer
OneCricketeer

Reputation: 191874

Divided into two categories:

those that might end in an if statement that has no else clause (a "short if statement")

if (condition) {

} 

and those that definitely do not

if (condition) {

} else { 

} 

Leave out the curly brackets, you get one line if and else statements.

if (condition) action();  // short-if 
// else doOther();  // optional 

Upvotes: 2

Nier
Nier

Reputation: 658

Statements are thus grammatically divided into two categories: those that might end in an if statement that has no else clause (a "short if statement") and those that definitely do not.

It means statements are divided into StatementNoShortIf and Statement. Which Statement can appeared in a "short if statement" and StatementNoShortIf cannot.

A short if statement is a if statement without else clause.

IfThenStatement: // short if statement
    if ( Expression ) Statement

IfThenElseStatement: // not short if statement
    if ( Expression ) StatementNoShortIf else Statement

IfThenElseStatementNoShortIf: // not short if statement
    if ( Expression ) StatementNoShortIf else StatementNoShortIf

Only statements that definitely do not end in a short if statement may appear as an immediate substatement before the keyword else in an if statement that does have an else clause.

Only StatementNoShortIf can appear between the if and else in a non-short if statement.

And such StatementNoShortIf must be an immediate substatement of that if else statement.

This simple rule prevents the "dangling else" problem. The execution behavior of a statement with the "no short if" restriction is identical to the execution behavior of the same kind of statement without the "no short if" restriction; the distinction is drawn purely to resolve the syntactic difficulty.

Therefore, consider the dangling else problem sample like this:

if (door.isOpen())
    if (resident.isVisible())
        resident.greet("Hello!");
else door.bell.ring();  // A "dangling else"

The else cannot be the part of the first if.

If so, according to the immediate substatement rule, line 2-3 need to be a StatementNoShortIf statement, but it is actually a IfThenStatemtn (short if statement) because StatementNoShortIf always needs an else. Therefore, the else must be a part of the second if, thus the dangling else problem is solved.

Upvotes: 1

Edwin Buck
Edwin Buck

Reputation: 70949

According to the definition in the paragraph you referenced; a short if statement is an if statement that fits the patterns

if (condition) {
}

if (condition) {
} else if (condition) {
}

A non-short if statement doesn't fit that pattern, for example it might be any of the following

if (condition) {
} else {
}

if (condition) {
} else if (condition) {
} else {
}

and so on.

Basically, the "short if" statement is an if statement that is known to not have an else block.

Upvotes: 4

Paul Boddington
Paul Boddington

Reputation: 37655

In addition to the other 2 answers, it's good to see what the problem is that this rule avoids.

For example, what does this print?

int a = 0; if (a == 1) if (a == 2) System.out.println("A"); else System.out.println("B");

It could be interpreted as

int a = 0;
if (a == 1)
    if (a == 2)
        System.out.println("A");
    else
        System.out.println("B");

(which would print nothing), or as

int a = 0;
if (a == 1)
    if (a == 2)
        System.out.println("A");
else
    System.out.println("B");

(which looks like it should print "B").

However the rule excludes the second of these, because this if (a == 1) part does have an else clause, yet the statement if (a == 2) System.out.println("A"); is a short if.

Therefore this prints nothing.

On the other hand, if the if (a == 2) was not a short if, it would look like this, and there would be no ambiguity to worry about.

int a = 0;
if (a == 1)
    if (a == 2)
        System.out.println("A");
    else
        System.out.println("C");
else
    System.out.print("B");

Upvotes: 1

Related Questions