If, if else, else control Statement in Java With Example

Basic Java a program usually executes and express the results from top to bottom except when we use control statements, we can control the order of execution of the program, based on logic and values.

In Java, control statements can be divided into the following three categories:

  • Selection Statements
  • Iteration Statements
  • Jump Statements

Selection Statements

If Statement 

 if condition Java example

The if statement is used to check conditions and if the condition is true, It runs a block of statements which called the If statement, else we process another block of statements, called the else block statement which is optional.

Syntax

 

if (condition) {
    block of code to be executed if the condition is true
}

If statement

 

if – Else statement

The else statement specifies a block of code to be executed when if block conditions false.

Syntax

if (condition) {
    block of code to be executed if the condition is true
} else {
    block of code to be executed if the condition is false
}

 

Else if statements

The else if statement specifies a new condition if the first condition is false:

Syntax

if (condition1) {
    block of code to be executed if condition1 is true
} else if (condition2) {
    block of code to be executed if the condition1 is false and condition2 is true
} else {
    block of code to be executed if the condition1 is false and condition2 is false
}

If else statement

 

Add Comment