Java Statements

A Java program is made up of classes, these classes are made up of methods, constructors and variable definitions, these methods and constructors are made up of set of instructions and these set of instructions are called statements in Java. Statements are tasks carried out by the computer to solve a problem, they are the building blocks of a Java Program. In order to have a good understanding of Java Statement it will first be beneficial to understand how to break a solution down into algorithmic steps. Each step of an algorithm can be represented as a Java statement or statements.

Let us take for example a simple Java program that adds two integer numbers together, the algorithm for such problem will look like the following:

  1. Begin
  2. Declare and initialize variables x, y, z as numbers
  3. Enter two numbers x and y
  4. Are x and y values provided?
  5. a. If (4) is executed, then add x and y and assign result to z ==> z=x + y,  b. If (4) is not executed, display “please enter values x and y”
  6. Display result (value of z)
  7. End



 Now let’s break the algorithm down into statements;

To achieve Step2 of the algorithm, we need two java statements; declaration and initialization statements. These two statements can either be separated or combined into one as shown in the example below

int x;         //Declaration only
int y;         //Declaration only
x = 0;         //Initialization only
y = 0;         //Initialization only
int z = 0;     //Declaration and Initialization

To achieve Step 3, we need expression statements since we need to create an object of the Java Scanner class (The scanner class is one of the ways to read input from the keyboard) by calling its constructor and read user inputs as shown below

Scanner in = new Scanner(;
System.out.print(&Enter first integer value : &);
x = in.nextInt();
System.out.print(&Enter second integer value : &);
y = in.nextInt();

To achieve Step 4, we need control statements since we need to make a decision based on the validity of some conditions i.e. if values x and y have been provided by the user. So we can use the If else statement as shown below

if (x != 0 && y != 0) {
        } else {

Please note that “!=” means “not equals to”, click here  if you missed Java Operator post.

To achieve Step 5, we need expression statement as shown below

         z =x + y;

To achieve Step 6, we need expression statement again as shown below

System.out.println(“Sum of entered integers = ” + z);

System.out.println(“Please enter values x and y”);

So, we can see that without Java statements there is no Java program as each statement form a complete unit of execution that makes up a solution. Putting all the pieces of the statements in the example above together make what is shown in the code snippet below:




 Java Statements are not limited to the ones used in the example above. Therefore, I have categorized them into functional groups as shown below

Control Flow Statement: These statements either repetitive or conditional and just as the name implies, they are used to control the flow of execution of statements based on specified conditions. They include:

  • If statements
  • If then else statements
  • Switch statements
  • The while statements
  • For statement
  • Do – while statements

Branching Statements: They are statements used to change the normal flow of a program execution. They includes:

  • Return Statement
  • Continue
  • Break

Expression Statements: They are expressions can be made into a statement by terminating the expression with a semicolon (;).

  • Assignment expressions
  • Any use of ++ or
  • Method invocations
  • Class Initialization or Object creation expressions

In my next post, I will be sharing sample codes on how and when to use the different statements in your code. Until then, please run the sample code discussed in this post yourself. I would love to have your feedback. Happy coding!!!




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s