Java programming if condition with example

Java programming if condition with example

In this article, we will discuss the Java programming if condition with example

In this post, we will learn ow to use Java programming if statements with examples

If condition Java

if condition one of the decisions making statements in Java similar other languages C, C++, etc; if statement can be followed by else if or else statements, which executes when boolean expression is false
In this tutorial, we will learn about three types of if statements
  • if statement
  • if else statement
  • if else-if  else statement

 

if statements
Syntex of if statements

if (Test condition) { //code to be execute only when condition is true

}

Syntex of if inside another if statements – Nested if statements

if (Test condition){

if (Test condition){

//code to be execute only when condition is true

} //code to be execute only when condition is true

}

 

 

Program 1

When the above code is executed,it produces the following result
marks is greater than 60 


When the test condition is false
Output
No output will be displayed on the screen

 

Program 2

This program contains two if statements, one if statement inside another if

When the above code is executed,it produces the following result

age is enough
You can enter university


When both test conditions are false




 

Output
No output will be displayed on the screen


if-else statement

if ( condition_to_test )  { // Executes when the boolean expression is true

}        else { // Executes when the boolean expression is false

       }
Flow diagram of if-else condition in java

 

Example – if – else
program 1
class user {
public static void main(String arg[]){
int age=17;
if(age<=18)
{                                   //Boolean Exprssion is true
System.out.println("the user is a younger");    // So this statement is display
}
else
{
System.out.println("user old man");
}


}


}

 

In this program, When the test expression is true, statements of inside the “if” are executed. otherwise, statements of inside the “else” are executed
When the above code is executed,it produces the following result

output – the user is a younger


if else-if else

Syntex in if else – if else in java

  if ( Boolean expression_one_To_Test ) { // Executes when the boolean expression is true if boolean expression is  false loop goes to next loop(else if)

}

else if ( //condition_two_To_Test

{

// Executes when the boolean expression is true

}

else if ( //condition_three_To_Test  )

        {

// Executes when the boolean expression is true

}
else {

          // Executes when the none of the above condition is true

}

Flow diagram of if -else if else condition in java

When the boolean expression evaluates to true, The block of code of inside of “if” will be executed, otherwise block of code inside else if and else will be executed, based on the above the flow diagram

Example 2 – if else if else
class ifpass {
    public static void main(String[] args) {

        int testscore = 16;
        String grade;

        if (testscore >= 80) {    // this statement is false 
            grade = "merit";
        }
else if (testscore >= 50) {  // this statement is false 
            grade = "pass";
        } else {
            grade = "Fail";         // all condition are false So  else part is display
        }
        System.out.println("Grade = " + grade);
    }

}

When the above code is executed,it produces the following result


Example 2 – if else if – else

class loopifdemo {
  public static void main(String[] args) {
      int testscore = 76;
      char grade;
      if (testscore >= 90) { // this condition is false because 76<90
          grade = ‘A’;
      } else if (testscore >= 80) { // this condition is false because 76<80
          grade = ‘B’;
      } else if (testscore >= 70) { // this condition is true this part is  executed
          grade = ‘C’;
      } else if (testscore >= 60) { // skip this step because above condition is true
          grade = ‘D’;
      } else {                               //skip this step
          grade = ‘F’;
      }
      System.out.println(“Grade = ” + grade);
  }

}

Out put here

When the above code is executed,it produces the following result

Grade – C

 

Similar post

If statements in C language

Nested if in C language

If statements in C++ language

Nested if in C++ language

If statements in Python language

Nested if statements in Python

Nested if in Java language

 

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.