0Pricing
Scala for Backend Engineering & Functional Programming · 강의

제어 구조와 함수

Scala의 조건식(if/else), 반복문과 간단한 함수 정의 방법을 배웁니다.

제어 구조와 함수은(는) CoddyKit의 무료 Scala for Backend Engineering & Functional Programming 강의입니다. 이것은 3개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Scala for Backend Engineering & Functional Programming 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Scala for Backend Engineering & Functional Programming 강의에는 총 3개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

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!

자주 묻는 질문

“제어 구조와 함수” 강의는 무료인가요?

네 — “제어 구조와 함수” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Scala for Backend Engineering & Functional Programming 강의 전체를 잠금 해제할 수 있습니다. Scala for Backend Engineering & Functional Programming 강의에는 총 3개의 강의가 포함되어 있습니다.

“제어 구조와 함수”에서 뭘 배우나요?

Scala의 조건식(if/else), 반복문과 간단한 함수 정의 방법을 배웁니다. 브라우저에서 직접 실행하는 실습 코드로 Scala for Backend Engineering & Functional Programming을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Scala for Backend Engineering & Functional Programming을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Scala for Backend Engineering & Functional Programming은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 3개 중 3번째 강의입니다.

“제어 구조와 함수” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Scala for Backend Engineering & Functional Programming 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Scala for Backend Engineering & Functional Programming 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. Scala 시작하기
  2. 기본 구문과 타입
  3. 제어 구조와 함수
← Scala for Backend Engineering & Functional Programming(으)로 돌아가기