Operators, strings and subroutines
แนะนำใหม่
13:08
การเขียนโปรแกรม
นี่คือโปรแกรมสั้นๆ อ่านคะแนนทดสอบห้าตัวและพิมพ์ผลรวม คะแนนทั้งห้าคือแปด, เก้า, เจ็ด, หก และสิบ ดังนั้นผลรวมควรจะเป็นสี่สิบ ดูเลย…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| program/ˈprəʊɡræm/ | A set of instructions specifying operations for a computer to carry out. |
| values/ˈvæljuːz/ | |
| operators/ˈɒpəreɪtəz/ | ตัวดำเนินการ |
| strings/strɪŋz/ | LENGTH, SUBSTRING , UCASE, LCASE. |
| code/kəʊd/ | |
| comments/ˈkɒments/ | Explanatory text in source code or markup that does not execute as a program instruction. |
| Arithmetic/əˈrɪθmətɪk/ | |
| power/ˈpaʊə/ | |
| library routines/ˈlaɪbrəri ruːˈtiːnz/ | Reusable functions or procedures supplied in a collection for programs to call. |
| remainder/rɪˈmeɪndə/ | For integer division here, the amount left after taking whole groups of the divisor. |
| Relational/rɪˈleɪʃənl/ | |
| Boolean/ˈbuːlɪən/ | A logical value that is either true or false. |
| Logical/ˈlɒdʒɪkl/ | |
| characters/ˈkærɪktəz/ | A text unit such as a letter, digit, punctuation mark or other symbol. |
| LENGTH/leŋθ/ | |
| SUBSTRING/ˈsʌbstrɪŋ/ | |
| UCASE | |
| LCASE | |
| procedure/prəˈsiːdʒə/ | กระบวนการ |
| function/ˈfʌŋkʃn/ | ฟังก์ชัน |
| RETURN/rɪˈtɜːn/ | |
| parameter/pəˈræmɪtə/ | พารามิเตอร์ |
| local variable/ˈləʊkl ˈveərɪəbl/ | ตัวแปรภายใน |
| subroutine/ˈsʌbruːtiːn/ | |
| global/ˈɡləʊbl/ | ระดับสากล |
| CALL/kɔːl/ |
เครื่องมือของโปรแกรม
- โปรแกรมจะรวมค่าด้วย ตัวดำเนินการ (operators) และทำงานกับ สตริง (strings)
- เพื่อหลีกเลี่ยงการเขียนโค้ดซ้ำซ้อน เราจะใช้ procedure และ function
- การตั้งชื่อและการใส่ Comment ที่ดีทำให้โปรแกรม แก้ไขบำรุงรักษาได้ง่าย (maintainable)
ตัวดำเนินการ
- Arithmetic:
+ - * /,^(เลขยกกำลัง),MOD(เศษเหลือ),DIV(ส่วนจำนวนเต็ม) เช่น17 MOD 5 = 2,17 DIV 5 = 3 - Relational (ให้ผลลัพธ์เป็น Boolean):
= < <= > >= <>(ไม่เท่ากับ) - Logical:
AND(ถูกทั้งคู่),OR(ถูกอย่างน้อยหนึ่ง),NOT(กลับด้าน)

ลูปเงื่อนไขก่อน (WHILE) ตรวจสอบก่อนส่วนประกอบหลัก ดังนั้นอาจไม่ทำงานเลย; ลูปเงื่อนไขหลัง (REPEAT) ตรวจสอบหลังส่วนประกอบหลัก ดังนั้นจึงทำงานอย่างน้อยหนึ่งครั้ง.

What is 17 MOD 5?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
MOD gives the remainder: 17 ÷ 5 = 3 remainder 2.
What is 17 DIV 5?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
DIV gives the whole-number part: 17 ÷ 5 = 3 (remainder discarded).
การจัดการสตริง
- สตริงประกอบด้วยตัวอักษร การดำเนินการที่มีประโยชน์:
- LENGTH (จำนวนตัวอักษร), SUBSTRING (ส่วนย่อย), UCASE (ตัวพิมพ์ใหญ่), LCASE (ตัวพิมพ์เล็ก)
name ← "Computer"
OUTPUT LENGTH(name) // 8
OUTPUT SUBSTRING(name, 1, 4) // "Comp"

ตัวแปรคือที่เก็บที่มีชื่อซึ่งค่าสามารถเปลี่ยนแปลงได้
SUBSTRING: take part of a string
The first character is at position 1. SUBSTRING(Word, Start, Length) takes Length characters beginning at Start — drag the two handles and read the call it builds.
What does LENGTH("Computer") return?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
"Computer" has 8 characters.
Match each operator or string function to what it gives.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
MOD/DIV split a division into remainder and quotient; LENGTH and SUBSTRING work on strings.
Procedure, function และสไตล์ที่ดี
- Procedure ทำงาน (เรียกใช้ด้วย
CALL); function คืนค่า (returns a value) - Parameter คือค่าที่ถูกส่งเข้า (สูงสุดสามค่า)
- Local variable มีอยู่เฉพาะภายใน subroutine ของมันเอง (ปลอดภัยกว่า); global มองเห็นได้ทุกที่
- Library routines:
MOD,DIV,ROUND,RANDOM - โปรแกรมที่ แก้ไขบำรุงรักษาได้ง่าย ใช้ ชื่อที่มีความหมาย, Comment, และ subroutine

ชนิดข้อมูลพื้นฐาน: integer, real, char, string และ boolean
A local variable:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Locals are safer because other parts of the program cannot change them by mistake.
A function returns a value (used in an expression) while a procedure just performs a task, and a local variable can only be used inside the subroutine where it is declared.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Functions return values; locals keep variables safe inside their own subroutine — both make code easier to maintain.
การ์ดนี้เป็นการ์ดใหม่ จึงแสดงเป็นภาษาอังกฤษจนกว่าคำแปลจะพร้อม
Worked example: minutes to hours
A film lasts 135 minutes. Use DIV and MOD to calculate its length in hours and minutes, then write a function that returns 返回 the whole number of hours.
DIV(135, 60)= 2 (the whole number of hours) andMOD(135, 60)= 15 (the remainder), so the film is 2 hours 15 minutes.- As a function with a parameter, it returns a value to the program that called 调用 it:
FUNCTION Hours(Mins : INTEGER) RETURNS INTEGER
RETURN DIV(Mins, 60)
ENDFUNCTION

What is 23 MOD 4?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
23 = 5 × 4 + 3, so the remainder is 3.
Name ← "Shanghai". What does SUBSTRING(Name, 1, 5) return?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
It takes 5 characters starting at position 1.
คุณเข้าใจแล้ว
MOD= เศษเหลือ,DIV= ส่วนจำนวนเต็ม; logicalAND/OR/NOT- strings: LENGTH, SUBSTRING, UCASE, LCASE
- procedure ทำงาน; function คืนค่า; parameters ส่งค่าเข้ามา
- local ปลอดภัยกว่า global; แก้ไขบำรุงรักษาง่าย = ชื่อมีความหมาย + Comment + subroutine
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้