In this tutorial, we will learn about java if-else statements with step by step explanation.
In Java, the if statement is used to execute a block of code if a condition is true. If a condition evaluates to false, the if…else statement is used alongside an if statement to run code. The if…else statement is also used to test multiple conditions.
In Java, decision-making helps in the creation of decision-driven statements and the execution of a specific set of code in response to certain conditions. If a condition is true, the if statement will execute a block of statements, if the condition is false, the if statement will not execute a block of statements.
Java supports the following mathematical logical conditions:
Less than | a < b |
Less than or equal to | a <= b |
Greater than | a > b |
Greater than or equal to | a >= b |
Equal to | a == b |
Not Equal to | a != b |
The conditional statements in Java are as follows:
if | If a specified condition is true, a block of code will be executed. |
else | if the same condition is false, else to define a code block to be performed |
else if | If the initial condition is false, use else if to define a new condition to test. |
switch | the switch is used to specify a large number of alternative code blocks to be executed. |
The if statement in Java is used to test the condition. It determines whether a boolean condition is true or false. In Java, there are several types of if statements.
- if statement
- if-else statement
- if-else-if ladder
- nested if statement
Contents
Java if Statement
The if statement in Java is the most basic decision-making statement. It is used to determine whether or not a specific statement or block of statements will be executed, i.e. if a certain condition is true, then a block of statements is executed, otherwise not.
if(condition) { … } |
Example:
/A Java programme to show how to utilise the if statement. public class Sample { public static void main(String[] args) { int value=10; if(value>7){ System.out.print("Value is greater than 7"); } } }
Output:
Value is greater than 7 |
Java if-else Statement
The condition is also tested using the Java if-else expression. If the condition is true, the if block is executed, otherwise, the else block is run.
if(condition) { //… if the condition is true } else{ //…if the condition is false } |
Example:
// This is a Java programme that shows how to use the if-else statement. public class Sample { public static void main(String[] args) { int n =10; // Find out if the number is divisible by two. if(number%2==0){ System.out.println("even number"); }else{ System.out.println("odd number"); } } }
Output:
even number |
Using Ternary Operator
The Java ternary operator allows you to build a single-line if statement. The result of a ternary operator can be either true or false. The ternary operator is used to solve this problem. To make your code easier to read, the Java ternary operator is used to replace simple if…else statement.
Example:
public class Sample { public static void main(String[] args) { int n=10; //Using ternary operator String number = (number%2==0)?"even number":"odd number"; System.out.println(number); } }
Output:
even number |
Java if-else-if ladder Statement
To choose amongst numerous alternatives, a Java if-else-if ladder is utilized. From the top-down, the if statements are executed. The statement linked with that it is performed as soon as one of the conditions controlling the if is true, and the rest of the ladder is bypassed.
if(First condition){ //… to be executed if the First condition is true }else if( Second condition ){ //… to be executed if the Second condition is true } else if( Third condition){ //… to be executed if the Third condition is true } … else{ //… to be executed if all the conditions are false } |
java if-else statements, java if-else statements, , java if-else statements, , java if-else statements, , java if else statements, , java if-else statements, , java if-else statements, , java if-else statements, , java if else statements, java if-else statements, java if-else statements,
Example:
/A Java programme to show how to use the If-else-if ladder. // It's a grading system that includes fail, D grade, C grade, B grade, A grade, and A+. public class Sample { public static void main(String[] args) { int Percent=50; if(Percent<40){ System.out.println("fail"); } else if(Percent>=50 && Percent<60){ System.out.println("D grade"); } else if(Percent>=60 && Percent<70){ System.out.println("C grade"); } else if(Percent>=70 && Percent<80){ System.out.println("B grade"); } else if(Percent>=80 && Percent<90){ System.out.println("A grade"); }else if(Percent>=90 && Percent<100){ System.out.println("A+ grade"); }else{ System.out.println("Wrong Input "); } } }
Output:
D grade |
Java Nested if statement
nested-if: An if statement that is the target of another if or else statement is known as a nested if. An if statement inside an if statement is referred to as nested if statements. Yes, we may stack if statements within if statements in Java. The final else line will be executed if none of the conditions are true.
if(condition){ //… to be executed if(condition){ //… to be executed } } |
Example:
// A Java programme that shows how to use nested if statements.public class Sample { public static void main(String[] args) { //Creating two variables for age and weight int x=30; int y=40; //applying condition on x and y if(x>=18){ if(y>50){ System.out.println("You are applicable"); } } }}
Output:
You are applicable |
You May like:
Java Input and Output | Java Tutorial
Operators in Java | Java Tutorial
Java Data Types | Primitive and Non-Primitive Data Types
We hope that this article will assist you in understanding all about Java if … Else. We have concentrated on making a basic, meaningful, and easy-to-learn guide to the concepts. Still, if you have any problems regarding this, please post them in the comment section, we will be glad to assist you.
Pingback: Java Break Statement with Example - Developers Dome