Logical Operator
Swipe to show menu
You learn how Java combines multiple conditions into a single decision using logical operators. Instead of only asking one question at a time, we can now build logic that depends on several conditions working together.
In a real coffee shop system, decisions are rarely simple — whether the system gives a discount, accepts an order, or shows a special message usually depends on more than one factor.
Operators
&&— AND: returnstrueonly if both conditions aretrue;||— OR: returnstrueif at least one condition istrue;!— NOT: reverses a boolean value —truebecomesfalseand vice versa.
Example
Main.java
1234567891011121314151617181920package com.example; public class Main { public static void main(String[] args) { int orderValue = 8; boolean isDeliveryAvailable = true; boolean isLoyalCustomer = false; boolean isWeekendPromo = true; boolean freeDelivery = isDeliveryAvailable && (orderValue > 10); boolean discount = isLoyalCustomer || isWeekendPromo; boolean showRegularPrice = !isWeekendPromo; System.out.println(freeDelivery); // false System.out.println(discount); // true System.out.println(showRegularPrice); // false } }
The example sets up three conditions about the order state. freeDelivery uses && — both delivery must be available AND the order must exceed 10, but since orderValue is 8, the result is false. discount uses || — even though the customer is not loyal, the weekend promo is active, so the result is true. showRegularPrice uses ! to reverse isWeekendPromo, which gives false.
Logical operators are important because they allow combining multiple rules into a single decision instead of writing separate checks.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat