HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to GroovyVariablesData TypesOperatorsStringsListsConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 List & Map
🔀 Condition & Loop
🧩 Function
Groovy›Conditions (if / else)

Conditions (if / else)

Making decisions with if-else if-else; conditions in parentheses, blocks in curly braces.

Simple enough for a 5-year-old to understand

decision with if

Example code
1
def age = 20
2
if (age >= 18) {
The condition is inside parentheses.
ConditionThe condition is inside parentheses.
3
println "Adult"
4
} else {
5
println "Child"
6
}
Condition
💡 Hint: The condition is inside parentheses ( ), and the block is inside curly braces { }.

About this topic

Conditions in Groovy are written with if, else if, and else. They look identical to Java but diverge in one important aspect: Groovy's truthiness rules are much broader.

Values considered false in Groovy are: null, zero, empty string, empty collection, and false. This means you don't need to compare a list's length to check if it's populated — you can put it directly into the condition.

This flexibility shortens code but creates a trap for those coming from Java: while Java only accepts boolean values, Groovy accepts everything and interprets it according to its own rules.

What you will learn

  • the if and else structure
  • Groovy truthiness rules
  • the flexibility of the switch structure
  • Ternary and Elvis operators
  • null value checking

Who is this for?

  • developers transitioning from Java
  • those getting unexpected results in conditions
  • writers of configuration scripts

When to use?

When checking collections and string fullness, Groovy truthiness noticeably shortens the code.

If you are checking whether a value matches one of several different formats, Groovy's flexible switch structure does this in a single block.

Common mistakes

Wrong

def liste = []
if (liste != null) {
  println "full"
}

Even though the list is empty, because it's not null, the condition evaluates to true and the wrong message is printed. In Groovy, to check for fullness, it's enough to put the value directly into the condition; an empty collection is already considered false.

Correct

def liste = []
if (liste) {
  println "full"
}

Wrong

def sayi = 0
if (sayi) {
  println "has value"
}

In Groovy, zero is considered false, so the block never runs. In cases where zero is a valid value, this rule silently causes incorrect behavior. If your intention is a null check, you need to write it explicitly.

Correct

def sayi = 0
if (sayi != null) {
  println "has value"
}

Wrong

def deger = "abc"
switch (deger) {
  case "abc":
    println "matched"
  case "def":
    println "this also runs"
}

Just like in Java, branches in Groovy automatically fall through to the next one; since no break command is written, the second branch runs as well. Groovy's switch structure is flexible with pattern matching, but it inherits the fall-through behavior from Java.

Correct

def deger = "abc"
switch (deger) {
  case "abc":
    println "matched"
    break
  case "def":
    println "this does not run"
    break
}

Frequently asked questions

What does the Groovy switch structure match with?

With constant values, ranges, regular expressions, classes, and even closures. It is much broader than Java's structure, which is limited only to constants, and gathers complex branching into a single block.

How do you distinguish between null and an empty string?

Groovy truthiness considers both false. If you want to distinguish them, you must explicitly write the null comparison. This distinction is important when reading configurations: was the setting omitted, or was it left blank?

What is the difference between the ternary operator and the Elvis operator?

The ternary operator chooses between two values based on a condition; the Elvis operator provides the right-hand value if the left-hand value is false. Elvis is a shorthand form that saves you from writing the same expression twice.

Summary

Empty collections and empty strings are considered false.

Zero is also considered false.

Fullness checking is done directly with the condition.

The switch structure flows through branches automatically.

The Elvis operator provides a default value.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code