A statement specifies an action in a Java program. For example, a statement may tell the add of values of x
and y
and assign their sum to the variable z
. It then prints a message to the standard output or writes data to a file, etc.
Java statements can be broadly classified into three categories:
- Declaration statement
- Expression statement
- Control flow statement
1. Declaration Statement
A declaration statement is used to declare a variable. For example,
int num;
int num2 = 100;
String str;
2. Expression Statement
An expression with a semicolon at the end is called an expression statement. For example,
/Increment and decrement expressions
num++;
++num;
num--;
--num;
//Assignment expressions
num = 100;
num *= 10;
//Method invocation expressions
System.out.println("This is a statement");
someMethod(param1, param2);
3. Flow Control Statement
By default, all statements in a Java program are executed in the order they appear in the program. Sometimes you may want to execute a set of statements repeatedly for a number of times or as long as a particular condition is true.
All of these are possible in Java using flow control statements. The if statement
, while loop statement
and for loop statement
are examples of control flow statements.
if(condition) {
System.out.println("Condition is true");
} else {
System.out.println("Condition is false");
}
You can learn more about these statements in separate tutorials in this blog.
Happy Learning !!