GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CODESCRACKER
  • Python
  • Java
  • C++
  • JavaScript
  • SQL
  • Quiz
  • C
  • C#
  • HTML
  • CSS
  • PHP
  • Networking
  • Operating System

This view

  • Original page
  • New address

GeezerVison is showing codescracker.com — Java if-else statement with examples

  • Java Programming Basics
  • Java Tutorial
  • Java Environment Setup
  • Java Separators
  • Java Data Types
  • Java Variables
  • Java Variable Scope
  • Java Type Casting
  • Java Operators
  • Java Increment Decrement
  • Java Left Shift
  • Java Right Shift
  • Java Bitwise Operators
  • Java Ternary Operator
  • Java Control Statements
  • Java if-else statement
  • Java for Loop
  • Java while Loop
  • Java do-while Loop
  • Java switch Statement
  • Java break Statement
  • Java continue Statement
  • Java Popular Topics
  • Java Arrays
  • Java Multidimensional Array
  • Java Strings
  • Java Methods
  • Java Date and Time
  • Java Exception Handling
  • Java File Handling
  • Java OOP
  • Java Classes and Objects
  • Java Constructors
  • Java Constructor Overloading
  • Java Object as Parameter
  • Java Returning Objects
  • Java Encapsulation
  • Java Abstraction
  • Java Inheritance
  • Java Polymorphism
  • Java Packages
  • Java Import Statement
  • Java Multithreading
  • Java Suspend Resume Stop Thread
  • Java Programming Examples
  • Java Programming Examples

Java if-else statement with examples

Conditional statements, which let you control the flow of your program based on specific conditions, are one of the most basic programming concepts.

In Java, there are three types of conditional statements, which are

  • if
  • else if
  • else

These statements serve as the foundation for the majority of decision-making logic in Java, allowing you to execute different blocks of code based on various conditions. They are required for developing robust and efficient programs capable of handling a wide range of inputs and scenarios.

One more thing is that the "else if" and "else" statements are always used with the "if" statement.

We'll look at these conditional statements in Java and how you can use them in your own programs in this article. Writing effective Java code requires a solid understanding of conditional statements, regardless of your level of programming experience. So let's get started!

The "if" statement in Java

The "if" statement determines whether a given condition is true and, if so, executes a block of code. The syntax of an "if" statement is as follows:

if (condition) {
    // block of code to execute if "condition" is true
}

For example:

Java Code
public class Example {
  public static void main(String[] args) {
    int x = 10;

    if (x > 5) {
      System.out.println("The value of 'x' is greater than 5.");
    }
  }
}
Output
The value of 'x' is greater than 5.

Since the condition "x > 5" (10 > 5) evaluates to true, the program flow enters the body of the "if" statement and executes the "System.out.println()" statement, which prints the text: "The value of 'x' is greater than 5."

The "else if" statement in Java

You can use the "else if" statement to test for multiple conditions. If the first condition is not met, it checks the next condition and, if met, executes the block of code associated with it. The syntax of an "else if" statement is as follows:

if (condition1) {
    // block of code to execute if "condition1" is true
    
} else if (condition2) {
    // block of code to execute if "condition2" is true
    
} else if (condition3) {
    // block of code to execute if "condition3" is true
    
} else if (condition4) {
    // block of code to execute if "condition4" is true
    
}
:
else {
    // block of code to execute if none of the above conditions are true
}

For example:

Java Code
public class Example {
   public static void main(String[] args) {
      int x = 10;

      if (x < 0) {
         System.out.println("The value of 'x' is negative");
      } else if (x == 0) {
         System.out.println("The value of 'x' is zero");
      } else {
         System.out.println("The value of 'x' is positive");
      }
   }
}
Output
The value of 'x' is positive

Since the conditions "x < 0" and "x == 0" both evaluate to false, the block of code associated with "else" was executed.

The "else" statement in Java

The "else" statement is used to execute a block of code if none of the conditions in the "if" or "else if" statements are true. An "else" statement looks like this:

if (condition) {
    // block of code to execute if "condition" is true
} else {
    // block of code to execute if "condition" is false
}

For example:

Java Code
public class Example {
  public static void main(String[] args) {
    int x = 10;

    if (x < 5) {
      System.out.println("The value of 'x' is less than 5.");
    } else {
      System.out.println("The value of 'x' is greater than 5.");
    }
  }
}
Output
The value of 'x' is greater than 5.

Java if-else statement example

Java Code
public class Example {
  public static void main(String[] args) {
    int x = 10;

    if (x < 0) {
      System.out.println("The value of 'x' is negative.");
    } else if (x == 0) {
      System.out.println("The value of 'x' is zero.");
    } else if (x > 0 && x < 10) {
      System.out.println("The value of 'x' is a single-digit positive number.");
    } else if (x >= 10 && x < 100) {
      System.out.println("The value of 'x' is a two-digit positive number.");
    } else {
      System.out.println("The value of 'x' is a positive number with three or more digits.");
    }
  }
}
Output
The value of 'x' is a two-digit positive number.

Java Online Test


« Previous Tutorial Next Tutorial »
Practice Quizzes

Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes.

All Quizzes SQL Quiz Python Quiz Java Quiz C++ Quiz
Popular Topics

Explore the most popular programming and computer science tutorials on CodesCracker.

Python Tutorial Java Tutorial C++ Tutorial SQL Tutorial JavaScript Tutorial HTML Tutorial CSS Tutorial Computer Networking
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.