Control Structures and Functions
Learn about conditional expressions (if/else), loops, and defining simple functions in Scala.
Control Structures and Functions is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Guide Your Code's Path
Imagine your program as a journey. Sometimes you need to make decisions, like taking a left or right turn. Other times, you need to repeat an action, like walking several steps.
Control structures are tools that let your code make these decisions and repeat actions. They determine the order in which statements are executed, guiding the flow of your program.
Conditional Logic: If/Else
The most basic control structure is the if/else expression. It allows your program to execute different blocks of code based on whether a condition is true or false.
In Scala, if is an expression, meaning it always returns a value. This is a powerful feature compared to many other languages where if is just a statement.
If/Else in Action
Let's see how if/else works. This example checks if a number is greater than 5 and assigns a string based on the result.
object Main {
def main(args: Array[String]): Unit = {
val x = 10
val result = if (x > 5) {
"Greater"
} else {
"Smaller"
}
println(result)
}
}Handling Multiple Conditions
What if you have more than two possibilities? You can chain multiple conditions using else if to create more complex decision paths.
The code will check conditions in order. The first one that evaluates to true will have its code block executed, and the rest will be skipped.
Repeating Actions: While Loops
Sometimes you need to repeat a block of code until a certain condition is no longer met. This is where loops come in handy.
The while loop repeatedly executes a block of code as long as its condition remains true. Be careful not to create an infinite loop by always keeping the condition true!
While Loop Example
This simple while loop prints a message three times. Notice how the variable i is updated inside the loop to eventually make the condition i < 3 false.
object Main {
def main(args: Array[String]): Unit = {
var i = 0
while (i < 3) {
println(s"Count: $i")
i = i + 1
}
}
}Iterating with For Expressions
Scala's for expression is incredibly versatile for iteration. It can iterate over ranges, collections, and even filter values.
For now, let's focus on iterating over a simple range of numbers. The to keyword creates an inclusive range.
For Loop Code Example
Here's a for loop that iterates from 1 to 3 (inclusive) and prints each number. This is a common way to perform repetitive tasks for a known number of times.
object Main {
def main(args: Array[String]): Unit = {
for (i <- 1 to 3) {
println(s"Number: $i")
}
}
}Defining Your Own Functions
Functions are blocks of code designed to perform a particular task. They help you organize your code, make it reusable, and easier to understand.
In Scala, you define a function using the def keyword, followed by its name, parameters (with their types), and an optional return type. If the function returns Unit (nothing), the return type can be omitted.
Simple Function Example
Let's define a function called add that takes two integers and returns their sum. Notice how we call the function from our main method.
object Main {
def add(a: Int, b: Int): Int = {
a + b
}
def main(args: Array[String]): Unit = {
val sum = add(5, 3)
println(s"The sum is: $sum")
}
}Quick Check: Control Flow
What will the following Scala code print?
Recap: Control & Functions
Great job! In this lesson, you've learned to guide your code's execution:
if/elseexpressions for making decisions, returning a value based on conditions.whileloops for repeating actions as long as a condition is true.forexpressions for iterating over ranges and other sequences.- Defining functions to encapsulate reusable blocks of code.
These are fundamental building blocks for any program, allowing you to create dynamic and organized applications!
Frequently asked questions
Is the “Control Structures and Functions” lesson free?
Yes — the full text of “Control Structures and Functions” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.
What will I learn in “Control Structures and Functions”?
Learn about conditional expressions (if/else), loops, and defining simple functions in Scala. You practise Scala for Backend Engineering & Functional Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Scala for Backend Engineering & Functional Programming?
No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Control Structures and Functions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Scala for Backend Engineering & Functional Programming lesson?
Yes. Every Scala for Backend Engineering & Functional Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Getting Started with Scala
- Basic Syntax and Types
- Control Structures and Functions