Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, statements are the basic building blocks of a program. They control the flow of execution and perform various actions. In this tutorial, we will cover three types of statements: empty statements, compound statements, and expression statements.
1. Empty Statements
An empty statement in Java is simply a semicolon (;) without any code. It is used when you want to create a statement that does nothing. Empty statements are often used in loops and conditional statements when no action is required.
Example of an empty statement:
for (int i = 0; i < 10; i++) { // Empty statement - no action required ; }
In this example, the empty statement is used in a loop that iterates ten times but does not perform any action.
2. Compound Statements
A compound statement, also known as a block, is a group of statements enclosed in curly braces ({ }). Compound statements can be used wherever a single statement is expected, such as in loops, conditional statements, or methods.
Example of a compound statement:
public class Main { public static void main(String[] args) { int x = 5; if (x > 0) { // Compound statement System.out.println("x is positive"); System.out.println("x = " + x); } } }
In this example, the compound statement is used in an if
statement to group two System.out.println()
statements.
3. Expression Statements
Expression statements are statements that consist of an expression followed by a semicolon (;). Examples of expressions that can be used as expression statements include method calls, assignments, increments, and decrements.
Examples of expression statements:
public class Main { public static void main(String[] args) { int x = 5; // Assignment expression statement x++; // Increment expression statement x--; // Decrement expression statement System.out.println("x = " + x); // Method call expression statement } }
In this example, we have several expression statements, including assignments, increments, decrements, and a method call.
In this tutorial, we covered three types of statements in Java: empty statements, compound statements, and expression statements. Understanding these types of statements is essential for writing and organizing your Java code effectively.
Using Empty Statements in Java Code:
;
does nothing when executed.for (int i = 0; i < 5; i++);
Creating Compound Statements in Java:
{ int x = 5; int y = 10; }
Expression Statements in Java:
int result = x + y;
Java Empty Statement Use Cases:
for (int i = 0; i < 10; i++) { // Empty statement }
Benefits of Compound Statements in Java:
if (condition) { // Multiple statements }
Java Single-Line vs Multi-Line Statements:
int x = 5; // Single-line int y = 10; int result = x + y; // Multi-line
Java Expression Statement Examples:
int a = 5; a++; // Expression statement
Compound Statements and Control Flow in Java:
if (condition) { // True block } else { // False block }
Nested Compound Statements in Java:
if (outerCondition) { if (innerCondition) { // Nested block } }
Expression Statements in Java Loops:
for (int i = 0; i < 5; i++) { // Loop body }
Common Mistakes with Java Statements:
int x = 5;;