Reputation: 3311
class if1
{
public static void main(String args[])
{
int a = 100;
if(a==100); //Expected a compile error but did not get one.
}
}
I expected the compiler to issue me an error but surprisingly it compiled just fine. What is the rationale for a compiler to ignore whether the "if" statement has any statements to process or not. Why does it not throw an error like in the below case ?
class if2
{
public static void main(String args[])
{
int a = 100;
if(a == 101) //Compiler complains here...
else
{
System.out.println("in else");
}
}
}
In the above statement, the compile complains that "if" clause does not have anything to process.
Can someone tell me why ?
Upvotes: 3
Views: 4553
Reputation: 1
char charVar = ’z’;
Write an if
statement that if the variable holds a, the condition is true and then tell the user the value of your variable is a.
Upvotes: 0
Reputation: 68715
if(a==100); //Expected a compile error but did not get one.
No compiler error as this is assumed to be an empty if block as it is terminated by semicolon (;)
if(a == 101) //Compiler complains here...
This is an incomplete if block, there should be at least one statement in if or else it should be terminated by semicolon(;)
as above. As the if is not complete so else will also not make any sense to compiler.
Upvotes: 7
Reputation: 234795
According to the Java Language Specification, an empty statement (a semicolon by itself) is a perfectly legal statement that can be used anywhere a statement is allowed. Your first case
if (a==100);
is exactly that. The second case:
if (a==100)
else { . . . }
is missing the statement that is required for the if
branch. (See the JLS specification of the if
statement syntax.)
Upvotes: 0
Reputation: 1404
The simicolon is the empty statement. Writing ;
is very similar to {}
. It does nothing. The compiler expects a statement after the if condition. So having an else immediately after the if condition will give you a compiler error, but having a statement (including the empty statement) will not.
Upvotes: 0
Reputation: 41200
There is issue with sytax. java supports specified syntax for if-else
and if
.
if(a==100); // Empty if block
if(a==100) // if block following with single statement
System.out.println("a: "+a);
Both are valid if
statement.
For the case if-else
-
if(a==100)
System.out.println("a: "+a);
else
System.out.println("else ");
or
if(a==100){
System.out.println("a: "+a);
}else{
System.out.println("else ");
}
Both of this are valid. But
if(a == 101)
else{...}
This one is not invalid, due to if statement does not have proper ending.
Upvotes: 1
Reputation: 6602
c-heritage languages permit an empty statement which makes possible a for
loop of the form:
for(/*initialization*/;/*iteration*/;/*condition*/){}
This allows the programmer to optionally run code at initialization (such as for local variables), iteration (increment/decrement a value, etc), and finally test for a condition on which the loop will break.
Because this is possible, empty statements are also possible.
;
;;
if(false);
for(;;);
etc. Are all legal.
Upvotes: 0
Reputation: 500287
An if
statement without an else
permits the "then" clause to be empty; an if
statement with an else
does not.
The reasons for this are explained in some detail in the Java Language Specification (§14.5 Statements):
As in C and C++, the if statement of the Java programming language suffers from the so-called "dangling
else
problem," illustrated by this misleadingly formatted example:
if (door.isOpen())
if (resident.isVisible())
resident.greet("Hello!");
else door.bell.ring(); // A "dangling else"
The problem is that both the outer
if
statement and the innerif
statement might conceivably own theelse
clause. In this example, one might surmise that the programmer intended theelse
clause to belong to the outerif
statement.The Java programming language, like C and C++ and many programming languages before them, arbitrarily decrees that an
else
clause belongs to the innermostif
to which it might possibly belong. This rule is captured by the following grammar:....
Statements are thus grammatically divided into two categories: those that might end in an
if
statement that has noelse
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 keywordelse
in anif
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 shortif
" restriction; the distinction is drawn purely to resolve the syntactic difficulty.
Upvotes: 0
Reputation: 35557
if(a == 101)
here you are getting error due to this is not an statement. you have to put (;).
There is no issue code like if(a == 101);
We can do some thing like this with if
statements.
if(a==2)
System.out.println("This is correct");
so we can use this as follows too
if(a==0)
;
But you can't do like follows, Since you have to put (;)
if(a==2)
System.out.println("This is correct")
Then following also not valid
if(a==2)
Upvotes: 0
Reputation: 504
There is sytax error. java supports specified syntax for if-else and if.
if(a==100);
After the statement there is no semicolon(;) so block as it is terminated by semicolon
if(a==100)
System.out.println("a: "+a);
else
{
System.out.println("in else");
}
This code will be execute.
Upvotes: 0
Reputation: 213223
Ok, take it this way, your first statement is equivalent to:
if (a == 10)
; // Here you have one statement, but that's an empty statement.
So, you do have a statement, though an empty one. So, compiler is ok with it. Just putting a semi-colon is a way to write an empty block, without curly braces. So, the above if
block is really equivalent to:
if (a == 10) { }
In 2nd case,
if (a == 10)
// No statement here
The compiler expects a statement after the if
block, which it doesn't find.
Upvotes: 2
Reputation: 68915
if(a==100); //Expected a compile error but did not get one.
This is perfectly valid(not of any use though). You are saying if a variable is equal to 100 do nothing. Above is equivalent to
if(a==100){}
But
if(a == 101) //Compiler complains here...
else
{
System.out.println("in else");
}
in above case you have a else hanging in middle of nowhere. If-else is a construct and must be together(if statement can be independent though). Logically speaking why would there be an else if there is no if.
Upvotes: 0
Reputation: 172408
In the first there is one statement ;
if(a == 101)
; // Hence there is no compile time error.
But in the second thee is no statement. It is an empty block.
For the second statement you have to give it a block. Something like this:-
if(a == 101)
{
}
or
if(a == 101);
Upvotes: 2