Java Statements

What is Java Statement?

  • In Java, a statement is a complete command of a program, and it is a line of code that expresses an action or a command to be executed by the computer. In other words, it is a single unit of execution that carries out a specific task or operation. Java is a structured programming language, and statements are used to control the flow of execution, perform operations, or define structures.
  • Each statement in Java typically ends with a semicolon (;). The sequence of statements makes up the Java program, and when the program is executed, the statements are executed one after another in the order they appear, unless the flow of execution is altered using control statements like loops and conditionals.
  • Java supports various types of statements, including but not limited to:
 Types of Statements:

Java statements can be categorized as:

  • Expression Statements
  • Declaration Statements
  • Control Statements

Expression Statements:

In Java, an expression statement is a type of statement that consists of a single expression and is terminated by a semicolon (;). An expression is a combination of literals, variables, operators, and method calls that are evaluated for a single value. The main purpose of an expression statement is to evaluate an expression for its side effects (for example, changing variables or summoning methods) rather than returning a value.

Here are some examples of expression Statements:

1. Assignment Expression:
int a = 10; // Assigns the value 10 to the variable 'a'.
2. Increment/Decrement Expression:
a++; // Increases the value of 'a' by 1.
b--; // Decrements the value of 'b' by 1.
3. Method Call Expression:
System.out.println("Hello Java"); // Calls the 'println' method to display the text.
4. Method Invocation with a Return Value (although not used as a value here, the method is invoked for its side effects):
int sum = addtwoNumbers(5, 7); // Calls the 'addtwoNumbers' method and assigns the result to 'sum'.
5. Object Creation Expression (creates a new object of a class):
Person person = new Person("John", 30); // Creates a new 'Person' object and assigns it to 'person'.

Expression statements are essential for their side effects, such as changing variables or invoking methods that carry out actions. Non-expression statements like control flow statements (if, switch, for, while, etc.) are utilized for their control flow effects (altering the program flow) instead of evaluating a value.

Declaration Statements:

We declare variables and constants in declaration statements by giving their name and data types. The Java program will use a variable that holds a value.

For example:
int numbers;
boolean married;
String message;

Also, we can initialize a value to a variable.

For example:
int numbers = 50;  
boolean married = true;  
String message = "Hello";

Java also enables the declaration of multiple variables in a single statement, provided that all the variables share the same data type.

For example:
int numbers, id_numbers, batch_numbers;  
boolean married = true, isContains = false;  
String message = "Hello", how are you;

Control Statements:

Control statements in Java are statements that allow you to control the flow of execution in a program. They determine which blocks of code should be executed based on specific conditions or how many times a block of code should be repeated. Control statements are crucial for making decisions, handling repetitions, and creating complex program structures. Java provides several types of control statements.

 

There are the following types of control statements:

1. Conditional or Selection Statements:
  • if Statement
  • if-else Statement
  • if-else-if Statement
  • switch Statement
2. Loop or Iterative Statements:
  • for Loop
  • while Loop
  • do-while Loop
  • for-each Loop
3. Flow Control or Jump Statements:
  • return
  • continue
  • break
Implement of Statement:
//Declaration Statement  
int numbers;  
//Expression Statement  
numbers = 400;  
//Control flow Statement  
if (numbers > 10 )  {  
//Expression Statement  
   System.out.println(numbers + " is greater than 100");  
}

 

Visited 1 times, 1 visit(s) today

Comments are closed.

Close