Foundations
Conditionals
Make Decisions
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.
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");
}
}
}
x ← 10
1public class Simple {2 public static void main(String[] args) {3 int x→ 10 = 10; //@x=3, 25if (x > 5)
5if (x10 > 5) {6 System.out.println("x > 5");7}outputx > 5
x ← 3
1public class Simple {2 public static void main(String[] args) {3 int x→ 3 = 3;
x ← 25
1public class Simple {2 public static void main(String[] args) {3 int x→ 25 = 25;if (x > 5)
5if (x25 > 5) {6 System.out.println("x > 5");7}outputx > 5if (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.
Find the maximum in an array
Scan through all elements, keep track of the largest one found.
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];
}
}
}
}
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]for (int i = 1; i < nums.length; i++)
pass 1 of 68for (int i1 = 1; i < nums.length7; i++) {9 if (nums[i] > max) {All 6 passes — pass 1 is the card above pass i1 1 2 2 3 3 4 4 5 5 6 6 max ← 45
pass 1 of 38for (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 pass nums[i]imax1 45 1 23 → 45 2 67 3 45 → 67 3 89 5 67 → 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.
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];
}
}
}
}
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]for (int i = 1; i < nums.length; i++)
pass 1 of 68for (int i1 = 1; i < nums.length7; i++) {9 if (nums[i] < min) {All 6 passes — pass 1 is the card above pass inums[i]min1 1 — — 2 2 12 23 → 12 3 3 — — 4 4 — — 5 5 — — 6 6 — — 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.
First N Fibonacci numbers
Generate a variable-length Fibonacci sequence.
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));
}
}
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;fib[i] ← 1
pass 1 of 109for (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 pass ifib[i - 1]fib[i - 2]fib[i]1 2 1 0 0 → 1 2 3 1 1 0 → 2 3 4 2 1 0 → 3 4 5 3 2 0 → 5 5 6 5 3 0 → 8 6 7 8 5 0 → 13 7 8 13 8 0 → 21 8 9 21 13 0 → 34 9 10 34 21 0 → 55 10 11 55 34 0 → 89 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]
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;fib[i] ← 1
pass 1 of 39for (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 pass ifib[i - 1]fib[i - 2]fib[i]1 2 1 0 0 → 1 2 3 1 1 0 → 2 3 4 2 1 0 → 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]
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;fib[i] ← 1
pass 1 of 189for (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 pass ifib[i - 1]fib[i - 2]fib[i]1 2 1 0 0 → 1 2 3 1 1 0 → 2 3 4 2 1 0 → 3 4 5 3 2 0 → 5 5 6 5 3 0 → 8 6 7 8 5 0 → 13 7 8 13 8 0 → 21 8 9 21 13 0 → 34 9 10 34 21 0 → 55 ⋯ 7 more passes ⋯ 17 18 1597 987 0 → 2584 18 19 2584 1597 0 → 4181 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.
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;
}
}
}
}
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]for (int i = 0; i < nums.length; i++)
pass 1 of 710for (int i0 = 0; i < nums.length7; i++) {11 if (nums[i] == target) {All 7 passes — pass 1 is the card above pass inums[i]targetfoundposition1 0 — — — — 2 1 — — — — 3 2 — — — — 4 3 67 67 1 3 5 4 — — — — 6 5 — — — — 7 6 — — — — 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 }
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]for (int i = 0; i < nums.length; i++)
pass 1 of 710for (int i0 = 0; i < nums.length7; i++) {11 if (nums[i] == target) {All 7 passes — pass 1 is the card above pass inums[i]targetfoundposition1 0 — — — — 2 1 — — — — 3 2 12 12 1 2 4 3 — — — — 5 4 — — — — 6 5 — — — — 7 6 — — — — 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 }
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]for (int i = 0; i < nums.length; i++)
pass 1 of 710for (int i0 = 0; i < nums.length7; i++) {11 if (nums[i] == target) {All 7 passes — pass 1 is the card above pass i1 0 2 1 3 2 4 3 5 4 6 5 7 6
Loop through, check each element against the target. When found, we can stop early or record the position.