Data Types and Operators
(Non-Primitive Data Types)
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference

In Java, conditional statements are used to perform different actions based on different conditions. These include ‘if‘, ‘if-else‘, nested ‘if‘, and ‘switch‘ statements. Here is a detailed explanation of each:

1. If Statement

The if statement evaluates a boolean expression and executes a block of code if the expression is true.

Syntax:

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

				
			

Example:

				
					int age = 18;
if (age >= 18) {
    System.out.println("You are an adult.");
}

				
			

2. If-Else Statement

The ‘if-else‘ statement evaluates a boolean expression and executes one block of code if the expression is true and another block if it is false.

Syntax:

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

				
			

Example:

				
					int age = 16;
if (age >= 18) {
    System.out.println("You are an adult.");
} else {
    System.out.println("You are not an adult.");
}

				
			

3. Nested If Statement

A nested ‘if‘ statement is an ‘if‘ statement within another ‘if‘ or ‘else‘ block. It allows you to check multiple conditions sequentially.

Syntax:

				
					if (condition1) {
    // code to be executed if condition1 is true
    if (condition2) {
        // code to be executed if condition2 is true
    } else {
        // code to be executed if condition2 is false
    }
} else {
    // code to be executed if condition1 is false
}

				
			

Example:

				
					if (condition1) {
    // code to be executed if condition1 is true
    if (condition2) {
        // code to be executed if condition2 is true
    } else {
        // code to be executed if condition2 is false
    }
} else {
    // code to be executed if condition1 is false
}

				
			

4. Switch Statement

The ‘switch‘ statement allows you to execute one code block from many based on the value of a variable. It’s useful when you have multiple possible values for a single variable.

Syntax:

				
					switch (expression) {
    case value1:
        // code to be executed if expression equals value1
        break;
    case value2:
        // code to be executed if expression equals value2
        break;
    // other cases
    default:
        // code to be executed if expression doesn't match any case
        break;
}

				
			

Example:

				
					int day = 3;
switch (day) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    case 3:
        System.out.println("Wednesday");
        break;
    case 4:
        System.out.println("Thursday");
        break;
    case 5:
        System.out.println("Friday");
        break;
    case 6:
        System.out.println("Saturday");
        break;
    case 7:
        System.out.println("Sunday");
        break;
    default:
        System.out.println("Invalid day");
        break;
}

				
			

Key Points to Remember:

  • If-Else vs. Switch: if-else is suitable for conditions that result in boolean expressions, while switch is ideal for fixed values like integers, strings, and enums.
  • Break Statement in Switch: Each case block in a switch statement should end with a break statement to prevent fall-through.
  • Nested If: You can nest if statements to handle multiple conditions but be cautious of readability and complexity.

These conditional statements are fundamental in controlling the flow of a Java program based on different conditions and scenarios.

Scroll to Top