Start Learning
Javaneer
Back to stage
Stage 1·Core Language

Java Syntax & Control Flow

The anatomy of a class, statements and scope, conditionals, loops, and methods.

20 min readBeginner
On this page

A program that only stores values isn't very useful. Real programs make decisions and repeat actions. This lesson covers the anatomy of a Java program and the control flow - if, switch, loops, and methods - that brings code to life.

The anatomy of a Java program

Every Java program has the same basic skeleton:

public class Program {          // 1. a class
    public static void main(String[] args) {   // 2. the main method
        // 3. statements run here, top to bottom
        System.out.println("Running!");
    }
}
  • A class is a container for your code (more on classes in Stage 2).
  • The main method is where the program starts.
  • Statements inside run one after another, each ending with a semicolon ;.
  • Curly braces { } group statements into blocks.
Blocks and scope are like nested rooms

Think of { } as rooms in a house. A variable declared inside a room exists only in that room (its scope) and vanishes when you leave. Code in an inner room can see things in the outer rooms, but not the other way around.

Making decisions with if / else

An if statement runs code only when a condition is true:

int temperature = 30;

if (temperature > 25) {
    System.out.println("It's hot!");
} else if (temperature > 15) {
    System.out.println("Pleasant weather.");
} else {
    System.out.println("Bring a jacket.");
}

Java checks each condition top to bottom and runs the first block whose condition is true. If none match, the else block runs.

switch: choosing among many options

When you're comparing one value against many fixed options, a switch is cleaner than a long if/else chain. Modern Java has a concise switch expression:

int day = 3;

String name = switch (day) {
    case 1 -> "Monday";
    case 2 -> "Tuesday";
    case 3 -> "Wednesday";
    default -> "Some other day";
};

System.out.println(name);  // Wednesday

Repeating with loops

Loops repeat a block of code. Java has three you'll use constantly.

for loop - when you know how many times to repeat:

for (int i = 1; i <= 3; i++) {
    System.out.println("Count: " + i);
}
// Count: 1
// Count: 2
// Count: 3

Read it as: start at i = 1; keep going while i <= 3; add 1 to i each time.

while loop - repeat as long as a condition holds:

int fuel = 3;
while (fuel > 0) {
    System.out.println("Driving... fuel = " + fuel);
    fuel--;   // decrease by 1
}

Enhanced for ("for-each") - step through every item in a collection:

String[] fruits = { "apple", "banana", "cherry" };
for (String fruit : fruits) {
    System.out.println(fruit);
}

Beware the infinite loop

If a loop's condition never becomes false, it runs forever and freezes your program. Always make sure something inside the loop moves it toward the exit - like fuel-- above.

Methods: reusable blocks of logic

A method is a named, reusable block of code. Instead of repeating logic, you write it once and call it whenever you need it.

public class Calc {
    // a method that takes two ints and returns their sum
    static int add(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        int result = add(3, 4);
        System.out.println(result);   // 7
        System.out.println(add(10, 20)); // 30
    }
}
  • Parameters (int a, int b) are inputs.
  • return sends a value back to whoever called the method.
  • void means the method returns nothing (like main).
A method is like a coffee machine

You don't rebuild a coffee machine each morning. You put in inputs (water, beans) and press a button; it returns coffee. A method is the same: give it inputs, and it hands back a result - reusable as often as you like.

Method overloading lets several methods share a name with different parameters:

static int add(int a, int b)          { return a + b; }
static double add(double a, double b) { return a + b; }

Java picks the right one based on the arguments you pass.

Quick check

Which loop is the best fit when you know exactly how many times to repeat?

Key takeaways

  • Every program lives in a class; execution starts in the main method; statements end with semicolons.
  • Curly braces define blocks and scope - variables live only inside their block.
  • if / else if / else run the first matching block; switch cleanly chooses among many options.
  • for loops fit known counts; while loops repeat while a condition holds; for-each iterates collections.
  • Methods are named, reusable blocks that take parameters and return values; void returns nothing.
  • Overloading lets methods share a name with different parameter lists.

Next, we'll dig into two things you'll use in nearly every program: Strings and arrays.