0Pricing
Scala for Backend Engineering & Functional Programming · Lektion

Kontrollstrukturen und Funktionen

Lernen Sie bedingte Ausdrücke (if/else), Schleifen und das Definieren einfacher Funktionen in Scala kennen.

Kontrollstrukturen und Funktionen ist eine kostenlose Scala for Backend Engineering & Functional Programming-Lektion auf CoddyKit. Dies ist Lektion 3 von 3. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Scala for Backend Engineering & Functional Programming-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Scala for Backend Engineering & Functional Programming-Kurs umfasst insgesamt 3 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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/else expressions for making decisions, returning a value based on conditions.
  • while loops for repeating actions as long as a condition is true.
  • for expressions 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!

Häufig gestellte Fragen

Ist die Lektion „Kontrollstrukturen und Funktionen“ kostenlos?

Ja — der vollständige Text von „Kontrollstrukturen und Funktionen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Scala for Backend Engineering & Functional Programming-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Scala for Backend Engineering & Functional Programming-Kurs umfasst insgesamt 3 Lektionen.

Was lerne ich in „Kontrollstrukturen und Funktionen“?

Lernen Sie bedingte Ausdrücke (if/else), Schleifen und das Definieren einfacher Funktionen in Scala kennen. Du übst Scala for Backend Engineering & Functional Programming mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Scala for Backend Engineering & Functional Programming zu starten?

Keine Vorkenntnisse erforderlich. Scala for Backend Engineering & Functional Programming auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 3.

Wie lange dauert die Lektion „Kontrollstrukturen und Funktionen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Scala for Backend Engineering & Functional Programming-Lektion Code schreiben und ausführen?

Ja. Jede Scala for Backend Engineering & Functional Programming-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Erste Schritte mit Scala
  2. Grundlegende Syntax und Typen
  3. Kontrollstrukturen und Funktionen
← Zurück zu Scala for Backend Engineering & Functional Programming