- Java If . Else
- The if Statement
- Syntax
- Example
- Example
- Example explained
- The else Statement
- Syntax
- Example
- Example explained
- The else if Statement
- Syntax
- Example
- Example explained
- The if-then and if-then-else Statements
- The if-then-else Statement
- If, If..else Statement in Java with Examples
- If statement
- Example of if statement
- Nested if statement in Java
- Example of Nested if statement
- If else statement in Java
- Example of if-else statement
- if-else-if Statement
- Example of if-else-if
- Top Related Articles:
- About the Author
- Java if. else Statement
- 1. Java if (if-then) Statement
- Working of if Statement
- Example 1: Java if Statement
- Example 2: Java if with String
- 2. Java if. else (if-then-else) Statement
- How the if. else statement works?
- Example 3: Java if. else Statement
- 3. Java if. else. if Statement
- How the if. else. if ladder works?
- Example 4: Java if. else. if Statement
- 4. Java Nested if..else Statement
- Example 5: Nested if. else Statement
- Table of Contents
Java If . Else
You already know that Java supports the usual logical conditions from mathematics:
- Less than: a < b
- Less than or equal to: a
- Greater than: a > b
- Greater than or equal to: a >= b
- Equal to a == b
- Not Equal to: a != b
You can use these conditions to perform different actions for different decisions.
Java has the following conditional statements:
- Use if to specify a block of code to be executed, if a specified condition is true
- Use else to specify a block of code to be executed, if the same condition is false
- Use else if to specify a new condition to test, if the first condition is false
- Use switch to specify many alternative blocks of code to be executed
The if Statement
Use the if statement to specify a block of Java code to be executed if a condition is true .
Syntax
if (condition) < // block of code to be executed if the condition is true >
Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an error.
In the example below, we test two values to find out if 20 is greater than 18. If the condition is true , print some text:
Example
We can also test variables:
Example
int x = 20; int y = 18; if (x > y)
Example explained
In the example above we use two variables, x and y, to test whether x is greater than y (using the > operator). As x is 20, and y is 18, and we know that 20 is greater than 18, we print to the screen that «x is greater than y».
The else Statement
Use the else statement to specify a block of code to be executed if the condition is false .
Syntax
if (condition) < // block of code to be executed if the condition is true > else < // block of code to be executed if the condition is false >
Example
int time = 20; if (time < 18) < System.out.println("Good day."); >else < System.out.println("Good evening."); >// Outputs "Good evening."
Example explained
In the example above, time (20) is greater than 18, so the condition is false . Because of this, we move on to the else condition and print to the screen «Good evening». If the time was less than 18, the program would print «Good day».
The else if Statement
Use the else if statement to specify a new condition if the first condition is false .
Syntax
if (condition1) < // block of code to be executed if condition1 is true > else if (condition2) < // block of code to be executed if the condition1 is false and condition2 is true > else < // block of code to be executed if the condition1 is false and condition2 is false >
Example
int time = 22; if (time < 10) < System.out.println("Good morning."); >else if (time < 18) < System.out.println("Good day."); >else < System.out.println("Good evening."); >// Outputs "Good evening."
Example explained
In the example above, time (22) is greater than 10, so the first condition is false . The next condition, in the else if statement, is also false , so we move on to the else condition since condition1 and condition2 is both false — and print to the screen «Good evening».
However, if the time was 14, our program would print «Good day.»
The if-then and if-then-else Statements
The if-then statement is the most basic of all the control flow statements. It tells your program to execute a certain section of code only if a particular test evaluates to true . For example, the Bicycle class could allow the brakes to decrease the bicycle’s speed only if the bicycle is already in motion. One possible implementation of the applyBrakes method could be as follows:
If this test evaluates to false (meaning that the bicycle is not in motion), control jumps to the end of the if-then statement.
In addition, the opening and closing braces are optional, provided that the «then» clause contains only one statement:
Deciding when to omit the braces is a matter of personal taste. Omitting them can make the code more brittle. If a second statement is later added to the «then» clause, a common mistake would be forgetting to add the newly required braces. The compiler cannot catch this sort of error; you’ll just get the wrong results.
The if-then-else Statement
The if-then-else statement provides a secondary path of execution when an «if» clause evaluates to false . You could use an if-then-else statement in the applyBrakes method to take some action if the brakes are applied when the bicycle is not in motion. In this case, the action is to simply print an error message stating that the bicycle has already stopped.
The following program, IfElseDemo , assigns a grade based on the value of a test score: an A for a score of 90% or above, a B for a score of 80% or above, and so on.
class IfElseDemo < public static void main(String[] args) < int testscore = 76; char grade; if (testscore >= 90) < grade = 'A'; >else if (testscore >= 80) < grade = 'B'; >else if (testscore >= 70) < grade = 'C'; >else if (testscore >= 60) < grade = 'D'; >else < grade = 'F'; >System.out.println("Grade = " + grade); > >
The output from the program is:
You may have noticed that the value of testscore can satisfy more than one expression in the compound statement: 76 >= 70 and 76 >= 60 . However, once a condition is satisfied, the appropriate statements are executed (grade = ‘C’;) and the remaining conditions are not evaluated.
If, If..else Statement in Java with Examples
When we need to execute a set of statements based on a condition then we need to use control flow statements. For example, if a number is greater than zero then we want to print “Positive Number” but if it is less than zero then we want to print “Negative Number”. In this case we have two print statements in the program, but only one print statement executes at a time based on the input value. We will see how to write such type of conditions in the java program using control statements.
In this tutorial, we will see four types of control statements that you can use in java programs based on the requirement: In this tutorial we will cover following conditional statements:
a) if statement
b) nested if statement
c) if-else statement
d) if-else-if statement
If statement
If statement consists a condition, followed by statement or a set of statements as shown below:
The statements gets executed only when the given condition is true. If the condition is false then the statements inside if statement body are completely ignored.
Example of if statement
public class IfStatementExample < public static void main(String args[])< int num=70; if( num < 100 )< /* This println statement will only execute, * if the above condition is true */ System.out.println("number is less than 100"); >> >
Nested if statement in Java
When there is an if statement inside another if statement then it is called the nested if statement.
The structure of nested if looks like this:
Statement1 would execute if the condition_1 is true. Statement2 would only execute if both the conditions( condition_1 and condition_2) are true.
Example of Nested if statement
public class NestedIfExample < public static void main(String args[])< int num=70; if( num < 100 )< System.out.println("number is less than 100"); if(num >50) < System.out.println("number is greater than 50"); >> > >
number is less than 100 number is greater than 50
If else statement in Java
This is how an if-else statement looks:
The statements inside “if” would execute if the condition is true, and the statements inside “else” would execute if the condition is false.
Example of if-else statement
public class IfElseExample < public static void main(String args[])< int num=120; if( num < 50 )< System.out.println("num is less than 50"); >else < System.out.println("num is greater than or equal 50"); >> >
num is greater than or equal 50
if-else-if Statement
if-else-if statement is used when we need to check multiple conditions. In this statement we have only one “if” and one “else”, however we can have multiple “else if”. It is also known as if else if ladder. This is how it looks:
if(condition_1) < /*if condition_1 is true execute this*/ statement(s); >else if(condition_2) < /* execute this if condition_1 is not met and * condition_2 is met */ statement(s); >else if(condition_3) < /* execute this if condition_1 & condition_2 are * not met and condition_3 is met */ statement(s); >. . . else < /* if none of the condition is true * then these statements gets executed */ statement(s); >
Note: The most important point to note here is that in if-else-if statement, as soon as the condition is met, the corresponding set of statements get executed, rest gets ignored. If none of the condition is met then the statements inside “else” gets executed.
Example of if-else-if
public class IfElseIfExample < public static void main(String args[])< int num=1234; if(num =1) < System.out.println("Its a two digit number"); >else if(num =100) < System.out.println("Its a three digit number"); >else if(num =1000) < System.out.println("Its a four digit number"); >else if(num =10000) < System.out.println("Its a five digit number"); >else < System.out.println("number is not between 1 & 99999"); >> >
Top Related Articles:
About the Author
I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.
Java if. else Statement
In programming, we use the if..else statement to run a block of code among more than one alternatives.
For example, assigning grades (A, B, C) based on the percentage obtained by a student.
- if the percentage is above 90, assign grade A
- if the percentage is above 75, assign grade B
- if the percentage is above 65, assign grade C
1. Java if (if-then) Statement
The syntax of an if-then statement is:
Here, condition is a boolean expression such as age >= 18 .
- if condition evaluates to true , statements are executed
- if condition evaluates to false , statements are skipped
Working of if Statement
Example 1: Java if Statement
class IfStatement < public static void main(String[] args) < int number = 10; // checks if number is less than 0 if (number < 0) < System.out.println("The number is negative."); >System.out.println("Statement outside if block"); > >
Statement outside if block
Note: If you want to learn more about about test conditions, visit Java Relational Operators and Java Logical Operators.
We can also use Java Strings as the test condition.
Example 2: Java if with String
Best Programming Language
In the above example, we are comparing two strings in the if block.
2. Java if. else (if-then-else) Statement
The if statement executes a certain section of code if the test expression is evaluated to true . However, if the test expression is evaluated to false , it does nothing.
In this case, we can use an optional else block. Statements inside the body of else block are executed if the test expression is evaluated to false . This is known as the if-. else statement in Java.
The syntax of the if. else statement is:
Here, the program will do one task (codes inside if block) if the condition is true and another task (codes inside else block) if the condition is false .
How the if. else statement works?
Example 3: Java if. else Statement
class Main < 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."); >// execute this block // if number is not greater than 0 else < System.out.println("The number is not positive."); >System.out.println("Statement outside if. else block"); > >
The number is positive. Statement outside if. else block
In the above example, we have a variable named number . Here, the test expression number > 0 checks if number is greater than 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 .
If we run the program with the new value of number , the output will be:
The number is not positive. Statement outside if. else block
Here, the value of number is -5 . So the test expression evaluates to false . Hence code inside the body of else is executed.
3. 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 multiple other blocks.
if (condition1) < // codes >else if(condition2) < // codes >else if (condition3) < // codes >. . else < // codes >
Here, if statements are executed from the top towards the bottom. When the test condition is true , codes inside the body of that if block is executed. And, program control jumps outside the if. else. if ladder.
If all test expressions are false , codes inside the body of else are executed.
How the if. else. if ladder works?
Example 4: Java if. else. if Statement
class Main < 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."); >// if both condition is false else < System.out.println("The number is 0."); >> >
In the above example, we are checking whether number is positive, negative, or zero. Here, we have two condition expressions:
Here, the value of number is 0 . So both the conditions evaluate to false . Hence the statement inside the body of else is executed.
Note: Java provides a special operator called ternary operator, which is a kind of shorthand notation of if. else. if statement. To learn about the ternary operator, visit Java Ternary Operator.
4. Java Nested if..else Statement
In Java, it is also possible to use if..else statements inside an if. else statement. It’s called the nested if. else statement.
Here’s a program to find the largest of 3 numbers using the nested if. else statement.
Example 5: Nested if. else Statement
class Main < public static void main(String[] args) < // declaring double type variables Double n1 = -1.0, n2 = 4.5, n3 = -5.3, largest; // 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) < largest = n1; >else < largest = n3; >> else < // if..else statement inside else block // checks if n2 is greater than or equal to n3 if (n2 >= n3) < largest = n2; >else < largest = n3; >> System.out.println("Largest Number: " + largest); > >
In the above programs, we have assigned the value of variables ourselves to make this easier.
However, in real-world applications, these values may come from user input data, log files, form submission, etc.