All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Conditional Statement In Java

Last Updated : Mar 11, 2024

Conditional Statement In Java

In this article we will show you the solution of conditional statement in java, the flow of the code goes one way when a condition is met, making conditionals also known as branching statements. An alternative condition will be evaluated if the first condition is not met (if there is one).

The evaluation of the conditions continues until they have all been evaluated as true or false.

With conditional statements, you can compare two numbers (x and y) using the following statements: x > y, x = y, and x < y.

In the case of x = 1 and y = 2, there will be three evaluations before the final x < y conditional statement evaluates to true.

We will now discuss the idea of how to create conditional statement in java with an example.

Step By Step Guide On Conditional Statement In Java :-

Code 1

class TalkersCode {
  public static void main(String[] args) {
    int number = 0;
     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 0.");
    }
  }
}

Code 2

public class TalkersCode{
public static void main(String[] args) {
    int age=24;
    int weight=42;
    if(age>=18){
        if(weight>50){
            System.out.println("You are eligible to donate blood");
        } else{
            System.out.println("You are not eligible to donate blood");
        }
    } else{
      System.out.println("Age must be greater than 18");
    }
} }
  1. A TalkersCode class has been defined.
  2. There is a static void public method defined as the main method.
  3. As soon as an integer variable named number is created, it is initialized to 0.
  4. Checking whether a number exceeds 0 is performed using an if statement
  5. In the case that the number exceeds 0, the statement "The number is positive." The console will display the message.
  6. As a result, if the number is not greater than 0, the else if statement determines whether or not it is less than zero.
  7. When the number is less than 0, a message will appear on the console: "The number is negative.".
  8. When the number is neither greater or lesser than zero, the else statement is executed.
  9. Upon typing "The number is 0," the console will display "The number is 0".
  10. This program has come to an end.

Conclusion :-

As a result, we have successfully learned how to create a conditional statement in java with an example.

The execution flow was directed using conditional statements in this tutorial.

As a result of the lesson, you've learned when to use if and switch statements as well as written a few examples of using them.

In addition, the best practices for clean code, as well as what to avoid when dealing with conditionals, were taught to you.

I hope this article on conditional statement in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪