Chapter 6. Groovy control structures

 

This chapter covers

  • Groovy truth
  • Conditionals and branching
  • Looping
  • Exception handling

The pursuit of truth and beauty is a sphere of activity in which we are permitted to remain children all our lives.

Albert Einstein

At the hardware level, computer systems use simple arithmetic and logical operations, such as jumping to a new location if a memory value equals zero. Any complex flow of logic that a computer is executing can always be expressed in terms of these simple operations. Fortunately, languages like Java raise the abstraction level available in programs you write so that you can express the flow of logic in terms of higher-level constructs—for example, looping through all of the elements in an array or processing characters until you reach the end of a file.

In this chapter, we explore the constructs Groovy provides to describe logic flow in ways that are even simpler and more expressive than Java. Before we look at the constructs themselves, however, we have to examine Groovy’s answer to that age-old philosophical question: What is truth?[1]

1 Groovy has no opinion as to what beauty is. We’re sure that if it did, however, it would involve expressive minimalism. Closures too, probably.

6.1. Groovy truth

6.2. Conditional execution structures

6.3. Looping

6.4. Exiting blocks and methods

6.5. Summary

sitemap