โครงสร้างควบคุมและลูป
เชี่ยวชาญคำสั่งเงื่อนไข (if/else) และโครงสร้างลูปต่าง ๆ (for, while) เพื่อกำหนดพฤติกรรมของสัญญาแบบไดนามิก
โครงสร้างควบคุมและลูป เป็นบทเรียน Blockchain Smart Contracts with Solidity ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Blockchain Smart Contracts with Solidity และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Blockchain Smart Contracts with Solidity มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Control Structures Intro
Welcome to Lesson 2! Today, we'll learn about control structures in Solidity. These are essential for making your smart contracts dynamic and responsive.
Control structures allow your contract to:
- Make decisions based on conditions.
- Repeat actions multiple times.
- Execute different code paths.
Think of them as the 'brain' of your contract, guiding its behavior.
Conditional 'if' Statements
The simplest control structure is the if statement. It executes a block of code only if a specified condition is true.
The condition must evaluate to a bool (true or false). If true, the code inside the curly braces {} runs; otherwise, it's skipped.
'if-else' for Alternatives
Often, you want to perform one action if a condition is true and a different action if it's false. This is where the if-else statement comes in.
If the if condition is true, its block executes. If false, the code in the else block executes instead. Only one of the two blocks will ever run.
'else if' for Multiple Choices
What if you have more than two possible outcomes? Use else if to check multiple conditions sequentially.
The contract checks each condition in order. The first one that evaluates to true will have its code block executed, and all subsequent else if and else blocks are skipped.
Conditional Logic Example
Let's see if-else if-else in action. This contract checks if a number is positive, negative, or zero.
Try calling checkNumber with different integer values like 5, -3, or 0.
pragma solidity ^0.8.0;
contract ConditionalChecker {
function checkNumber(int _number) public pure returns (string memory) {
if (_number > 0) {
return "Positive";
} else if (_number < 0) {
return "Negative";
} else {
return "Zero";
}
}
}Introducing Loops
Sometimes you need to repeat a block of code multiple times. This is called looping, and it's super efficient!
Loops are useful for:
- Iterating over arrays or lists.
- Performing calculations multiple times.
- Waiting for a condition to be met.
Solidity provides for and while loops.
The 'for' Loop
The for loop is perfect when you know exactly how many times you want to repeat an action. It has three parts:
- Initialization: Sets up a counter (e.g.,
uint i = 0). - Condition: The loop continues as long as this is true (e.g.,
i < 10). - Increment/Decrement: Changes the counter after each iteration (e.g.,
i++).
Runnable 'for' Loop
Here's a for loop example that calculates the sum of numbers up to a given limit. This shows how to accumulate a value over several iterations.
Try calling calculateSum with a small number like 5.
pragma solidity ^0.8.0;
contract ForLoopDemo {
// Calculates the sum of numbers from 1 up to _limit
function calculateSum(uint _limit) public pure returns (uint) {
uint total = 0;
for (uint i = 1; i <= _limit; i++) {
total += i;
}
return total;
}
}The 'while' Loop
The while loop is ideal when you don't know exactly how many times the loop needs to run, but you have a specific condition that must be met to stop.
The loop continues as long as its condition is true. Be careful to ensure the condition eventually becomes false to avoid infinite loops!
Runnable 'while' Loop
This example demonstrates a simple countdown using a while loop. The loop continues as long as currentCount is greater than zero.
Call simpleCountdown with a value like 3. It will return 0 after completing the countdown.
pragma solidity ^0.8.0;
contract WhileLoopDemo {
// Counts down from a starting number to zero
function simpleCountdown(uint _start) public pure returns (uint) {
uint currentCount = _start;
while (currentCount > 0) {
currentCount--; // Decrement counter
}
return currentCount; // Will be 0 when loop finishes
}
}Check Your Understanding
Consider the following Solidity contract function:
pragma solidity ^0.8.0;
contract ScoreChecker {
function getStatus(uint _score) public pure returns (string memory) {
if (_score >= 90) {
return "Excellent";
} else if (_score >= 70) {
return "Good";
} else if (_score >= 50) {
return "Pass";
} else {
return "Fail";
}
}
}Recap: Control & Loops
Great job! You've mastered the fundamentals of control structures and loops in Solidity.
- Conditional statements (
if,else if,else) allow your contract to make decisions. - Loops (
for,while) enable repetitive execution of code blocks.
These tools are crucial for building smart contracts that can react dynamically and perform complex operations. Next, we'll dive into functions and visibility modifiers!
คำถามที่พบบ่อย
บทเรียน “โครงสร้างควบคุมและลูป” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “โครงสร้างควบคุมและลูป” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Blockchain Smart Contracts with Solidity ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Blockchain Smart Contracts with Solidity มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “โครงสร้างควบคุมและลูป”
เชี่ยวชาญคำสั่งเงื่อนไข (if/else) และโครงสร้างลูปต่าง ๆ (for, while) เพื่อกำหนดพฤติกรรมของสัญญาแบบไดนามิก คุณปฏิบัติ Blockchain Smart Contracts with Solidity ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Blockchain Smart Contracts with Solidity หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Blockchain Smart Contracts with Solidity บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “โครงสร้างควบคุมและลูป” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Blockchain Smart Contracts with Solidity นี้ได้ไหม
ได้ บทเรียน Blockchain Smart Contracts with Solidity ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ชนิดข้อมูลและตัวแปรใน Solidity
- โครงสร้างควบคุมและลูป
- ฟังก์ชันและตัวปรับการมองเห็น
- โครงสร้างและแจกแจง