Of course it's nested. Nesting has nothing to do with the way you format the code.

if (...)
{
    // some code
}
else if (...)
{
    // some code
}
else
{
    // some code
}

is exactly equivalent to

if (...)
{
    // some code
}
else
{
    if (...)
    {
        // some code
    }
    else
    {
        // some code
    }
}

There's no 'else if' keyword in Java, the effect is achieved purely by a formatting convention which stops long chains of nested elses from drifting right across the screen.

In case anybody needs persuading, here is the specification for an if statement: Java Language Specification section 14.9

Answer from JeremyP on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_conditions_nested.asp
Java Nested If Statements
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... You can also place an if statement inside another if. This is called a nested ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ nested-if-in-java
Java Nested if - GeeksforGeeks
July 23, 2025 - Nested if in Java refers to having one if statement inside another if statement. If the outer condition is true the inner conditions are checked and executed accordingly.
Discussions

if statement - Nested if - else in java - Stack Overflow
I am preparing for the OCA 8 examination ... There was a question in the enthuware test, what is the proper structure of the following code(like which if belongs to which else - without curly brac... More on stackoverflow.com
๐ŸŒ stackoverflow.com
In Java, is this considered an example of a "nested IF statement"? - Stack Overflow
There's no 'else if' keyword in Java, the effect is achieved purely by a formatting convention which stops long chains of nested elses from drifting right across the screen. In case anybody needs persuading, here is the specification for an if statement: Java Language Specification section 14.9 More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - Avoiding nested if statements - Stack Overflow
Sign up to request clarification or add additional context in comments. ... A function should do only one thing, if you have nested if like this your function is certainly doing more than one thing. So that means, each if/else if/else block should then be in its own function. More on stackoverflow.com
๐ŸŒ stackoverflow.com
if statement - Nested if-else vs if-else? - Stack Overflow
I'm not the downvoter, but... wasn't the OP's code in Java? 2015-11-20T02:16:39.31Z+00:00 ... That makes zero difference, nested is nested regardless of language, the process is the same. Syntax is similar too. 2015-11-20T02:17:27.187Z+00:00 ... @DanWhite thanks for the help Ill press solved when the 6 minutes is up 2015-11-20T02:19:09.05Z+00:00 ... A nested if statement is essentially a series of if statements within each other like so... if () { if () { ... } else ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Codedamn
codedamn.com โ€บ news โ€บ java
Nested if-else statement in Java (with examples)
October 18, 2022 - public class codedamn { public static void main(String[] args) { int a=10; if(a%5==0) { //Divisible by 5 System.out.println("Divisibe by 5"); } else { //Not divisible by 5 System.out.println("Not dividible by 5"); } } } Code language: Java (java) ... In the above example, if the if-condition is satisfied, the statement inside it is executed otherwise it moves to the else part. The number a=10 is divisible by 5, so it prints โ€œDivisible by 5โ€ and the else statement is skipped. We saw how helpful if and else statements are, but what if we need to check for more conditions even when one condition is satisfied? In such cases, we use nested if-else statement.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ decision-making-javaif-else-switch-break-continue-jump
Decision Making in Java - Conditional Statements - GeeksforGeeks
... The if-else-if ladder allows multiple independent conditions to be checked in order. As soon as one condition is true, its block executes, and the rest are skipped. ... import java.util.*; class Geeks { public static void main(String args[]) ...
Published ย  October 6, 2025
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ nested_if_statements_in_java.htm
Nested If Statements in Java
Java Vs. C++ ... It is always legal to nest if-else statements which means you can use one if or else if statement inside another if or else if statement.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 38944125 โ€บ nested-if-else-in-java
if statement - Nested if - else in java - Stack Overflow
If you do not put curly braces, only the first statement after if() is considered. In your case, the two if statements are nested but the first else statement is for the parent if, the second else hence is without an if.
Find elsewhere
๐ŸŒ
Coders Campus
coderscampus.com โ€บ home โ€บ nested if statements
Nested IF statements - Coders Campus
April 27, 2021 - So, as predictable as the sun will rise and set, Java will follow the same set of rules for this new if..else block. We will now evaluate if age is less than 65. And as luck would have it, our variable is set to 29. So, yes, 29 is less than 65, so we'll execute the code inside of this block and the console will output โ€œYou are an adult.โ€ ยท You have to consider the fact that you are now inside of nested if statements and make note of where the beginning and end of all of those curly braces {} are.
๐ŸŒ
Medium
joshgunh.medium.com โ€บ yet-another-way-to-deal-with-nested-if-else-conditions-176057a419d
Yet another way to deal with nested if/else conditions | by Joshgun Huseynov | Medium
November 23, 2022 - Today Iโ€™ll show you how I used the Fluent Interface pattern to eliminate the verbosity of the nested if/else statements when I needed to validate some conditions before actually creating the object. You can learn more about Fluent Interface from these links: https://java-design-patterns....
Top answer
1 of 4
14

You can indeed reduce the level of nesting significantly. However I leave it up to you whether you like it or not.

Your code currently has three levels of nesting, that's relatively okay. Use comments to make your code more readable instead. However you may adapt some of the following tricks.


Is still unassigned

String returnMessage = null;

// Something is empty
if (cipherFileRead.isEmpty() || freqFileRead.isEmpty()) {
    returnMessage = "Both files must be loaded and contain text. Decryption unsuccessful.";
}

// No description type given
if (returnMessage == null && freqChooser.getSelection() == null) {
    returnMessage = "Please select decryption type. Decryption unsuccessful";
}

// Decrypt
if (returnMessage == null) {
    // Select technique
    String decryptedText = null;
    if (nearestFreq.isSelected()) {
        decryptedText = decrypter.nearestFreq(cipherFileRead, freqFileRead);
    } else if (rankingFreq.isSelected()) {
        decryptedText = decrypter.byRanking(cipherFileRead, freqFileRead);
    }

    // Write decrypted text
    file.writeToFile(decryptedText, "output.txt");

    returnMessage = "Successfully decrypted to output.txt";
}

// Show return message
JOptionPane.showMessageDialog(null, returnMessage);

So the trick was to check whether returnMessage is still unassigned.

The disadvantage of this style is that it slows the program a bit because it makes it harder for the processor to guess the conditions correct for pipelining. This is called branch prediction and all processors do that.

But if it enhances readability, for example by reducing a really deep nesting, you may use it.


Return from method

However if this is a method I would definitely give the advice to use such a style, but then by using return like:

public String decryptFile() {
    // Something is empty
    if (cipherFileRead.isEmpty() || freqFileRead.isEmpty()) {
        return "Both files must be loaded and contain text. Decryption unsuccessful.";
    }

    // No description type given
    if (freqChooser.getSelection() == null) {
        return "Please select decryption type. Decryption unsuccessful";
    }

    // Decrypt
    // Select technique to use
    String decryptedText = null;
    if (nearestFreq.isSelected()) {
        decryptedText = decrypter.nearestFreq(cipherFileRead, freqFileRead);
    } else if (rankingFreq.isSelected()) {
        decryptedText = decrypter.byRanking(cipherFileRead, freqFileRead);
    } else {
        // Out of techniques?
        return "No decryption technique can be applied.";
        // Or if this must not happen due to context, use:
        // throw new AssertionError();
    }

    // Write decrypted text
    file.writeToFile(decryptedText, "output.txt");

    return "Successfully decrypted to output.txt";
}

And the caller will then do it like:

// Show return message
JOptionPane.showMessageDialog(null, decryptFile());

In this scenario, next to reducing the nesting, you also reduce the amount of time you spend in the code itself.

You should definitely pack this inside a method. It should not be the decision of the decryption where and how to display the return message. Instead return the message and let the caller decide how to use it.

Note that in practice you probably want to add some arguments to the method and so on. Also you may want to use exceptions instead of plain String messages, like IllegalArgumentException.

2 of 4
6

I'll answer by using the principles of one of my favorite book called "Clean Code":

  1. A function should do only one thing, if you have nested if like this your function is certainly doing more than one thing.

  2. So that means, each if/else if/else block should then be in its own function. This solved the nested if problem.

  3. And the extracted function should be given a descriptive name of what it does. This is even better then commenting it because comment can go out of date, or even irrelevant, and thus become misleading.

So if I were try to refactor your function with the above principles, I may come up with something like this:

public void displayDecryptResult() {
    String decryptResult = decrypt();
    JOptionPane.showMessageDialog(null, decryptResult);
}

private String decrypt() {
    String decryptResult;

    if(!cipherFileRead.isEmpty() && !freqFileRead.isEmpty() ){
        decryptResult = decryptForValidInputFiles();
    } else{
        decryptResult = "Both files must be loaded and contain text. Decryption unsuccesful.";
    }

    return decryptResult;
}

private String decryptForValidInputFiles() {
    if (freqChooser.getSelection() != null){
        message = decryptForValidInputFilesAndDecryptionType();
    } else {
        message = "Please select decryption type. Decryption unsuccesful";}
    }

    return message;
}

private String decryptForValidInputFilesAndDecryptionType() {
    if(nearestFreq.isSelected()){
        file.writeToFile(decrypter.nearestFreq(cipherFileRead, freqFileRead), "output.txt");;
    }
    else if (rankingFreq.isSelected()){
        file.writeToFile(decrypter.byRanking(cipherFileRead, freqFileRead), "output.txt");;
    }

    return "Succesfully decrypted to output.txt";
}

Some may find the function name is too descriptive that it becomes laughable. But after a long time you've written the code, and you have to debug it, you would thank you had given those descriptive name, so you can easily skim through the pseudocode like names to get idea of what it's trying to do.

๐ŸŒ
Quora
quora.com โ€บ How-do-we-use-nesting-if-and-if-else-statements-in-Java
How do we use nesting if and ifโ€ฆelse statements in Java? - Quora
Answer (1 of 3): Syntax given below : If(condition) { Statements which will be executed if the condition is true } else { Statements which will be executed if the condition is false } * If there are too many nested โ€œifโ€ consider using โ€œswitch ...
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ if-else-statement
Java 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.
๐ŸŒ
Shiksha
shiksha.com โ€บ home โ€บ it & software โ€บ programming โ€บ colleges in india
Nested If Else in Java | About, Syntax, Flowchart and ...
September 3, 2024 - Among the many institutions that offer the Java, there is a split of colleges by ownership, private: 91, public / government: 29 & public private: 1.
๐ŸŒ
DEV Community
dev.to โ€บ kaviyak โ€บ if-else-and-nested-if-in-java-28m5
if else and nested if in java: - DEV Community
January 30, 2025 - **program:** public class Greater ... } } } output: Both are equal ยท Nested if in Java Refers to having one if statement inside another if statement....
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ multiple if statements vs. a cascading if else
r/learnprogramming on Reddit: Multiple if statements vs. a cascading if else
April 22, 2015 -

I had my first code-review the other day. My boss explains to me that having 4 if statements, each returning out of the method is a much clearer, more readable, smarter way to program.

Wait what? Is this really the case? I know this may be a stylistic preference, but is it not considered bad practice? I was taught that returning nothing in a void method is bad and good logic usually looks like a well-thought out cascading if else.

What are the pros and cons of these two different styles?

edit: examples for clarity. sorry for the formatting i did my best

public void example1(String name){
if(name.equals(""))
   return;
if(name==null)
   return;
if(name.contains("something"){
   out.println("Your name is not okay.");
   return;
}
this.name=name;
}

public void example2(String name){
if(name==null||name.equals("")){
     return;
}
else if(name.contains("something")){
     out.println("your name is not okay")
}
else
    this.name=name;
}

My boss would prefer example 1, and i would code something like example 2 minus the empty return

๐ŸŒ
DEV Community
dev.to โ€บ paulike โ€บ nested-if-and-multi-way-if-else-statements-3neh
Nested if and Multi-Way if-else Statements - DEV Community
May 2, 2024 - For example, the following is a nested if statement: if (i > k) { if (j > k) System.out.println("i and j are greater than k"); } else System.out.println("i is less than or equal to k");
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_conditions.asp
Java If ... Else
assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new package private protected public return requires short static super switch synchronized this throw throws transient try var void volatile while Java String Methods
๐ŸŒ
Studytonight
studytonight.com โ€บ java-programs โ€บ java-nested-if-program
Java Nested If Program - Studytonight
We will perform various Java Programs programs using the nested if statement in java. When there is an if statement within another if statement it is known as a nested if statement.