All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

If Statement In Java Example

Last Updated : Mar 11, 2024

If Statement In Java Example

In this article we will show you the solution of if statement in java example, there are a number of different decision-making statements in Java, but the if statement is the most simple.

The condition determines whether statements or blocks of statements will be executed, so if a certain condition is true then the statements will be carried out, otherwise they will not.

In Java, "if" statements (also known as "if-then statements") are the most straightforward form of decision making.

The if-statement facilitates the establishment of certain conditions. The code to be executed is specified in accordance with these conditions.

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

Step By Step Guide On If Statement In Java Example :-

public class TalkersCode {
public static void main(String[] args) {
    int marks=95;
    else if(marks>=60 && marks<80){
        System.out.println("D grade");
    else if(marks>=60 && marks<90){
        System.out.println("C grade");
    else if(marks>=70 && marks<80){
        System.out.println("B grade");
    else if(marks>=70 && marks<90){
        System.out.println("A grade");
    }else if(marks>=90 && marks<100){
        System.out.println("A+ grade");
  1. There is a class named "TalkersCode" that we define.
  2. As part of the class declaration, we declare a main method.
  3. Our "marks" variable has the value 95 when declared and initialized.
  4. In this example, we check whether the "marks" value is less than 50 by using an "if" conditional statement. This will result in the console displaying the message "fail".
  5. If the marks value is not less than 50, we move on to the first "else if" statement.
  6. Using the first "else if" statement, we check whether the "marks" value is greater or equal to 60 AND less than 80. An error message with the letter "D" will be displayed if it is.
  7. Our next step is to move to the second "else if" statement if the marks value does not satisfy the first condition.
  8. In the second "else if" statement, we check whether "marks" is greater than or equal to 60 AND less than 90. In that case, a message stating "C grade" will appear.
  9. In the third "else if" statement, we check to see if the "marks" value is greater than or equal to 70 AND less than 80. In that case, the message "B grade" will appear on the screen.
  10. In the fourth "else if" statement, the "marks" value is checked to see if it is greater than 70 AND less than 90. The "A grade" message will appear if this is the case.
  11. In the fifth "else if" statement, whether the "marks" value is greater than or equal to 90 AND less than 100 is checked. If it is, the "A+ grade" message will be displayed.
  12. In the case that the "marks" value does not meet any of the "if" or "else if" conditions, the "Invalid!" message will appear.
  13. This concludes the execution of the program.

Conclusion :-

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

Using the have if statement, we can test whether the condition is true.

In this case, the condition is checked for boolean values: true or false. As with the if statement, Java also supports else and if else statements, which are executed if the if statement is false.

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

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪