If Else Statement
The if-else statement is used to specify a condition following an if. It means that the program executes the if block if the condition is true; otherwise, the else block is executed.
Syntax:
Flow
Diagram
Source Code:
import java.util.Scanner;
public class Ifelse {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input product price and quantity
System.out.print("Enter product price: ");
double price = scanner.nextDouble();
System.out.print("Enter quantity: ");
int quantity = scanner.nextInt();
// Calculate total amount
double totalAmount = price * quantity;
double discount;
// Apply discount based on total amount
if (totalAmount >= 100) {
discount = totalAmount * 0.10; // 10% discount
} else {
discount = totalAmount * 0.05; // 5% discount
}
double finalAmount = totalAmount - discount;
// Display results
System.out.println("\nTotal Amount: $" + totalAmount);
System.out.println("Discount Applied: $" + discount);
System.out.println("Final Amount after Discount: $" + finalAmount);
scanner.close();
}
}Result:
Watch the video:
Ebook: https://softkhpc.blogspot.com/2025/05/java-programming-ebooks.html

