Skip to content

Java OOP

0/36 lessons · 0% complete

— XP

Java OOP · Module 0

Decisions and repetition

if / else, comparison, and the three loops you need

12m30 XP
→→→

What you'll be able to do

  • Write an if / else if / else chain that reads like a sentence
  • Use ==, !=, >, <, && and || correctly
  • Choose between for, while and for-each
  • Know why comparing text with == is a trap
1

if / else

Making decisions with if / else

Validation — the heart of Module 4 — is nothing more than an if statement placed in the right spot.

An if runs a block only when a boolean condition is true. Add else if for alternatives and else for everything that is left. Java checks the branches top to bottom and stops at the first true one.

A grading decision
Main.java
1int score = 72;
2if (score >= 90) {
3 System.out.println("Excellent");
4} else if (score >= 60) {
5 System.out.println("Pass");
6} else {
7 System.out.println("Fail");
8}
Order matters: if you put `score >= 60` first, nobody would ever be Excellent.

Operators you will use constantly

OperatorMeansExample
==equal to (same value / same object)amount == 0
!=not equal tostatus != 1
>= <=greater / less or equalamount <= balance
&&and — both must be trueamount > 0 && amount <= balance
||or — at least one is trueisAdmin || isOwner
!not — flips a boolean!isClosed

== on text compares boxes, not contents

For String (and every object) == asks *are these the same object in memory?* while .equals(...) asks *do they contain the same characters?* Use name.equals("Sara"). You will meet this rule again the moment you create your first objects.

Try it: move the boundary

Change `score` to 95, then to 40, and predict each output before running.

Output
Press Run to execute this example.
2

Loops

Repeating work: for, while, for-each

The classic counting loop

Main.java
1for (int i = 1; i <= 3; i++) {
2 System.out.println("Round " + i);
3}
for (int i = 1; i <= 3; i++) {
Three parts separated by ;: start (int i = 1), keep-going test (i <= 3), and step (i++, meaning add one).
System.out.println("Round " + i);
The body runs once per value of i: 1, 2, 3. Change <= to < and it runs twice — that is the famous off-by-one bug.

Which loop should I use?

for / for-each

You know how many times

Main.java
1int[] grades = new int[] { 80, 90, 70 };
2for (int grade : grades) {
3 System.out.println(grade);
4}
  • for-each reads as 'for each grade in grades'
  • No index to get wrong — fewer bugs
  • Use a classic for when you actually need the index

while

You repeat until something changes

Main.java
1int countdown = 3;
2while (countdown > 0) {
3 System.out.println("tick " + countdown);
4 countdown = countdown - 1;
5}
  • The condition is checked before every round
  • Forget to change the variable and it loops forever
  • Perfect when the number of rounds is unknown

Common mistakes

Wrongif (score = 90) { ... }
WhyA single `=` assigns instead of comparing, so this is not even a boolean.
Fixif (score == 90) { ... } — double equals compares.
Wrongif (name == "Sara") { ... }
WhyThat compares references, so it can be false even when the letters match.
Fixif (name.equals("Sara")) { ... }
Wrongwhile (countdown > 0) { System.out.println(countdown); }
Why`countdown` never changes, so the condition stays true forever.
FixChange the variable inside the loop: countdown = countdown - 1;

Lesson playground

Free practice

Print a times table. Change the multiplier or the loop bounds and watch what happens.

Output
Press Run to execute this example.

Check understanding

Quiz

Question 1 of 4

Predict the complete output.

Main.java
1public class Main {
2 public static void main(String[] args) {
3 int total = 0;
4 for (int i = 1; i <= 4; i++) {
5 if (i % 2 == 0) {
6 total = total + i;
7 }
8 }
9 System.out.println(total);
10 int countdown = 2;
11 while (countdown > 0) {
12 System.out.println("tick " + countdown);
13 countdown = countdown - 1;
14 }
15 }
16}

Summary

  • `if / else if / else` picks the first true branch, so order the conditions from strictest to loosest.
  • Combine conditions with `&&` (both) and `||` (either) — mixing them up breaks validation silently.
  • Use `.equals(...)` for text, never `==`.
  • for = known count, for-each = walk a collection, while = repeat until something changes.