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 = ;
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 = ;
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 = ;
if (x > 5) {
System.out.println("x > 5");
}
if (x > 20) {
System.out.println("x > 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];
}
}
}
}
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];
}
}
}
}
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 = ;
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 = ;
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 = ;
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));
}
}
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 = ;
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 = ;
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 = ;
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;
}
}
}
}
Loop through, check each element against the target. When found, we can stop early or record the position.