Java Tutorials
  • Introduction to Java
    • What is Java?
    • History and Features of Java
    • Java Virtual Machine (JVM) and Bytecode
    • Why Java?
  • Setting up Java Development Environment
    • Installing Java Development Kit (JDK)
    • JDK vs JRE
    • Setting up IDE (Eclipse, IntelliJ, NetBeans) or Text Editor (VS Code, Sublime Text)
  • Basic Java
    • First Java Program : Hello World
    • Variable
    • Data Type
    • Constant
    • Date and Format
    • Operator
    • Condition
    • Looping
    • Function
    • Variadic Function
    • Enums
    • Array
    • Collection
    • Exception and Exception Handling
    • Naming Convention
  • Object Oriented Programming (OOP)
    • Classes and Objects
    • Inheritance and Polymorphism
    • Encapsulation and Abstraction
  • File Handling
    • Reading and Writing Binary File
    • Reading and Writing Text File
    • Serialization and Deserialization
  • Multithreading
    • Creating and Running Threads
    • Synchronization
    • Thread Pools and Executors
  • Collections API
    • Sorting and Comparable
    • Searching and Comparator
  • Java Database Connectivity (JDBC)
    • Introduction and Life Cycle
    • Connection to Database (MySQL)
    • Downloading JDBC Drivers for Various Databases
    • Maven and Gradle JDBC Drivers for Various Databases
    • JDBC URL Formats
    • Statement and PreparedStatement
    • CallableStatement
    • Selecting Data using JDBC
    • Inserting Data using JDBC
    • Updating Data using JDBC
    • Deleting Data using JDBC
    • Invoking Function and Stored Procedure using JDBC
  • Lambda
    • Introduction to Lambda Expressions
    • Functional Interface
    • Filtering, Mapping, Reducing
    • Lambda Expressions in Collections
    • Method References
    • Functional Programming Concepts
    • Stream API
    • Error Handling in Lambda Expressions
    • Optional in Functional Programming
    • Parallel Processing with Lambda
    • Functional Programming Patterns
    • Advanced Topics in Lambda Expressions
    • Best Practices and Design Patterns
    • Real-World Use Cases and Examples
Powered by GitBook
On this page
  1. Basic Java

Condition

Conditions in Java are used to make decisions in a program. They allow you to execute different code blocks based on whether a specified condition evaluates to true or false. Conditions are typically implemented using if statements, else statements, and else if statements.

if Statement:

The if statement is used to execute a block of code only if a specified condition is true. Here's the syntax:

if (condition) {
    // Code to be executed if the condition is true
}

if-else Statement:

The if-else statement allows you to specify two blocks of code: one to be executed if the condition is true, and another if the condition is false. Here's the syntax:

if (condition) {
    // Code to be executed if the condition is true
} else {
    // Code to be executed if the condition is false
}

else if Statement:

The else if statement allows you to specify multiple conditions. It is used in conjunction with if and else statements to test multiple conditions. Here's the syntax:

if (condition1) {
    // Code to be executed if condition1 is true
} else if (condition2) {
    // Code to be executed if condition2 is true
} else {
    // Code to be executed if neither condition1 nor condition2 is true
}

Example Code:

Here's an example demonstrating the use of if, else if, and else statements in Java:

public class ConditionExample {

    public static void main(String[] args) {
        int number = 10;

        if (number > 0) {
            System.out.println("The number is positive.");
        } else if (number < 0) {
            System.out.println("The number is negative.");
        } else {
            System.out.println("The number is zero.");
        }
    }
}

Explanation:

  1. int number = 10;: Declares and initializes a variable number with the value 10.

  2. if (number > 0) { ... }: Checks if number is greater than 0. If true, the code inside the block is executed, printing "The number is positive."

  3. else if (number < 0) { ... }: If the first condition is false, this statement checks if number is less than 0. If true, it prints "The number is negative."

  4. else { ... }: If both previous conditions are false, this block is executed, printing "The number is zero."

When you run this Java program, it will output:

The number is positive.

This output occurs because the value of number (10) is greater than 0, satisfying the first condition.

PreviousOperatorNextLooping

Last updated 1 year ago