proshantoS
proshantoS

Reputation: 55

Statement Vs. Block

I am a new java user. Recently I have learned that in java each statement is terminated with a semicolon (;) and each block is delimited by a pair ob curly braces - {} (please correct me if I am wrong).

But in many places I have found writers are saying that if statement. So my question is what is the difference between statement and block in java?

Thanks in advance.

Upvotes: 2

Views: 2842

Answers (4)

Clashsoft
Clashsoft

Reputation: 11882

A statement is part of a block. A block contains statements separated by semicolons, but is also a statement itself. In BNF:

statement := block | ...
block := '{' { block | (statement ';') } '}'

Upvotes: 0

RealSkeptic
RealSkeptic

Reputation: 34628

The Java Language Specification defines blocks and statements formally. To simplify what it says:

  • It is not true that every statement in Java ends in a semicolon. This is a convenient way to think about them, but it's not formally correct.
  • Some statements include a semicolon in their definition. Those are usually simple statements. For example:
    • The empty statement is just a semicolon: ;.
    • An expression statement is one of a permitted set of expressions, followed by a semicolon. new ClassName();, System.out.println();, i++; etc.
  • A block is a type of statement that contains declarations and other statements surrounded by braces { int i = 1; System.out.println(i); }.
  • Some statements are built using other statements. Their definition does not include a semicolon. If the sub-statement ends in a semicolon, then those statements end in a semicolon. The if statement falls into this category. It is built as if ( expression ) statement . So if the statement part inside it has a semicolon, it ends in a semicolon. If the statement inside it happens to be a block (which is a type of statement!), then it ends in a brace:

    if ( a == b )
       System.out.println(a);
    

    vs.

    if ( a == b ) {
       System.out.println(a);
    }
    

    In the first format, the substatement of the if is an expression statement (a method invocation is an expression statement, and expression statements end in a semicolon).

    In the second format, the substatement of the if is a block statement, which contains a single expression statement. So the if ends in the block`s brace, not in a semicolon.

Bottom line: statements are defined by belonging to certain formal categories. Some of them end in a semicolon, some do not.

Upvotes: 5

Tim
Tim

Reputation: 2912

If is actually both typically.

The if statement itself is just that, a control flow statement. If the statement is evaluated to true it executes the statement, or block after it. In other words, the block after an if is not required if you just wanted to execute a single line of code.

So, and pardon my of the cuff java, you could have either of these:

if(someboolean)
    DoSuff();

Or

 if(someboolean) {
    DoSuff();
  }

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html

Upvotes: 1

Crazyjavahacking
Crazyjavahacking

Reputation: 9697

Your definitions are correct. if is a statement even if it does not end with semicolon.

Generally speaking if the program construct produces a value then it is expression, otherwise it is statement. In Java unlike other languages (Scala, Groovy, ...) if is statement and not expression.

Upvotes: 1

Related Questions