In this article, you will learn-
- 1 Java if, if…else Statement
- 2 Java if (if-then) Statement
- 3 How if statement works?
- 4 Example 1: Java if Statement
- 5 Example 2: Java if…else with String
- 6 Java if…else (if-then-else) Statement
- 7 How if…else statement works?
- 8 Example 2: Java if-else Statement
- 9 Java if..else..if Statement
- 10 Example 3: Java if..else..if Statement
- 11 Java Nested if..else Statement
- 12 Example 4: Nested if…else Statement
Java if, if…else Statement
java if-else statement: In this tutorial, you will find out about control flow statements in Java using Java if and if…else statements with the help of examples.
In computer programming, it’s regularly alluring to execute a specific part of code depending on whether the predetermined condition is true or false (which is known distinctly during the run time). For such cases, control flow statements are used.
Java if (if-then) Statement
In Java, the syntax of the if-then statement is:
if (expression) {
// statements
}
Here expression is a boolean articulation. A boolean articulation returns either true or false.
If the expression is assessed to true, statement(s) inside the body of if (statements inside the enclosure) are executed
If the expression is assessed to false, statement(s) inside the body of if are skipped from execution
How if statement works?
Example 1: Java if Statement
class IfStatement {
public static void main(String[] args) {
int number = 10;
// checks if number is greater than 0
if (number > 0) {
System.out.println("The number is positive.");
}
System.out.println("This statement is always executed.");
}
}
Output
The number is positive.
This statement is always executed.
In the above example, we have a variable named number. Here, the test expression checks if the number is greater than 0 (number > 0).
Since the number is greater than 0. So the test expression evaluates to true. Hence code inside the body of if is executed.
Now, change the value of the number to a negative integer. Let’s say -5.
int number = -5;
If we run the above program with the new value of the number, the output will be:
This statement is always executed.
Here, the value of number is less than 0. So, the test expression number > 0 evaluates to false. Hence, the body of if is executed.
To learn more about test expression, visit Java RelationalOperators and Java Logical Operators.
Note: We can also use strings as the boolean expression.
Example 2: Java if…else with String
class Main {
public static void main(String[] args) {
// create a string variable
String language = "Java";
// if statement
if(language == "Java") {
System.out.println("This is best programming language.");
}
}
}
Output
This is best programming language.
Here, we are looking at two strings in the if block.
Java if…else (if-then-else) Statement
The if statement executes a specific segment of code if the test expression is evaluated to true. In any case, if the test expression is evaluated to false, it does nothing.
For this situation, we can use a discretionary else block. Statements inside the collection of else block are executed if the test articulation is assessed to bogus. This is known as the if-then-else statement in Java.
The syntax of the if-then-else statement is:
if (expression) {
// codes
}
else {
// some other code
}
Here, our program will do one assignment (task inside if block) if the test expression is true and another errand (task inside else block) if the test expression is false.
How if…else statement works?
Example 2: Java if-else Statement
class IfElse {
public static void main(String[] args) {
int number = 10;
// checks if number is greater than 0
if (number > 0) {
System.out.println("The number is positive.");
}
else {
System.out.println("The number is not positive.");
}
System.out.println("This statement is always executed.");
}
}
Output
The number is positive.
This statement is always executed.
In the above example, we have a variable named number. Here, the test expression checks if the number is greater than 0 (number > 0).
Since the value of the number is 10, the test expression evaluates to true. Hence code inside the body of if is executed.
Now, change the value of the number to a negative integer. Let’s say -5.
int number = -5;
If we run the program with the new value of the number, the yield will be:
The number is not positive.
This statement is always executed.
Here, the value of number is -5. So the test expression evaluates to false. Hence code inside the body of else is executed.
Java if..else..if Statement
In Java, we have an if…else…if ladder, that can be used to execute one block of code among numerous different blocks.
if (expression1) {
// codes
}
else if(expression2) {
// codes
}
else if (expression3) {
// codes
}
.
.
else {
// codes
}
Here, if statements are executed from the top towards the base. When the test expression is true, codes inside the body of that the if statement is executed. At that point, the control of the program bounces outside the if-else-if ladder.
If all test expressions are false, codes inside the body of else is executed.
Example 3: Java if..else..if Statement
class Ladder {
public static void main(String[] args) {
int number = 0;
// checks if number is greater than 0
if (number > 0) {
System.out.println("The number is positive.");
}
// checks if number is less than 0
else if (number < 0) {
System.out.println("The number is negative.");
}
else {
System.out.println("The number is 0.");
}
}
}
Output
The number is 0.
In the above example, we are checking whether the number is positive, negative or zero. Here, we have two test expressions:
number > 0 – checks if the number is greater than 0
number < 0 – checks if the number is less than 0
Here, the value of number is 0. So both the test expression evaluates to false. Hence the statement inside the body of else is executed.
Java Nested if..else Statement
In Java, it is also possible to if..else statements inside a if..else statement. It’s called nested if…else statement.
Here’s a program to find largest of 3 numbers:
Example 4: Nested if…else Statement
class Number {
public static void main(String[] args) {
// declaring double type variables
Double n1 = -1.0, n2 = 4.5, n3 = -5.3, largestNumber;
// checks if n1 is greater than or equal to n2
if (n1 >= n2) {
// if...else statement inside the if block
// checks if n1 is greater than or equal to n3
if (n1 >= n3) {
largestNumber = n1;
}
else {
largestNumber = n3;
}
}
else {
// if..else statement inside else block
// checks if n2 is greater than or equal to n3
if (n2 >= n3) {
largestNumber = n2;
}
else {
largestNumber = n3;
}
}
System.out.println("The largest number is " + largestNumber);
}
}
Output
The largest number is 4.5
Note: In the above programs, we have assigned the value of factors ourselves to make this simpler. In any case, in real-world applications, these qualities may come from user input data, log files, from submission, and so forth.
Java provides a special operator called ternary operator, which is a kind of shorthand notation of if…else statement.
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.