You're building a login system. If the password matches, show the dashboard. Otherwise, show "Invalid password". Programs need to take different paths based on conditions - that's what conditionals do.

Simple if

Execute code only when a condition is true.

x
Simple.java
Replay: real traced execution (multi-file project)
public class Simple {
    public static void main(String[] args) {
        int x = 10;

        if (x > 5) {
            System.out.println("x > 5");
        }

        if (x > 20) {
            System.out.println("x > 20");
        }
    }
}
public class Simple {
    public static void main(String[] args) {
        int x = 3;

        if (x > 5) {
            System.out.println("x > 5");
        }

        if (x > 20) {
            System.out.println("x > 20");
        }
    }
}
public class Simple {
    public static void main(String[] args) {
        int x = 25;

        if (x > 5) {
            System.out.println("x > 5");
        }

        if (x > 20) {
            System.out.println("x > 20");
        }
    }
}
  1. x ← 10

    1public class Simple {2    public static void main(String[] args) {3        int x→ 10 = 10;  //@x=3, 25
  2. if (x > 5)

    5if (x10 > 5) {6    System.out.println("x > 5");7}
    outputx > 5
  1. x ← 3

    1public class Simple {2    public static void main(String[] args) {3        int x→ 3 = 3;
  1. x ← 25

    1public class Simple {2    public static void main(String[] args) {3        int x→ 25 = 25;
  2. if (x > 5)

    5if (x25 > 5) {6    System.out.println("x > 5");7}
    outputx > 5
  3. if (x > 20)

    9if (x25 > 20) {10    System.out.println("x > 20");11}
    outputx > 20

The code inside if only runs when the condition is true.

if `if (condition)` - executes the block only when condition is true.
comparison `>` greater, `<` less, `>=` greater or equal, `<=` less or equal, `==` equal.

Find the maximum in an array

Scan through all elements, keep track of the largest one found.

Max.java
Replay: real traced execution (multi-file project)
public class Max {
    public static void main(String[] args) {
        int[] nums = {23, 45, 12, 67, 34, 89, 41};

        System.out.println("nums=" + java.util.Arrays.toString(nums));
        int max = nums[0];

        for (int i = 1; i < nums.length; i++) {
            if (nums[i] > max) {
                max = nums[i];
            }
        }
    }
}
  1. max ← 23

    1public class Max {2    public static void main(String[] args) {3        int[] nums = {23, 45, 12, 67, 34, 89, 41};4        5        System.out.println("nums=" + java.util.Arrays.toString(nums));6        int max→ 23 = nums[0]23;
    outputnums=[23, 45, 12, 67, 34, 89, 41]
  2. for (int i = 1; i < nums.length; i++)

    pass 1 of 6
    8for (int i1 = 1; i < nums.length7; i++) {9    if (nums[i] > max) {
    All 6 passes — pass 1 is the card above
    passi
    11
    22
    33
    44
    55
    66
  3. max ← 45

    pass 1 of 3
    8for (int i = 1; i < nums.length; i++) {9    if (nums[i]45 > max23) {10        max→ 45 = nums[i]45;11    }
    All 3 passes — pass 1 is the card above
    passnums[i]imax
    145123 → 45
    267345 → 67
    389567 → 89

Compare each element to our current max. If bigger, update max. This is the find maximum pattern.

Find the minimum in an array

Same idea, but track the smallest.

Min.java
Replay: real traced execution (multi-file project)
public class Min {
    public static void main(String[] args) {
        int[] nums = {23, 45, 12, 67, 34, 89, 41};

        System.out.println("nums=" + java.util.Arrays.toString(nums));
        int min = nums[0];

        for (int i = 1; i < nums.length; i++) {
            if (nums[i] < min) {
                min = nums[i];
            }
        }
    }
}
  1. min ← 23

    1public class Min {2    public static void main(String[] args) {3        int[] nums = {23, 45, 12, 67, 34, 89, 41};4        5        System.out.println("nums=" + java.util.Arrays.toString(nums));6        int min→ 23 = nums[0]23;
    outputnums=[23, 45, 12, 67, 34, 89, 41]
  2. for (int i = 1; i < nums.length; i++)

    pass 1 of 6
    8for (int i1 = 1; i < nums.length7; i++) {9    if (nums[i] < min) {
    All 6 passes — pass 1 is the card above
    passinums[i]min
    11——
    221223 → 12
    33——
    44——
    55——
    66——
  3. min ← 12

    8for (int i = 1; i < nums.length; i++) {9    if (nums[i]12 < min23) {10        min→ 12 = nums[i]12;11    }
    values this step2i

Same pattern, opposite comparison: update when you find something smaller.

else `else` - executes when the if condition is false.

First N Fibonacci numbers

Generate a variable-length Fibonacci sequence.

n
FibonacciN.java
Replay: real traced execution (multi-file project)
public class FibonacciN {
    public static void main(String[] args) {
        int n = 12;
        int[] fib = new int[n];

        fib[0] = 0;
        fib[1] = 1;

        for (int i = 2; i < n; i++) {
            fib[i] = fib[i - 1] + fib[i - 2];
        }

        System.out.println("fib=" + java.util.Arrays.toString(fib));
    }
}
public class FibonacciN {
    public static void main(String[] args) {
        int n = 5;
        int[] fib = new int[n];

        fib[0] = 0;
        fib[1] = 1;

        for (int i = 2; i < n; i++) {
            fib[i] = fib[i - 1] + fib[i - 2];
        }

        System.out.println("fib=" + java.util.Arrays.toString(fib));
    }
}
public class FibonacciN {
    public static void main(String[] args) {
        int n = 20;
        int[] fib = new int[n];

        fib[0] = 0;
        fib[1] = 1;

        for (int i = 2; i < n; i++) {
            fib[i] = fib[i - 1] + fib[i - 2];
        }

        System.out.println("fib=" + java.util.Arrays.toString(fib));
    }
}
  1. n ← 12, fib[1] ← 1

    1public class FibonacciN {2    public static void main(String[] args) {3        int n→ 12 = 12;  //@n=5, 204        int[] fib = new int[n];5        6        fib[0]0 = 0;7        fib[1]→ 1 = 1;
  2. fib[i] ← 1

    pass 1 of 10
    9for (int i2 = 2; i < n12; i++) {10    fib[i]→ 1 = fib[i - 1]1 + fib[i - 2]0;11}
    All 10 passes — pass 1 is the card above
    passifib[i - 1]fib[i - 2]fib[i]
    12100 → 1
    23110 → 2
    34210 → 3
    45320 → 5
    56530 → 8
    67850 → 13
    781380 → 21
    8921130 → 34
    91034210 → 55
    101155340 → 89
  3. System.out.println("fib=" + java.util.Arrays.toString(fib));

    13    System.out.println("fib=" + java.util.Arrays.toString(fib));14}
    outputfib=[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
  1. n ← 5, fib[1] ← 1

    1public class FibonacciN {2    public static void main(String[] args) {3        int n→ 5 = 5;4        int[] fib = new int[n];5        6        fib[0]0 = 0;7        fib[1]→ 1 = 1;
  2. fib[i] ← 1

    pass 1 of 3
    9for (int i2 = 2; i < n5; i++) {10    fib[i]→ 1 = fib[i - 1]1 + fib[i - 2]0;11}
    All 3 passes — pass 1 is the card above
    passifib[i - 1]fib[i - 2]fib[i]
    12100 → 1
    23110 → 2
    34210 → 3
  3. System.out.println("fib=" + java.util.Arrays.toString(fib));

    13    System.out.println("fib=" + java.util.Arrays.toString(fib));14}
    outputfib=[0, 1, 1, 2, 3]
  1. n ← 20, fib[1] ← 1

    1public class FibonacciN {2    public static void main(String[] args) {3        int n→ 20 = 20;4        int[] fib = new int[n];5        6        fib[0]0 = 0;7        fib[1]→ 1 = 1;
  2. fib[i] ← 1

    pass 1 of 18
    9for (int i2 = 2; i < n20; i++) {10    fib[i]→ 1 = fib[i - 1]1 + fib[i - 2]0;11}
    18 passes — pass 1 is the card above
    passifib[i - 1]fib[i - 2]fib[i]
    12100 → 1
    23110 → 2
    34210 → 3
    45320 → 5
    56530 → 8
    67850 → 13
    781380 → 21
    8921130 → 34
    91034210 → 55
    ⋯ 7 more passes ⋯
    171815979870 → 2584
    1819258415970 → 4181
  3. System.out.println("fib=" + java.util.Arrays.toString(fib));

    13    System.out.println("fib=" + java.util.Arrays.toString(fib));14}
    outputfib=[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181]

The user specifies how many Fibonacci numbers they want. The condition i < n controls how many iterations run.

Search for a value in array

Check if a specific number exists.

target
Search.java
Replay: real traced execution (multi-file project)
public class Search {
    public static void main(String[] args) {
        int[] nums = {23, 45, 12, 67, 34, 89, 41};
        int target = 67;

        System.out.println("nums=" + java.util.Arrays.toString(nums));
        int found = 0;
        int position = -1;

        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == target) {
                found = 1;
                position = i;
            }
        }
    }
}
public class Search {
    public static void main(String[] args) {
        int[] nums = {23, 45, 12, 67, 34, 89, 41};
        int target = 12;

        System.out.println("nums=" + java.util.Arrays.toString(nums));
        int found = 0;
        int position = -1;

        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == target) {
                found = 1;
                position = i;
            }
        }
    }
}
public class Search {
    public static void main(String[] args) {
        int[] nums = {23, 45, 12, 67, 34, 89, 41};
        int target = 99;

        System.out.println("nums=" + java.util.Arrays.toString(nums));
        int found = 0;
        int position = -1;

        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == target) {
                found = 1;
                position = i;
            }
        }
    }
}
  1. target ← 67, found ← 0, position ← -1

    1public class Search {2    public static void main(String[] args) {3        int[] nums = {23, 45, 12, 67, 34, 89, 41};4        int target→ 67 = 67;  //@target=12, 995        6        System.out.println("nums=" + java.util.Arrays.toString(nums));7        int found→ 0 = 0;8        int position→ -1 = -1;
    outputnums=[23, 45, 12, 67, 34, 89, 41]
  2. for (int i = 0; i < nums.length; i++)

    pass 1 of 7
    10for (int i0 = 0; i < nums.length7; i++) {11    if (nums[i] == target) {
    All 7 passes — pass 1 is the card above
    passinums[i]targetfoundposition
    10————
    21————
    32————
    43676713
    54————
    65————
    76————
  3. found ← 1, position ← 3

    10for (int i = 0; i < nums.length; i++) {11    if (nums[i]67 == target67) {12        found→ 1 = 1;13        position→ 3 = i3;14    }
  1. target ← 12, found ← 0, position ← -1

    1public class Search {2    public static void main(String[] args) {3        int[] nums = {23, 45, 12, 67, 34, 89, 41};4        int target→ 12 = 12;5        6        System.out.println("nums=" + java.util.Arrays.toString(nums));7        int found→ 0 = 0;8        int position→ -1 = -1;
    outputnums=[23, 45, 12, 67, 34, 89, 41]
  2. for (int i = 0; i < nums.length; i++)

    pass 1 of 7
    10for (int i0 = 0; i < nums.length7; i++) {11    if (nums[i] == target) {
    All 7 passes — pass 1 is the card above
    passinums[i]targetfoundposition
    10————
    21————
    32121212
    43————
    54————
    65————
    76————
  3. found ← 1, position ← 2

    10for (int i = 0; i < nums.length; i++) {11    if (nums[i]12 == target12) {12        found→ 1 = 1;13        position→ 2 = i2;14    }
  1. target ← 99, found ← 0, position ← -1

    1public class Search {2    public static void main(String[] args) {3        int[] nums = {23, 45, 12, 67, 34, 89, 41};4        int target→ 99 = 99;5        6        System.out.println("nums=" + java.util.Arrays.toString(nums));7        int found→ 0 = 0;8        int position→ -1 = -1;
    outputnums=[23, 45, 12, 67, 34, 89, 41]
  2. for (int i = 0; i < nums.length; i++)

    pass 1 of 7
    10for (int i0 = 0; i < nums.length7; i++) {11    if (nums[i] == target) {
    All 7 passes — pass 1 is the card above
    passi
    10
    21
    32
    43
    54
    65
    76

Loop through, check each element against the target. When found, we can stop early or record the position.