Controlling Your Code: Ifs, Else, and Decisions

So far, your Java programs have followed a single path: start at the top, go line by line, and stop at the end. That’s useful — but not smart. Now it's time to add logic — the ability for your programs to make decisions.

What You’ll Learn

Why Decisions Matter

Programs need to respond to different situations. If a user is logged in — show the dashboard. If they enter the wrong password — show an error. Let’s start with a basic structure:

if else structure

The if, else if, else Flow

An if statement lets the program ask a true/false question. If the answer is true, it runs a block of code. If it's false, it skips that block and checks the next one in the chain. Here's how it works:

This structure helps your program take one of several possible paths, depending on the input or data. Example:

if else flow

You can add more branches using else if:

if else flow with more branches

Note: only one block in this chain runs — the first one that evaluates to true.

What if you use multiple ifs?

If you write multiple separate if statements instead of using else if, more than one can run:

multiple ifs example

Output:

multiple ifs output

Now compare that to an if–else if structure:

if else example

Output:

if else output

The second condition (x > 0) was also true — but it was ignored, because the first match already ran.

Comparison Operators

These are used to check conditions:

comparison operators

Use them inside your if statements to compare values.

Logical Operators

You can combine multiple conditions using logic:

logical operators

Example:

logical operators example

Booleans in Action

If a boolean variable already holds true/false, just use it:

booleans in action

Quick Quiz Example

quiz example

This program checks the user's answer and gives feedback based on their input.

Common Mistakes

Using = instead of == (assignment vs. comparison)

common mistakes

Use == to compare values:

comparison mistake

Forgetting braces {} (especially for multi-line blocks):

braces mistake

This runs no matter what! Use braces to group statements properly:

braces fix

Unreachable else if conditions (e.g., if (x > 10) followed by else if (x > 5)):

unreachable else if

Reorder to make sense:

reorder else if

Now conditions are logically correct

Optional: switch Statement

Sometimes, when you're checking a single variable against many fixed values (like menu options or categories), using a switch statement can make your code cleaner than using a long if–else if chain.

switch statement
switch statement output

switch works best with int, char, String, or enum values.

Practice Time!

Now it's your turn! Try these exercises to practice what you've learned:

Ask the user for a number. Tell them if it’s even or odd.
Ask for a score and print a letter grade.
Ask for username and password, check if they match hardcoded values.

What’s Next

Next, we’ll take things a step further and learn how to make your code repeat actions using loops. Get ready to while, for, and do it again!