Control structures are fundamental in any programming language as they allow you to control the flow of your program. In Groovy, control structures are similar to those in Java, but with some enhancements and syntactic sugar that make them more concise and expressive.
- If-Else Statements
The if-else statement is used to execute a block of code based on a condition.
Syntax
if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}Example
Explanation
- The
ifstatement checks if theageis greater than or equal to 18. - If the condition is true, it prints "You are an adult."
- Otherwise, it prints "You are a minor."
- Switch Statement
The switch statement allows you to execute one block of code among many based on the value of a variable.
Syntax
switch (variable) {
case value1:
// code to be executed if variable == value1
break
case value2:
// code to be executed if variable == value2
break
default:
// code to be executed if variable doesn't match any case
}Example
def day = "Monday"
switch (day) {
case "Monday":
println("Start of the work week.")
break
case "Friday":
println("End of the work week.")
break
default:
println("Midweek day.")
}Explanation
- The
switchstatement checks the value ofday. - If
dayis "Monday", it prints "Start of the work week." - If
dayis "Friday", it prints "End of the work week." - For any other value, it prints "Midweek day."
- For Loop
The for loop is used to iterate over a range of values or a collection.
Syntax
Example
Explanation
- The
forloop iterates over the range from 1 to 5. - For each value of
i, it prints "Number: $i".
- While Loop
The while loop executes a block of code as long as a condition is true.
Syntax
Example
Explanation
- The
whileloop checks ifcountis less than or equal to 5. - If true, it prints "Count: $count" and increments
countby 1. - The loop continues until
countis greater than 5.
- Do-While Loop
The do-while loop is similar to the while loop, but it guarantees that the block of code is executed at least once.
Syntax
Example
Explanation
- The
do-whileloop executes the block of code first. - Then it checks if
countis less than or equal to 5. - If true, it repeats the loop.
Practical Exercises
Exercise 1: If-Else Statement
Write a Groovy script that checks if a number is positive, negative, or zero.
Solution
def number = -5
if (number > 0) {
println("The number is positive.")
} else if (number < 0) {
println("The number is negative.")
} else {
println("The number is zero.")
}Exercise 2: Switch Statement
Write a Groovy script that prints the name of the month based on a given number (1-12).
Solution
def monthNumber = 3
switch (monthNumber) {
case 1:
println("January")
break
case 2:
println("February")
break
case 3:
println("March")
break
case 4:
println("April")
break
case 5:
println("May")
break
case 6:
println("June")
break
case 7:
println("July")
break
case 8:
println("August")
break
case 9:
println("September")
break
case 10:
println("October")
break
case 11:
println("November")
break
case 12:
println("December")
break
default:
println("Invalid month number.")
}Exercise 3: For Loop
Write a Groovy script that prints the first 10 even numbers.
Solution
Exercise 4: While Loop
Write a Groovy script that prints the numbers from 10 to 1 in descending order.
Solution
Exercise 5: Do-While Loop
Write a Groovy script that prints the numbers from 1 to 5 using a do-while loop.
Solution
Conclusion
In this section, we covered the basic control structures in Groovy, including if-else statements, switch statements, for loops, while loops, and do-while loops. These constructs are essential for controlling the flow of your programs and making decisions based on conditions. Practice the exercises provided to reinforce your understanding and prepare for more advanced topics in Groovy.
