if Conditional Statement in Java


☞If you have only one condition to test, then use if.

☞If the condition evaluates to true, it executes the statements of if block.

☞If the condition evaluates to false, it does nothing.

Syntax :

if(condition) {
    Statement 1
    Statement 2
    ...........
}

Example

//Finding number is positive, negative or zero
import java.util.*;

public class IfExample {
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        System.out.println("Enter a number");
        int num=sc.nextInt();
        
        if(num>0)
            System.out.println("Positive number");
        
        if(num<0)
            System.out.println("Negative number");
        
        if(num==0)
            System.out.println("Zer0");
    
    }
}

Output

Enter a number
-6
Negative number