Java If Else Statement

Java If Else Statement

 

An “if structure” selects a statement or set of statements to execute on the basis of a condition. A statement or set of statements that is executed when a particular condition is true and ignored when the condition is false is called conditional statement.

Suppose a program displays pass if the student gets 40 or more than 40 marks. It displays fail when the marks are below 40. The program checks the marks before displaying the message. This process is known as decision making or selection.

The “if structure” is used to check the Boolean condition that results in either true or false. There are a number of if statements in Java.

  1. if statement
  2. if else statement
  3. nested if statement
  4. if else if ladder

 

Java if statement:

“If statement” is a decision making statement. It is the simplest form of “if construct”. It is used to execute or skip a statement by checking a condition.

The condition is given as a relational expression. If the condition is true, the statement after if statement is executed. If the condition is false, the statements after the “if statement” is not executed.

SYNTAX:

The following is the syntax of “if statement” in Java:

If (condition) {

Statement;

}

 

Flowchart of “if statement”:

flowchart-of-if-statement

EXAMPLE:

CODE:

public class ExampleIf {

public static void main (string [] args) {

int marks = 50;

If(marks>=40)

{

system. out. println (“Congratulations you have passed”);

}

}

}

OUTPUT:

Congratulations you have passed

 

Java if else statement:

It is another type of if statement. It executes one block of statement when the condition is true and the other when it is false. In any situation, one block is executed and the other is skipped. In if else statement:

  • Both blocks of statement can never be executed.
  • Both blocks of statements can never be skipped.

SYNTAX:

if (condition) {

   statement;

}

else {

  statement;

}

 

Flowchart of if else statement:

flowchart-of-if-else-statement

EXAMPLE:

CODE:

public class ExampleIfElse {

public static void main (string [] args) {

int n = 14;

if(n%2==0) {

  system. out. println (“n is even number”);

}

else {

   system. out. println (“n is odd number”);

       }

  }

}

OUTPUT:

n is even number

 

Java if else if ladder statement:

“If else if” ladder statement can be used to choose one block of statements from many blocks of statements. It is used when there are many options and only one block of statements should be executed on the basis of a condition.

 

Flowchart of if else if ladder statement:

flowchart-of-if-else-if-ladder-statement

SYNTAX:

The syntax of this structure is:

if (condition 1)

{

Block 1;

}

else if (condition 2)

{

Block 2;

}

else if (condition 3)

{

Block 3;

}

:

:

else

{

Block n;

}

Working of If else if:

The test conditions in if else statement with multiple alternatives are executed in a sequence until a true condition is reached. If a condition is true, the block of statements following the condition is executed. The remaining blocks are skipped. If a condition is false, the block of statements following the condition is skipped. The statement after the last else are executed if all conditions are false.

Example of if else if ladder statement:

Consider the following example in which we have displayed the grades of the students according to their marks using the “if else if” ladder statement:

CODE:

public class ExampleIfElseIf {

public static void main (string [] args) {

int marks = 50;

if (marks < 40) {

system. out. println (“You are fail”);

}

else if (marks > = 40 && marks < 50) {

system. out. println (“You obtained grade D”);

}

else if (marks > = 50 && marks < 70) {

system. out. println (“You obtained grade C”);

}

else if (marks > = 70 && marks < 80) {

system. out. println (“You obtained grade B”);

}

else if (marks > = 90 && marks < 90) {

system. out. println (“You obtained grade A”);

}

else if (marks > = 90 && marks < 100) {

system. out. println (“You obtained grade A+”);

}

else if (marks == 100)

{

system. out. println (“You obtained full marks”);

}

else {

system. out. println (“Invalid number”);

}

}

}

OUTPUT:

You obtained grade C

In the above example an integer type variable named “marks” has been declared. The variable is initialized with a value that is 50. Then we used the “if else if” ladder statement to check the conditions. The corresponding message will be printed according to the matched condition for example if marks are smaller than 40 the message will be printed that is “You are fail” and so on. And if the marks are not in range or has some value for example the value exceeding 100 then the “else” part of the “if else if” ladder statement will be executed and the message will be printed that “Invalid number”.