Java – If Block

If Block: It is used to execute a block of instructions if the condition is valid.

Program to give 15% discount on bill if the bill amount is greater than 5000

import java.util.Scanner;
class Code 
{
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.print("Enter bill amount : ");
		double bill = scan.nextDouble();
		if(bill > 5000){
			double discount = 0.15 * bill;
			bill = bill - discount;
		}
		System.out.println("Final bill to pay : " + bill);
	}
}
 

Program to give 20% bonus on salary if the employee has more than 5 years of experience:

import java.util.Scanner;
class Code 
{
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.print("Enter salary : ");
		double salary = scan.nextDouble();
		System.out.println("Enter experience : ");
		int exp = scan.nextInt();
		if(exp>5){
			double bonus = 0.2*salary;
			salary = salary + bonus ;
		}
		System.out.println("Salary to pay : " + salary);
	}
}
Scroll to Top