Java Conditional Statement

if statement

The if statement in java is the most basic form of conditional statement. It allows your program to execute a block of code only when the Test Expression is true.

Syntax:

if(condition){
//code to be executed if condition is true
}

#java program to demonstrate if statement

Program-1

import java.io.*;
    public class ApnaStudy{
    public static void main(String[] args) {
    int total_purchase=1200;
    if(total_purchase>=1000){
    System.out.println("you are gifted a pendrive");
    }
    }
}


/*OUTPUT*/

you are gifted a pendrive


Note: If the condition (total_purchase>=1000) is true the statement will be executed.If the condition is false the statement will not be executed.

Program-2

import java.io.*;
    public class ApnaStudy{
    public static void main(String[] args) {
    int number=10;
    //check if number is positive
    if(number>0){
    System.out.println(number+" is a positive number");
    }
   //multiple statement may be grouped by putting them inside curly braces {}
    if(number<0){
    System.out.println(number+" is a negative number");
    }
    }
}


/*OUTPUT*/

10 is a positive number

Previous Next