Table of Contents
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 (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
marks is greater than 60
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
if ( condition_to_test ) { // Executes when the boolean expression is true
} else { // Executes when the boolean expression is false
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"); } } }
output – the user is a younger
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
}
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
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
}
Out put here
When the above code is executed,it produces the following result
Grade – C
Similar post
If statements in Python language
Nested if statements in Python
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.