Introduction to Programming : Java & SpringBoot
  • Table of Contents
  • Introduction to Programming
  • Algorithms and logic
  • Data types and variables
  • Input and output
  • Conditional Statements
  • Loops
  • Functions
  • Arrays and strings
  • Pointers and references
  • Introduction to Object-Oriented Programming (OOP)
    • Demonstrate OOPS Concept
  • File handling
  • Introduction to Spring Boot
  • Core Concepts and Dependency Injection
  • Building Web Applications with Spring Boot
  • Spring MVC and Web Development
  • Database Integration with Spring Data
  • Building RESTful APIs with Spring Boot
  • Securing Spring Boot Applications with Spring Security
  • Advanced Topics in Spring Boot
  • Testing and Deployment
  • Real-world Projects
Powered by GitBook
On this page
  • If/else statements
  • Switch statements

Conditional Statements

If/else statements

In Java, you can use if/else statements to control the flow of your program based on certain conditions. Here's an example:

public class IfElseExample {
    public static void main(String[] args) {
        int age = 25;

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

In the above example, we use an if/else statement to check if the age variable is greater than or equal to 18. If it is, we display the message "You are an adult." If it's not, we display the message "You are a minor."

You can also use if statements without an else statement, like this:

public class IfExample {
    public static void main(String[] args) {
        int age = 25;

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

        System.out.println("Program complete.");
    }
}

In the above example, we use an if statement to check if the age variable is greater than or equal to 18. If it is, we display the message "You are an adult." Regardless of the result, we then display the message "Program complete."

Switch statements

In Java, you can use switch statements to simplify complex if/else statements. Here's an example:

public class SwitchExample {
    public static void main(String[] args) {
        int dayOfWeek = 2;

        switch (dayOfWeek) {
            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;
            default:
                System.out.println("Weekend");
                break;
        }
    }
}

In the above example, we use a switch statement to check the value of the dayOfWeek variable. Depending on its value, we display the corresponding day of the week. The default case is used when none of the other cases match. The break statement is used to exit the switch statement and continue with the rest of the program.

PreviousInput and outputNextLoops

Last updated 2 years ago