Expressions, Statements, and Blocks in java 7

In this section we will discuss expressions, Statements and Blocks in java 7. This is one type of Language Fundamentals.

Expressions, Statements, and Blocks in java 7

Expressions, Statements, and Blocks in java 7

In this section we will discuss expressions, Statements and Blocks in java 7. This is one type of Language Fundamentals.

Expressions :

An expression can be formed by combining literals, variables and operators. It is made according to the language syntax that evaluate some value. Usually it complete some work of your program. It is used in computing and assigning values to your variables and also very helpful to control your program flow.

Example :

Here some expressions example -
int  number=10;        //number=10 is expression.
String test="Hello";//test="Hello" is expression.
System.out.println("Number = " + number);  //"Number = " + number   is expression.

You can also create compound expressions with the help of other smaller expressions. It is necessary that data types of the smaller expressions matches with the compound statements.

x=(y*20) - (z/10); //unambiguous

For compound expression unambiguous recommended.

x=y*20 - z/10 ; //ambiguous

Statements :

When you write a complete expression with semicolon(;) it creates a statement. You can say a statement is a complete unit of execution.
Assignment expressions, Any use of ++ or --, Method invocations, Object creation expressions are used in foundation of a statements. Such kind of statements are also said expression statements.

Example :
number= 100; //assignment statement

number++; //increment statement

System.out.println("Hello Roseindia!");// method invocation statement

Roseindia myRose = new Roseindia ();// object creation statement

Blocks :

When you group zero or more statements by using some balanced braces then it forms block. So you can say a block is a group of any number of statements.

Example :

if (some condition) { // begin block 1
System.out.println("Condition is true.");
} // end block one
else { // begin block 2
System.out.println("Condition is false.");
} // end block 2

Example : This example

class ExpressionStatementBlock {
	public static void main(String[] args) {
		int x = 10; // statement, x=10 is expression
		int y = 20; // statement, y=20 is expression
		if (x > y) {// begin block
			System.out.println("x is greater than y."); // expression and statement.
											
		}// end block
		else {// begin block
			System.out.println("y is greater than x."); // expression and statement.
										
		}// end block
	}
}

Output :

y is greater than x.