Statements in java
What is Statement in java:
A statement in java is an instruction that explains what will happen based on the condition.
Types of java statements:
There are different statements in java
- Expression statement
- Declaration statement
- Control flow statement
Expression statement:
An expression is used to generate a new value and we can also assign a new value to a variable. it is the combination of values, variables, operators, and methods calls.
There are three types of expressions in Java:
- The expression is used to produce a value. we should enclose the expression in the parentheses that will be evaluated first, after that rest of the expression which is present will be evaluated.
- Expressions are used to assign a new value to a variable.
- Expression is used to neither produces any result nor assign a value like an increment or decrement
- expressions that produce a value use a high range of Java arithmetic, comparison, or conditional operators.
- arithmetic operators like +, *, /, <, >, ++ and %. Some conditional operators are like ||, and the comparison operators are like <, <= and >.
Example1:
// expression
number = 10
// statement
number = 10;
Example 2:
// expression
++number
// statement
++number;
Declaration statement:
Declare statements are used to declare the variable by specifying its data types and name.
Syntax:
Data_type variable_name = value;
Example:
public class CreateVariable {
public static void main(String[] args)
{
//variable declaration
int student_id = 10;
String student_name = "Java coder";
double numbers = 3.21;
Boolean shows = true;
System.out.println("Name:"+student_name+ "\nAge:" +student_id);
System.out.println("Number:"+numbers+ "\nBoolean:" +shows);
}
}
Output:
Name: java coder
Age:10
Number:3.21
Boolean: true
Control statements:
Control statements are used to decide the flow of a java program. The control statements are used to interrupt a flow of control of a particular section of a program based on the condition.
Control statements are divided into different types
- Conditional or Selection Statements
- if Statement
- if-else statement
- if-else-if statement
- switch statement
- Loop or Iterative Statements
- while Loop
- do-while Loop
- for loop
- for each Loop
- Flow Control or Jump Statements
- Break
- Continue
- return
Example:
//declaring the statement
int number;
//expression statement or initialising the value
number = 250;
//control flow statement
if (number > 50)
{
//expression statement
System.out.println(number + " is greater than 100");
}
Output:
250 is greater than 100