بُنى التحكم والدوال
تعلّم التعبيرات الشرطية (if/else) والحلقات وكيفية تعريف الدوال البسيطة في Scala
بُنى التحكم والدوال درس مجاني في Scala for Backend Engineering & Functional Programming على CoddyKit. هذا هو الدرس 3 من أصل 3. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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/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!
الأسئلة الشائعة
هل درس «بُنى التحكم والدوال» مجاني؟
نعم — نص درس «بُنى التحكم والدوال» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Scala for Backend Engineering & Functional Programming، انتقل إلى CoddyKit PRO. تتضمن دورة Scala for Backend Engineering & Functional Programming 3 دروس في المجموع.
ماذا ستتعلم في «بُنى التحكم والدوال»؟
تعلّم التعبيرات الشرطية (if/else) والحلقات وكيفية تعريف الدوال البسيطة في Scala تتمرن على Scala for Backend Engineering & Functional Programming مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Scala for Backend Engineering & Functional Programming؟
لا تُشترط خبرة سابقة. Scala for Backend Engineering & Functional Programming على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 3.
كم من الوقت يستغرق درس «بُنى التحكم والدوال»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Scala for Backend Engineering & Functional Programming هذا؟
نعم. كل درس في Scala for Backend Engineering & Functional Programming يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- البدء باستخدام Scala
- البنية النحوية والأنواع الأساسية
- بُنى التحكم والدوال