Skip to Content
Course content

12: Control Flow: If/Else

Click on the "Edit" button in the top corner of the screen to edit your slide content.

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.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.