Difficulty: Beginner to Intermediate | Prerequisites: Java Fundamentals (errors, conventions, data types, operators)
Once you know how Java stores data and evaluates expressions, the next step is learning how to organise code into reusable blocks (methods) and control which blocks run and how many times (conditionals and loops). This is where programs stop being linear sequences and start having real structure. Methods are how you avoid repeating yourself. Conditionals are how your program makes decisions. Loops are how it repeats work. Together, they form the backbone of every Java program you will write in this course and beyond.
Methods let you name a block of code and call it whenever you need it, reducing redundancy and improving readability. Parameters pass data into methods using value semantics for primitives (the method gets a copy, not the original). Loops repeat code, and conditionals decide which code runs. Method overloading lets you have multiple versions of the same method with different parameter lists.
Method
A named group of statements that performs a specific task. Methods fix redundancy and give structure to your code. Think of it as: a reusable recipe you can call by name whenever you need it.
Procedural decomposition
The practice of breaking a large problem into smaller methods, each responsible for one coherent task. In simple terms, dividing your program into bite-sized pieces so each piece is easy to understand and test.
Control flow
The order in which statements and method calls are executed. When a method is called, execution jumps to that method, runs it, and then returns to where it was called.
Algorithm
A step-by-step list of instructions for solving a problem. A structured algorithm splits the solution into coherent, manageable tasks.
Pseudocode
A plain-language description of a program's logic, written before actual code. It captures the layout and flow without worrying about syntax.
Parameter
A variable declared in a method's header that receives a value when the method is called. The order of parameters matters.
Argument
The actual value passed to a method when it is called. Arguments are matched to parameters by position.
Value semantics
The rule that primitive types are passed by value. When a primitive is passed as an argument, the method receives a copy. Changing the parameter inside the method does not affect the original variable.
Method overloading
Having multiple methods with the same name but different parameter lists (different number, order, or types of parameters). Java picks the correct version based on the arguments you pass. Think of it as: Math.abs() works on int, double, float, and long because there are separate overloaded versions for each type.
static
A keyword meaning the method or variable belongs to the class itself, not to any particular object. Static methods can be called without creating an instance of the class. In simple terms, a static method is a standalone tool. Math.abs() is static because you do not need to create a Math object to use it.
For loop
A loop with three parts in its header: initialisation, test condition, and update. It runs as long as the test condition is true.
Cumulative algorithm
A pattern where you accumulate a result (e.g., a running total) across iterations of a loop. The accumulator variable must be declared outside the loop.
If/else statement
A conditional construct. The if block runs when the test is true; the else block runs when it is false. Exactly one of the two blocks will execute.
If/else if chain
A series of conditional tests evaluated in order. The first true condition's block runs, and the rest are skipped. It is possible that none of them run if no condition is true and there is no trailing else.
Relational operators
Operators that compare two values: ==, !=, <, <=, >, >=. They return a boolean. They cannot be chained in Java the way you might in mathematics (e.g., 1 < x < 10 is not valid; use 1 < x && x < 10).
Logical operators
&& (and), || (or), ! (not). Used to combine boolean expressions.
Truth table
A table showing all possible input combinations for logical operators and their resulting boolean values. Essential for understanding compound conditions.
Expression (loop)
A formula of the form multiplier * loopVariable + constant, used to generate a sequence of values from a simple counting loop.
A method is a named, reusable block of code. Calling a method executes its statements and returns control to the caller.
Methods eliminate redundancy: write the logic once, call it many times.
Procedural decomposition means splitting a program into methods where each method handles one task.
Control flow tracks where execution is at any given moment, including jumps into and returns from method calls.
Comments should appear at the top of each method, explaining what it does.
Parameters are declared in the method header: public static void greet(String name, int times).
Arguments are the values you pass in when calling the method: greet("Alice", 3).
Order matters. The first argument maps to the first parameter, the second to the second, and so on.
For primitive types (int, double, char, boolean), Java uses value semantics. The method receives a copy of the value. Modifying the copy inside the method has no effect on the original variable outside it.
Overloaded methods share the same name but differ in their parameter lists.
Java selects the correct version at compile time based on the number and types of arguments.
Example: Math.abs(int) and Math.abs(double) are two different overloaded methods.
The return type alone does not distinguish overloaded methods. The parameter list must differ.
Math is a utility class. It cannot be instantiated with a constructor; all its methods are static.
Math.pow(base, exp) returns a double. Cast to int if needed.
Math.ceil() and Math.floor() return double (even though the result is a whole number conceptually).
Math.round() returns an int (or long).
Math.abs() is overloaded for int, double, float, and long.
Syntax: for (initialisation; test; update) { body }
The initialisation runs once. The test is checked before each iteration. The update runs after each iteration.
A variable declared in the initialisation (e.g., int i = 0) is local to the loop and cannot be used after it.
To generate a custom sequence like 2, 8, 14, 20, 26, 32, you can either step by 6 (i += 6) or use an expression (6 * i - 4).
Declare the accumulator variable (e.g., int sum = 0;) before the loop begins.
Inside the loop, update the accumulator each iteration (e.g., sum += value;).
If you declare the accumulator inside the loop, it resets every iteration and you lose your running total.
if (test) { ... } executes the block only when the test is true.
if (test) { ... } else { ... } guarantees exactly one of the two blocks runs.
if / else if / else if chains test conditions in order. The first true branch executes; the rest are skipped. If no condition is true and there is no else, nothing happens.
Relational operators (==, !=, <, <=, >, >=) cannot be chained. Write a < b && b < c, not a < b < c.
Logical operators: && (and), || (or), ! (not).
Truth tables map every combination of inputs to outputs for &&, ||, and !. Be comfortable constructing them.
// For loop: two ways to produce 2, 8, 14, 20, 26, 32
for (int i = 2; i <= 32; i += 6) {
System.out.print(i + " ");
}
for (int i = 1; i <= 6; i++) {
System.out.print(6 * i - 4 + " ");
}
// Value semantics example
public static void doubleIt(int x) {
x = x * 2; // modifies the local copy only
}
int num = 5;
doubleIt(num);
// num is still 5 here
// Cumulative sum
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
// sum is 55
Procedural decomposition is how professional software is built. No real program is written as one long block of code. Loops power everything from processing every pixel in an image to iterating through customer records in a database. Cumulative algorithms underlie calculations like running totals in spreadsheets, average scores, and bank account balances.
Students often think modifying a parameter inside a method changes the original variable outside it. For primitives, it does not. Java passes primitives by value (a copy).
Students forget to declare the cumulative variable outside the loop, then wonder why their sum is always just the last value.
Students try to chain relational operators like 1 < x < 10. Java does not support this. Use 1 < x && x < 10.
Students assume that an if / else if chain with no else must execute one branch. It might execute none if all conditions are false.
⚠️ Tracing method calls and parameter passing is a staple of exam questions. You will be asked what a variable's value is after a method call, and the answer depends on understanding value semantics.
⚠️ Loop output tracing is extremely common. Be prepared to hand-trace a for loop and write down the output for each iteration.
⚠️ Know the difference between overloaded methods. Exam questions may ask which version of a method Java calls given certain arguments.
⚠️ Cumulative algorithms appear in code-writing questions. Remember to initialise the accumulator before the loop.
⚠️ Truth table questions test your understanding of &&, ||, and !. Be able to evaluate compound boolean expressions.
True or false: If you change a parameter of type int inside a method, the original argument is also changed.
Fill in the blank: Two methods with the same name but different parameter lists are called ______ methods.
True or false: In a for loop, the update step runs before the test condition on each iteration.
Fill in the blank: A cumulative sum variable should be declared ______ the loop.
True or false: if (x > 0) else if (x < 0) guarantees that one of the two branches will execute.
Answers: 1. False. 2. Overloaded. 3. False (the test runs first; the update runs after the body). 4. Outside / before. 5. False (if x == 0, neither branch executes).
Q: What is procedural decomposition, and why is it useful?
A: Procedural decomposition is the practice of dividing a program into smaller methods, each handling one task. It reduces redundancy, makes code easier to read and test, and gives the program a clear structure.
Q: What is the output of the following code?
public static void mystery(int a, int b) {
a = a + b;
b = a - b;
System.out.println(a + " " + b);
}
// called as: mystery(3, 7)
A: Inside the method, a becomes 3 + 7 = 10, then b becomes 10 - 7 = 3. The output is 10 3. The original arguments outside the method are unchanged.
Q: Write a for loop that prints the numbers 5, 10, 15, 20, 25.
A: for (int i = 5; i <= 25; i += 5) { System.out.print(i + " "); }
Q: Why can you not chain relational operators in Java (e.g., 1 < x < 10)?
A: Java evaluates 1 < x first, which returns a boolean (true or false). Then it tries to compare that boolean with 10, which is not a valid operation. You must write 1 < x && x < 10 instead.
Q: What does the static keyword mean when applied to a method?
A: It means the method belongs to the class rather than to any particular object instance. You can call it without creating an object, e.g., Math.abs(-5).
Methods connect directly to object-oriented programming, where instance methods (non-static) define what an object can do. Value semantics for primitives contrasts with reference semantics for objects, which you will encounter when passing arrays or objects to methods. Loops and conditionals are the building blocks of every algorithm, from simple searches to sorting routines covered later in the course.
Java methods, procedural decomposition, control flow, parameters vs arguments, value semantics, pass by value Java, method overloading, static method, Math class Java, for loop syntax, loop tracing, cumulative algorithm, running total, if else Java, else if chain, relational operators, logical operators, truth table, pseudocode, algorithm, CS 101 Java review