Explain about for statement with an example.


 

Java Statements

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 writing data to a file, etc.

Java statements can be broadly classified into three categories:

  • Declaration statement
  • Expression statement
  • Control flow statement

1. Java Declaration Statement

A declaration statement is used to declare a variable. For example,

int num;
int num2 = 100;
String str;

2. Java 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. Java 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 statementwhile loop statement and for loop statement are examples of control flow statements.




Java compiler executes the java code from top to bottom. The statements are executed according to the order in which they appear. However, Java provides statements that can be used to control the flow of java code. Such statements are called control flow statements.

Java provides three types of control flow statements.

  1. Decision Making statement(1. if statement,2. if-else statement,3. lse-if statement,4. Nested if-statement,Switch Statement)
  2. Loop statements(for loop,while loop,do-while loop)
  3. Jump statements(break and continue)