MT2 Cram: Conditionals, Boolean logic, and loops
Part 1: Conditions (if, else if, else)
First, understand what a conditional does:
A conditional lets your program make a decision
Imagine telling a program:
If the temperature is below 32, print
"Freezing".Otherwise, print
"Not freezing".
Java needs a way to express that decision.
The basic structure:
if (temperature < 32) {
System.out.println("Freezing");
} else {
System.out.println("Not freezing");
}
Let’s break this down:
iftells Java we're checking a condition.(temperature < 32)is the condition.{ }contains the code that runs if the condition is true.elsehandles everything where the condition is false.
Java checks whether condition is true or false
The comparison operators
Operator | Meaning | Example |
|---|---|---|
| Equal to |
|
| Not equal to |
|
| Greater than |
|
| Less than |
|
| Greater than or equal to |
|
| Less than or equal to |
|
== and = are NOT the same
x = 5; // Assign 5 to x
x == 5; // Check whether x equals 5
else if - multiple possibilities
if (grade >= 90) {
System.out.println("A");
} else if (grade >= 80) {
System.out.println("B");
} else {
System.out.println("C");
}
Java checks from TOP TO BOTTOM.
Suppose grade = 85.
Is
85 >= 90? No.Is
85 >= 80? Yes.Print
"B"and skip the remaining branches.
Only one branch runs in this chain.
Boolean Logic (&&, ||, !)
These operators let us combine conditions.
| Operator | Meaning | Example |
|---|---|---|
| && | AND — both must be true | x > 0 && x < 10 |
| || | OR — at least one must be true | x < 0 || x > 10 |
| ! | NOT — reverses true/false | !(x == 5) |
Example:
int age = 20;
if (age >= 18 && age <= 25) {
System.out.println("Eligible");
}
Both conditions must be true:
age >= 18→ true.age <= 25→ true.
Therefore, the entire AND expression is true.
One concept: De Morgan’s law
These expressions are equivalent:
!(A && B)
and
!A || !B
Likewise:
!(A || B)
and
!A && !B
Basically:
Negating AND turns it into OR.
Negating OR turns it into AND.
Negate each individual condition.
Loops
Imagine I wanted java to print numbers 1-5
You could write:
System.out.println(1);
System.out.println(2);
System.out.println(3);
System.out.println(4);
System.out.println(5);
but this is BUNS, so we use a loop.
There are two main types you need to understand:
forloopswhileloops
The For Loop:
A for loop is useful when you generally know how many times you want something to repeat.
Here’s the structure:
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
This prints:
0
1
2
3
4
The while loop:
A while loop repeats as long as its condition is true.
Example:
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
This produces the exact same output as our first for loop.
So what's different?
A for loop puts the initialization, condition, and update together.
A while loop separates them:
|
|
|---|---|
Initialization inside loop header | Initialization before loop |
Condition inside loop header | Condition after |
Update inside loop header | Update usually inside body |
CRITICAL: You have to remember to update the variable in a while loop.
int i = 0;
while (i < 5) {
System.out.println(i);
}
there is no i++, it stays 0 forever so this is an infinite loop
Counters v Acculumators:
Counter: Keeps track of how many times something happens
int count = 0;
for (int i = 1; i <= 5; i++) {
count++;
}
After the loop, count = 5
Accumulator: Keeps a running total
int sum = 0;
for (int i = 1; i <= 5; i++) {
sum += i;
}
Let's trace the sum:
|
|
|
|---|---|---|
1 | 0 | 1 |
2 | 1 | 3 |
3 | 3 | 6 |
4 | 6 | 10 |
5 | 10 | 15 |
The final answer is 15.
What is a nested loop?
A nested loop is simply a loop within a loop
Look:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
System.out.println(i + " " + j);
}
}
We have an outer loop using i
An inner loop using j
The outer loop controls how many times the inner loop gets repeated.
Here’s the most important rule: Every time the outer loop runs once, the inner loop completes ALL its iterations.
Let’s trace:
Initially:
i = 1
Java enters outer loop.
Now the inner loop starts:
|
| Printed |
|---|---|---|
1 | 1 |
|
1 | 2 |
|
The inner loop is finished.
Now outer loop updates:
i = 2
The inner loop starts AGAIN:
|
| Printed |
|---|---|---|
2 | 1 |
|
2 | 2 |
|
Inner loop finishes again.
Outer loop updates:
i = 3
One final time:
|
| Printed |
|---|---|---|
3 | 1 |
|
3 | 2 |
|
And now the outer loop ends.
Final Output:
1 1
1 2
2 1
2 2
3 1
3 2
How do we count nested loop iterations?
Consider:
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 3; j++) {
System.out.println("*");
}
}
The outer loop runs 4 times, the inner loop runs 3 times for each OUTER iteration
Therefore 4×3=12 so it prints 12 stars
General rule: If both loops have fixed iteration counts, multiply them to find the total number of executions of the inner body.
Nested loops can create patterns
For example:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
Output:
*
**
***
Let’s understand why:
The outer loop controls the rows
The inner loop controls how many stars appear in each row
Outer | Inner loop condition | Number of stars |
|---|---|---|
1 |
| 1 |
2 |
| 2 |
3 |
| 3 |
Notice this line:
System.out.println();
It's OUTSIDE the inner loop but INSIDE the outer loop.
That means Java moves to a new line after finishing each row.
A harder nested loop:
Look at this:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j<=4; j++) {
System.out.print(i);
}
System.out.println();
}
What happens?
When
i = 1, print1four times.When
i = 2, print2four times.When
i = 3, print3four times.
Output:
1111
2222
3333
The outer variable determines WHAT gets printed.
The inner variable determines HOW MANY TIMES it gets printed.
That's a useful way to think about it.
The % operator and extracting digits
Now we are getting into another recurring topic
Understand these 2 operators:
Operator | Meaning |
|---|---|
| Division |
| Remainder |
In Java, when both numbers are integers, division discards the decimal portion.
For example:
17 / 5
the answer is 3, not 3.4
But:
17 % 5
The answer is 2 because 5 goes into 17 3 times with 2 left over
Why is % 10 so important?
It extracts the LAST digit of a positive integer
Example:
int num = 472;
System.out.println(num % 10);
Output: 2
Why?
472 / 10 leaves remainder of 2
Now look at:
System.out.println(num / 10);
Output: 47
Integer division removes the last digit
Reversing a number
First practice exam asks us to reverse a positive integer.
Suppose we have:
int num = 472;
We want 274
We need 2 variables
int reverse = 0;
num is the original number, reverse will gradually reverse the number
Here’s the code:
int num = 472;
int reverse = 0;
while (num > 0) {
int digit = num % 10;
reverse = reverse * 10 + digit;
num = num / 10;
}
System.out.println(reverse);
digit = 472 % 10;
digit = 2
reverse = 0 * 10 + 2;
reverse = 2
num = 472 / 10;
removes the last digit so num = 47
Second iteration:
digit = 47 % 10→ 7reverse = 2 * 10 + 7→ 27num = 47 / 10→ 4
Third iteration:
digit = 4 % 10→ 4reverse = 27 * 10 + 4→ 274num = 4 / 10→ 0
Now num > 0 is false. The loop ends.