Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Java programming if condition with example

Posted on February 19, 2016January 29, 2020

Table of Contents

  • Java programming if condition with example
    • If condition Java
    • if-else statement
    • if else-if else 

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

 

 

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes