If-else statement in java is used to test the condition. As the name if-else, if the condition of if is true then, the if-statement block will execute else it will execute else block.
In this article, we will cover the if-else statement in java and we will learn how to use them. we will also cover different types of if-else statements in java.
Types of if-else statements in java
- if-statement
- if-else statement
- if else-if else ladder
- Nested if
if-statement
The syntax for if statement in java
if (condition) {
// statement block
}
Example
int number = 5;
if (number == 5) {
System.out.println("The number is equal to 5 ");
}
Explanation
In the above example, we have declared an int variable named as the number
In the if condition we are checking if the number is equal to 5
In this example, the condition is true and hence, the if-statement block will execute
The output of the above example will be
The number is equal to 5(code-box)
Let's create a one java program to check even numbers
package learnjavaskills.in;
public class CheckEvenNumber
{
public static void main(String[] args)
{
int[] numbers = {1,2,3,4,5,6,7,8,9,10};
System.out.println("Even numbers are");
for (int i=0; i<=9; i++)
{
if (numbers[i] % 2 == 0)
{
System.out.print(numbers[i] + " ");
}
}
}
}
Output
Even numbers are
2 4 6 8 10(code-box)
Explanation of the above code
In the above java program, we are checking the given number from 1 to 10 is even or not
We have declared an array of int which is numbers. If you want to know more about array in java then read this
We are iterating array of int numbers using for loop. If you want to know more about loops in java then, read this
Inside of if statement we are given one condition such as numbers[i] % 2 == 0
. This condition means if the number from the array is divisible by 2 and the remainder is equal to 0 then, we are considering the given number is an even number
And finally, we are printing even number
if-else statement in java
The Syntax for the if-else statement
if (condition) {
// statement block
} else {
// statement block
}
Example
int number = 2;
if ( number == 2 ) {
Systems.out.println("condition is true");
} else {
Systems.out.println("condition is false");
}
Explanation
In the above example, we have declared int
number = 2 and we are checking if number equal to 2
Here, the number is equal to 2 then in this case, the if-statement block will execute
The output will be
condition is true(code-box)
Prime number program using if-else statement
package learnjavaskills.in;
public class CheckPrimeNumber
{
public static void main(String[] args)
{
int number = 7;
boolean isPrimeNumber = false;
for (int i=2; i<=number/2; i++)
{
if (number % i != 0)
{
isPrimeNumber = true;
}
}
if (isPrimeNumber)
{
System.out.println("Number " + number + " is a prime number");
}
else
{
System.out.println("Number " + number + " is not a prime number");
}
}
}
Output
Number 7 is a prime number(code-box)
Explanation
In the above java program, we are checking whether number 7 is a prime number or not
We have declared an int variable with name as a number which has value 7 and we also used a boolean primitive data type to use as a flag
In for loop, we are iterating till number/2 and inside for loop statement, we have one if statement to check if the number is divisible by 2 and 3 as per our for loop condition
If the remainder of the number is not zero then, we are considering a number is a prime number. So here, inside if statement, we assign an isPrimeNumber to true
After for loop finished his task, we have created an if-else statement to check the boolean value of isPrimeNumber
If isPrimeNumber is true that means the given number is a prime number
And finally, we are printing number is prime or not
If you want to learn more about the loops in java then learn from loops in java then, read Loops in java
if else-if else ladder
Syntax
if (condition) {
// statement block
} else if(condition) {
// statement block
} else {
// statement block
}
Explanation
In the above example, we are checking, if the given name which has value LearnJavaSkills.in contain learn or program
Java program to check grades using if else-if else ladder
package learnjavaskills.in;
public class MarkSheet
{
public static void main(String[] args)
{
int percentage = 70;
if (percentage >= 90)
{
System.out.println("Grade A+");
} else if (percentage >= 80 && percentage < 90 )
{
System.out.println("Grade A");
} else if (percentage >= 70 && percentage < 80)
{
System.out.println("Grade B+");
}else if (percentage >= 60 && percentage < 70)
{
System.out.println("Grade B");
}else if (percentage >= 50 && percentage < 60)
{
System.out.println("Grade c");
} else
{
System.out.println("Fail");
}
}
}
Output
Grade B+(code-box)
Explanation
In the above java program, we are checking grades from the percentage
If the percentage is > 90, then it means Grade A+ and if the percentage is >= 80 but less then 90 then we are considering Grade A
And So on
Nested if
Syntax
if (condition) {
if (second condition) {
// stattement block
}
}
Java Program to for nested if statement in java
package learnjavaskills.in;
public class LearnJavaSkills
{
public static void main(String[] args)
{
String language = "Java";
int experiance = 2;
if (language.equals("Java"))
{
if (experiance >= 2)
{
System.out.println("2+ year of experiance in java");
}
}
}
}
Output
2+ year of experience in java(code-box)
Explanation
In the above code, we are checking if developer know java and is he has 2+ year of experience in java
Another java Program for nested if in java
package learnjavaskills.in;
public class LearnJavaSkills
{
public static void main(String[] args)
{
String gender = "Male";
int age = 20;
double height = 5.7;
if (gender.equals("Male"))
{
if (age > 18)
{
if (height > 5.5)
{
System.out.println("Display the cloths");
}
}
}
}
}
Output
Display the cloths(code-box)
Explanation
In the above code are have two nested if statement to check age, gender and height
If the height, gender and age match the condition then, we are displaying the cloths
Conclusion
Today we have learned what is if else statements in java and what are the various types of if-else statement in java. We also learn how to use them.
Thanks for reading and keep learning
(getButton) #text=(Next: Array in java) #icon=(link) #color=(#2339bd)