---
title: Find the largest among three numbers [if-else & nested if-else]
url: https://docs.vultr.com/java/examples/find-the-largest-among-three-numbers-if-else--nested-if-else
publish_date: 2024-11-22T17:40:00.073302Z
last_updated: 2024-11-22T17:40:00.073302Z
header_image: https://assets.vultrdevrel.com/docs-main-doc-assets-1/collection-items/1013/0a7f6cf4-34a8-43d3-8f0f-3a878c27b555.jpg
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

# Java Program to Find the Largest Among Three Numbers [if-else & nested if-else]

## Introduction
Choosing the largest number from a set is a common task in programming, serving as a foundational concept for understanding conditional statements and logical reasoning. In Java, this is typically achieved using `if-else` or `nested if-else` constructs, which provide the control flow necessary to compare values and determine the largest among them.

In this article, you will learn how to implement a Java program to find the largest among three numbers. Dive into practical examples illustrating the use of both `if-else` and `nested if-else` statements, and gain proficiency in managing multiple conditions effectively within your Java applications.

## Using if-else Statement

### Basic Implementation
1. Declare and initialize three integers.
1. Employ an `if-else` statement to determine the largest number.

    ```java
    public class Main {
        public static void main(String[] args) {
            int num1 = 10, num2 = 20, num3 = 7;
            int largest;

            if (num1 >= num2 && num1 >= num3) {
                largest = num1;
            } else if (num2 >= num1 && num2 >= num3) {
                largest = num2;
            } else {
                largest = num3;
            }

            System.out.println("The largest number is " + largest);
        }
    }
    ```

    This code initializes three variables `num1`, `num2`, and `num3`. The `if-else` statement compares each number to find the largest. The result prints out the largest number.

## Using Nested if-else Statement

### Advanced Control Flow
1. Initialize the same three integers.
1. Use nested `if-else` to fine-tune the decision-making process.

    ```java
    public class Main {
        public static void main(String[] args) {
            int num1 = 10, num2 = 20, num3 = 30;
            int largest;

            if (num1 > num2) {
                if (num1 > num3) {
                    largest = num1;
                } else {
                    largest = num3;
                }
            } else {
                if (num2 > num3) {
                    largest = num2;
                } else {
                    largest = num3;
                }
            }

            System.out.println("The largest number is " + largest);
        }
    }
    ```

    In this variant, nested `if-else` structures further break down the comparisons. Initially, `num1` is compared with `num2`, and depending on the outcome, a secondary comparison is made with `num3`. This method is useful for clearer logic paths in more complex decision trees.

## Conclusion
Java's control structures like `if-else` and `nested if-else` provide robust tools for comparing numerical values and determining the largest among them. Mastery of these conditionals is crucial for developing logical solutions to common programming problems. Utilize the examples provided to enhance your understanding of Java conditionals and apply these structures effectively in varied programming scenarios. By practicing these techniques, your proficiency in handling decision-making processes within Java applications will improve significantly.