This episode covers Groovy's control flow: if/else, switch, loops, and ranges, followed by closures, method definitions, and optional parameters. You will also understand Groovy truth, safe navigation with operators, and Groovy's signature null-safe patterns.

Syntax and data types are just vocabulary. Episode 4 is when you start composing sentences — program logic with control flow and functions. Groovy simplifies nearly every Java construct while also adding features Java doesn't have.
You'll learn if/else, switch, loops with ranges, closures, method definitions, optional parameters, Groovy truth, safe navigation, and null-safe operators. After this episode, you can write real business logic.
Groovy's basic control flow resembles Java, but leverages Groovy truth. The condition doesn't have to be of type Boolean — empty strings, empty lists, and zero are all considered false.
def nama = ""
if (nama) {
println "Nama terisi: ${nama}"
} else {
println "Nama masih kosong"
}Because nama is an empty string, Groovy truth evaluates it as false, so the else block runs. if (nama) with an empty string shows how Groovy removes the boilerplate of checks like nama != null && !nama.isEmpty().
Switch in Groovy is far more flexible than in Java. Cases can be ranges, collections, classes, or even closures:
def nilai = 85
switch (nilai) {
case 90..100:
println "Nilai A"
break
case 80..<90:
println "Nilai B"
break
default:
println "Nilai lain"
}case 90..100 uses a range as a case, and case 80..<90 uses an exclusive range. This feature makes Groovy's switch a very expressive tool for classifying values.
Iteration in Groovy can use ranges directly:
for (i in 1..5) {
println "Iterasi ke-${i}"
}for (i in 1..5) repeats the block five times with i running from 1 to 5. This form is more concise than the classic C-style for, which Groovy still also supports.
for (int i = 0; i < 3; i++) {
println "Indeks ${i}"
}
def jumlah = 0
while (jumlah < 3) {
println "Jumlah: ${jumlah}"
jumlah++
}Choose for (i in 1..5) for simple iteration and the C-style for when you need an index or a special step. Use while when the number of iterations isn't known in advance.
A closure is a block of code that can be stored, passed around, and executed — the heart of Groovy's expressiveness. Closures are written in curly braces with optional parameters separated by an arrow:
def sapa = { nama -> println "Halo, ${nama}" }
sapa("Arman")
def kali = { a, b -> a * b }
println kali(6, 7)def sapa = { nama -> println "Halo, ${nama}" } defines a closure with one parameter. kali(6, 7) calls the kali closure with two arguments and returns the multiplication result.
A closure's power shines when passed to collection methods. This pattern will be used very often in episode 6:
[1, 2, 3, 4].each { angka ->
println "Nilai: ${angka}"
}[1, 2, 3, 4].each { angka -> println "Nilai: ${angka}" } calls the each method, which accepts a closure. Groovy provides the implicit it variable for single-parameter closures, so this can be written even shorter.
Groovy removes the return keyword as an obligation: the last expression's value in a method automatically becomes the return value.
def tambah(int a, int b) {
a + b
}
def sapa(String nama = "Teman") {
"Halo, ${nama}!"
}
println tambah(3, 4)
println sapa()
println sapa("Arman")def tambah(int a, int b) { a + b } returns a + b without return. Note also def sapa(String nama = "Teman"), which uses an optional parameter: if called without arguments, the default value Teman is used.
Groovy supports optional parameters with default values, removing the need for multiple method overloads like in Java. Some rules:
The sapa("Arman") pattern calls the method with one argument, while sapa() uses the default value. This capability removes a lot of the boilerplate Java requires.
The ?. operator avoids NullPointerException by returning null if the calling object is null:
def user = null
println user?.nama
def nama = user?.nama ?: "Anonim"
println namauser?.nama doesn't throw an exception even though user is null. The Elvis operator ?: then provides the replacement value Anonim when the result is null. The combination user?.nama ?: "Anonim" is Groovy's most idiomatic null-safe pattern.
?: Elvis, default value for null; shorthand a ?: b for a != null ? a : b.?. safe navigation for methods and properties.<=> spaceship, compares then returns -1, 0, or 1.Episode 4 completed the foundation of Groovy logic: control flow with Groovy truth, switch with ranges, iteration with for and closures, method definitions with automatic return values and optional parameters, and null-safe operators.
The key takeaways:
?. and ?: eliminate the majority of NullPointerExceptions.In episode 5 next, we'll discuss object-oriented Groovy — classes, objects, properties, constructors, inheritance, traits, interfaces, and an introduction to AST transformations. You'll start building more organized data structures.