Java Foundations·Objects that hold up

Syntax and control flow

Classes, main, variables and the branches and loops that decide what your program does.

Beginner 13 min read Lesson 1 of 300 Challenge included
What you will be able to do
  • Declare variables with a fixed type before using them.
  • Choose if/else for ranges and switch for matching one value.
  • Use a for loop for a known count and a while loop for an unknown count.
  • Keep variables in the smallest scope that needs them.

In plain English

A Java program is made of classes, and a runnable program needs a main method, which is the entry point where the program starts. The JVM, or Java Virtual Machine, is the program that runs your compiled bytecode, so the same Java file can run on any computer that has a JVM. A variable stores one value of a fixed type, such as int for whole numbers, boolean for true or false, or String for text. An if and else statement chooses between branches based on a condition, while a switch matches one value against several fixed cases. A for loop repeats a block a known number of times and a while loop repeats until a condition becomes false, and scope is the region where a variable can be seen before it disappears when its block ends.

Step by step

  1. 1Create a class with a main method that prints one line.
  2. 2Declare variables with int, boolean and String types.
  3. 3Add an if/else branch and a switch for a single value.
  4. 4Write a for loop and a while loop.
  5. 5Move a variable into a block and notice its scope shrink.
ControlFlow.javaJava
public class ControlFlow {
    public static void main(String[] args) {
        int minutes = 12;
        String level = minutes < 10 ? "short" : "long";

        if (minutes >= 14) {
            System.out.println("deep dive");
        } else if (minutes >= 8) {
            System.out.println("steady lesson");
        } else {
            System.out.println("quick intro");
        }

        switch (level) {
            case "short" -> System.out.println("under 10 minutes");
            case "long" -> System.out.println("10 minutes or more");
            default -> System.out.println("unknown");
        }

        int total = 0;
        for (int i = 0; i < 3; i++) {
            total += i;
        }

        while (total < 5) {
            total += 1;
        }

        System.out.println(total);
    }
}

Common mistakes

  • Using a variable outside the block where it was declared fails to compile, because its scope has already ended.
  • Writing a single = where == belongs in a condition fails to compile, since an assignment does not produce a true or false value.
  • Using a switch when you actually need range comparisons is a mistake, because switch matches a single value exactly.
  • Declaring every variable at the top of a method widens its scope and makes bugs harder to trace.
Your turn

Label a lesson by length

Easy

Write a main method that prints "short" when minutes is below 10, "medium" when it is below 20, and "long" otherwise.

JAVA · starter
public class Label {
    public static void main(String[] args) {
        int minutes = 15;
    }
}
Check your understanding before moving on.
Knowledge checkQuestion 1

What does the JVM do?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 1 of 300 · 0% of course