Condition statements in Java

If statement is one of the most basic of control flow statements in any programming language; of course, Java is not an exception.

If statement

Our program will run following steps when executing an If statement

  1. checking condition
  2. If the condition is true, the action statements will execute, then the program will keep going with the rest of code after If block
  3. If the condition is false, the program will skip action inside If block and continue going with the code after If block.
Data flow of If statement

Write If statements in Java

if(conditions){
    //action statements
}

Example

public class ifConditionSample{
    public static void main(String[] args){
        int number = 10;
        if(number > 1){
            System.out.println(number);
        }
    }
}

Output would be

10

If … else statement

If the conditions are true, then the actions in If block will be executed. If the conditions are false, then the actions in Else block will be executed.

Syntax of If…else block in java

if(conditions){
//action statements
}else{
//action statements
}

Example

public class ifElseConditionSample{
    public static void main(String[] args){
        int number = 15;
        if(number%2 == 0){
            System.out.println(number + " is not divisible by 2");
        }else{
           System.out.println(number);
        }
    }
}

The out put will be

15

If …. else if … else statement

In this flow statement, if the first condition is false, then going to check the second condition, and keep going still condition N. If all of the states are false, then the program will execute the action statement in else block.

The syntax of flow statements

if(conditions){
//action statements
}else if(conditions){
//action statements
}else{
//action statements
}

Example of If…else if …else block

public class ifConditionSample{
    public static void main(String[] args){
        int score = 50;
        if(score < 50){
            System.out.println("you lose!");
        }else if(score >= 50 && score <=80 ){
            System.out.println("you are passed!");
       }else{
            System.out.println("you are the best");
       }
    }
}

The output will be

you are passed!

Comparison operators

>

<

>=

<=

==

!=

&&

||

!

greater

less than

greater or equal

less than or equal

equal

not equal

and

Or

Not

Conclusion

Those are brief information about If conditions in Java and comparison operators that you can use for comparing the value. Thanks for reading.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: