Java
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented Java
-
Section 4: Collections Framework
-
Section 5: Exception Handling
-
Section 6: Generics
-
Section 7: Functional Java
-
Section 8: Concurrency
-
Section 9: I/O and NIO
-
Section 10: JVM Internals
-
Section 11: Modern Java Features
-
Section 12: Build Tools and Project Structure
-
Section 13: Testing
-
Section 14: Databases and Persistence
-
Section 15: Networking
-
Section 16: Design and Best Practices
-
Section 17: Reflection and Annotations
-
Section 18: Logging and Diagnostics
-
Section 19: Date, Time, and Internationalization
-
Section 20: Java Platform Module System
-
Section 21: Security in Java
-
Section 22: Advanced Collections and Data Structures
-
Section 23: More Concurrency Patterns
-
Section 24: Compression, Files, and System Integration
-
Section 25: GUI Programming
-
Section 26: Practical Projects
-
Section 27: Data Structures and Algorithms
-
Section 28: Interview and Algorithm Practice
-
Section 29: JSON and Data Interchange
-
Section 30: More Concurrency Utilities
-
Section 31: More Collections and Streams Practice
-
Section 32: More File and System Programming
-
Section 33: Standard Library Deep Dive
-
Section 34: More Practice and Drills
-
Section 35: More Testing and Quality
-
Section 36: More Design Patterns and Architecture
-
Section 37: Career and Ecosystem
-
Section 38: More OOP and Architecture Practice
-
Section 39: More Enterprise Concepts
-
Section 40: Advanced JavaFX
-
Section 41: More Interview Practice
12: Control Flow: If/Else
I've been working on a small project for a local cinema, and I've hit a bit of a wall with the ticketing logic. Right now, my code is too honest—or rather, it's too simple. Let's look at what I've got.
int age = 25;
System.out.println("Ticket price: $10");
System.out.println("You qualify for the student discount!");
Why this program is lying to me
The problem here is obvious. If I run this, it tells every single person—regardless of whether they are 5 or 95—that they get a student discount. In the real world, that's a great way to lose money. I need the program to actually decide whether to print that second line based on the value of the age variable.
In Java, we do this with an if statement. I'll try wrapping that discount message in a condition.
int age = 25;
System.out.println("Ticket price: $10");
if (age < 18) {
System.out.println("You qualify for the student discount!");
}
Now, if I run this with age = 25, the discount message vanishes. Perfect. If I change age to 15, it pops back up. The code inside the curly braces only executes if the expression inside the parentheses evaluates to true. It's a simple gatekeeper.
Dealing with the silence
But there's a UX problem now. When someone is 25, the program just stops after printing the price. It feels abrupt. I want to tell the user explicitly that they don't qualify for the discount, rather than just staying silent.
This is where else comes in. It's basically the "otherwise" branch of the logic.
int age = 25;
System.out.println("Ticket price: $10");
if (age < 18) {
System.out.println("You qualify for the student discount!");
} else {
System.out.println("Standard pricing applies.");
}
Now we have a complete circuit. If the if condition is true, the first block runs and the else block is ignored. If the if is false, Java jumps straight to the else. No one is left wondering why the program stopped talking to them.
It's not just black and white
Here is where it gets interesting. The cinema owner just told me that seniors (65+) also get a discount, but it's a different one. I can't just use a simple if/else because that only allows for two paths: "Discount" or "No Discount."
I could nest another if inside the else, but that gets messy fast—it's called "arrow code" because the indentation starts looking like a sideways arrow. Instead, I'll use else if to create a chain of checks.
int age = 70;
System.out.println("Ticket price: $10");
if (age < 18) {
System.out.println("You qualify for the student discount!");
} else if (age >= 65) {
System.out.println("You qualify for the senior discount!");
} else {
System.out.println("Standard pricing applies.");
}
I love how this reads. Java checks the first condition. If it's false, it moves to the else if. If that's also false, it finally falls back to the else. Crucially, as soon as Java finds one true condition, it executes that block and skips the rest of the chain entirely. If I'm 70, I get the senior discount, and Java doesn't even bother checking the final else.
📋 Practical Task
Build a Cinema Ticket Price Calculator
Write a program that determines the final price of a movie ticket based on a user's age. Use a variable int age to simulate the input.
- If the age is under 13, the price is $7.
- If the age is between 13 and 64 (inclusive), the price is $12.
- If the age is 65 or older, the price is $9.
The program should print a message stating the final price. For example: "Your ticket price is $7". Use an if / else if / else structure to handle these three distinct pricing tiers.
There are no comments for now.