Chapter 1 · Foundations

Decisions and loops

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 22 · Concept

Questions with true or false answers

In this lesson your programs decide and repeat. Both start with a question whose answer is true or false. You met one in lesson 3: age >= 18.

questionmeans7 ? 10 gives
a < bless thantrue
a > bmore thanfalse
a <= bless than or equaltrue
a >= bmore than or equalfalse
a == bequalfalse
a != bnot equaltrue
public class Weather {
    public static void main(String[] args) {
        int temp = 30;
        if (temp > 25) {
            System.out.println("Hot");
        } else {
            System.out.println("Not hot");
        }
    }
}
public class Grade {
    public static void main(String[] args) {
        int score = 72;
        if (score >= 90) {
            System.out.println("Grade: A");
        } else if (score >= 70) {
            System.out.println("Grade: B");
        } else {
            System.out.println("Grade: C");
        }
    }
}
public class Grade {
    public static void main(String[] args) {
        int score = 120;
        if (score >= 90) {
            System.out.println("Grade: A");
        } else if (score >= 70) {
            System.out.println("Grade: B");
        } else {
            System.out.println("Grade: C");
        }
    }
}
public class Count {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            if (i % 2 == 0) {
                System.out.println(i + " is even");
            } else {
                System.out.println(i + " is odd");
            }
        }
    }
}
public class Sum100 {
    public static void main(String[] args) {
        int total = 0;
        // add every number from 1 to 100 to total
        System.out.println("Sum: " + total);
    }
}
public class Doubling {
    public static void main(String[] args) {
        int n = 1;
        while (n <= 100) {
            System.out.print(n + " ");
            // something is missing here
        }
        System.out.println();
        System.out.println("Past 100: " + n);
    }
}
public class Collatz {
    public static void main(String[] args) {
        int n = 27;
        int steps = 0;
        // while n is not 1:
        //   halve n if it is even, else make it 3 * n + 1
        //   then count the step
        System.out.println("Steps: " + steps);
    }
}
public class FizzBuzz {
    public static void main(String[] args) {
        for (int n = 1; n <= 15; n++) {
            System.out.println(n);
        }
    }
}
public class Sevens {
    public static void main(String[] args) {
        // the ten multiples of 7 on one line,
        // a * after each one divisible by 3 or over 60
        // then the line Marked: and how many got a *
    }
}
public class Countdown {
    public static void main(String[] args) {
        System.out.println("Liftoff!");
    }
}
Terminal
Step 1 of 10Try it: hot or not

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)